Skip to main content

evault_core/traits/
clock.rs

1//! [`Clock`] — abstraction over "current time".
2//!
3//! Inject a fake clock in tests to assert that timestamps are recorded
4//! deterministically without depending on `OffsetDateTime::now_utc()`.
5
6use time::OffsetDateTime;
7
8/// Source of the current wall-clock time, in UTC.
9pub trait Clock: Send + Sync {
10    /// Return the current UTC instant.
11    fn now(&self) -> OffsetDateTime;
12}
13
14/// Production [`Clock`] that delegates to [`OffsetDateTime::now_utc`].
15#[derive(Debug, Default, Clone, Copy)]
16pub struct SystemClock;
17
18impl Clock for SystemClock {
19    fn now(&self) -> OffsetDateTime {
20        OffsetDateTime::now_utc()
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn system_clock_returns_recent_time() {
30        let before = OffsetDateTime::now_utc();
31        let t = SystemClock.now();
32        let after = OffsetDateTime::now_utc();
33        assert!(t >= before);
34        assert!(t <= after);
35    }
36}