Skip to main content

sendmock_core/clock/
system_clock.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3use super::Clock;
4
5#[derive(Debug, Default, Clone, Copy)]
6pub struct SystemClock;
7
8impl Clock for SystemClock {
9    fn now_unix_ms(&self) -> i64 {
10        // A system clock set before 1970 reads as the epoch instead of failing.
11        let elapsed_since_epoch = SystemTime::now()
12            .duration_since(UNIX_EPOCH)
13            .unwrap_or_default();
14        i64::try_from(elapsed_since_epoch.as_millis()).unwrap_or(i64::MAX)
15    }
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn reads_a_time_after_the_year_2024() {
24        let first_of_january_2024_unix_ms = 1_704_067_200_000;
25
26        assert!(SystemClock.now_unix_ms() > first_of_january_2024_unix_ms);
27    }
28}