use super::{Clock, MockClock, SystemClock};
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use std::sync::Arc;
impl SystemClock {
pub fn new() -> Self {
Self
}
}
impl Default for SystemClock {
fn default() -> Self {
Self::new()
}
}
impl Clock for SystemClock {
fn now(&self) -> DateTime<Utc> {
chrono::Utc::now()
}
}
impl MockClock {
pub fn new(time: DateTime<Utc>) -> Self {
Self {
time: Arc::new(RwLock::new(time)),
}
}
pub fn set_time(&self, time: DateTime<Utc>) {
*self.time.write() = time;
}
pub fn advance(&self, duration: chrono::Duration) {
let mut w = self.time.write();
*w += duration;
}
}
impl Clock for MockClock {
fn now(&self) -> DateTime<Utc> {
*self.time.read()
}
}