1use std::time::{Duration, SystemTime, UNIX_EPOCH};
8
9pub trait Clock: Send + Sync {
12 fn unix_secs(&self) -> u64;
14
15 fn now_rfc3339(&self) -> String {
18 format_rfc3339_utc(self.unix_secs())
19 }
20}
21
22#[derive(Debug, Default, Clone, Copy)]
24pub struct SystemClock;
25
26impl Clock for SystemClock {
27 fn unix_secs(&self) -> u64 {
28 SystemTime::now()
29 .duration_since(UNIX_EPOCH)
30 .unwrap_or(Duration::ZERO)
31 .as_secs()
32 }
33}
34
35#[derive(Debug)]
37pub struct MockClock {
38 secs: std::sync::atomic::AtomicU64,
39}
40
41impl MockClock {
42 pub fn at(secs: u64) -> Self {
44 Self {
45 secs: std::sync::atomic::AtomicU64::new(secs),
46 }
47 }
48
49 pub fn advance(&self, secs: u64) {
51 self.secs
52 .fetch_add(secs, std::sync::atomic::Ordering::SeqCst);
53 }
54}
55
56impl Default for MockClock {
57 fn default() -> Self {
58 Self::at(1_780_099_200)
60 }
61}
62
63impl Clock for MockClock {
64 fn unix_secs(&self) -> u64 {
65 self.secs.load(std::sync::atomic::Ordering::SeqCst)
66 }
67}
68
69fn format_rfc3339_utc(unix_secs: u64) -> String {
72 let days = (unix_secs / 86_400) as i64;
73 let secs_of_day = unix_secs % 86_400;
74 let (hour, min, sec) = (
75 secs_of_day / 3600,
76 (secs_of_day % 3600) / 60,
77 secs_of_day % 60,
78 );
79
80 let z = days + 719_468;
82 let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
83 let doe = (z - era * 146_097) as u64; let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; let year = yoe as i64 + era * 400;
86 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let day = doy - (153 * mp + 2) / 5 + 1; let month = if mp < 10 { mp + 3 } else { mp - 9 }; let year = if month <= 2 { year + 1 } else { year };
91
92 format!("{year:04}-{month:02}-{day:02}T{hour:02}:{min:02}:{sec:02}Z")
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn mock_clock_is_fixed_then_advances() {
101 let c = MockClock::at(1000);
102 assert_eq!(c.unix_secs(), 1000);
103 c.advance(50);
104 assert_eq!(c.unix_secs(), 1050);
105 }
106
107 #[test]
108 fn rfc3339_formats_known_instants() {
109 assert_eq!(format_rfc3339_utc(0), "1970-01-01T00:00:00Z");
111 assert_eq!(format_rfc3339_utc(1_780_099_200), "2026-05-30T00:00:00Z");
113 assert_eq!(
115 format_rfc3339_utc(1_780_099_200 + 3661),
116 "2026-05-30T01:01:01Z"
117 );
118 }
119
120 #[test]
121 fn mock_default_clock_renders_expected_date() {
122 assert_eq!(MockClock::default().now_rfc3339(), "2026-05-30T00:00:00Z");
123 }
124
125 #[test]
126 fn system_clock_is_after_2020() {
127 assert!(SystemClock.unix_secs() > 1_577_836_800); }
130}