use chrono::{DateTime, Utc};
pub trait Clock: Send + Sync {
fn now(&self) -> DateTime<Utc>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SystemClock;
impl Clock for SystemClock {
fn now(&self) -> DateTime<Utc> {
Utc::now()
}
}
#[cfg(feature = "test-utils")]
#[derive(Clone, Copy, Debug)]
pub struct FixedClock {
instant: DateTime<Utc>,
}
#[cfg(feature = "test-utils")]
impl FixedClock {
pub fn new(instant: DateTime<Utc>) -> Self {
Self { instant }
}
pub fn instant(&self) -> DateTime<Utc> {
self.instant
}
}
#[cfg(feature = "test-utils")]
impl Clock for FixedClock {
fn now(&self) -> DateTime<Utc> {
self.instant
}
}
#[cfg(all(test, feature = "test-utils"))]
mod tests {
use super::*;
#[test]
fn fixed_clock_does_not_advance() {
let instant = DateTime::from_timestamp(1_700_000_000, 0).unwrap();
let clock = FixedClock::new(instant);
assert_eq!(clock.now(), instant);
assert_eq!(clock.now(), clock.now());
}
}