clock.rs

 1use chrono::{DateTime, Duration, Utc};
 2use parking_lot::Mutex;
 3
 4pub trait Clock {
 5    fn now(&self) -> DateTime<Utc>;
 6}
 7
 8pub struct TestClock {
 9    now: Mutex<DateTime<Utc>>,
10}
11
12impl TestClock {
13    pub fn new() -> Self {
14        const START_TIME: &str = "2025-07-01T23:59:58-00:00";
15        let now = DateTime::parse_from_rfc3339(START_TIME).unwrap().to_utc();
16        Self {
17            now: Mutex::new(now),
18        }
19    }
20
21    pub fn set_now(&self, now: DateTime<Utc>) {
22        *self.now.lock() = now;
23    }
24
25    pub fn advance(&self, duration: Duration) {
26        *self.now.lock() += duration;
27    }
28}
29
30impl Clock for TestClock {
31    fn now(&self) -> DateTime<Utc> {
32        *self.now.lock()
33    }
34}