Skip to main content

auths_verifier/
clock.rs

1//! Clock provider abstraction for injectable time.
2
3use chrono::{DateTime, Utc};
4
5/// Provides the current wall-clock time, injectable for testing.
6///
7/// Implement this trait to decouple domain logic from `Utc::now()`.
8/// Use [`SystemClock`] in production and `MockClock` (from `auths-test-utils`)
9/// in tests.
10///
11/// Usage:
12/// ```ignore
13/// fn check_expiry(clock: &dyn ClockProvider, expires_at: DateTime<Utc>) -> bool {
14///     clock.now() < expires_at
15/// }
16/// ```
17pub trait ClockProvider: Send + Sync {
18    /// Returns the current time.
19    ///
20    /// Usage:
21    /// ```ignore
22    /// let now = clock.now();
23    /// ```
24    fn now(&self) -> DateTime<Utc>;
25}
26
27/// Production clock that delegates to [`Utc::now`].
28///
29/// Usage:
30/// ```ignore
31/// let clock = SystemClock;
32/// let timestamp = clock.now();
33/// ```
34pub struct SystemClock;
35
36impl ClockProvider for SystemClock {
37    fn now(&self) -> DateTime<Utc> {
38        Utc::now()
39    }
40}