use core::sync::atomic::{AtomicI64, Ordering};
use chrono::Utc;
use crate::epoch::EPOCH;
pub trait ClockSource: Default {
fn current_timestamp(&self) -> i64;
}
#[derive(Default)]
pub struct UtcClock;
impl ClockSource for UtcClock {
fn current_timestamp(&self) -> i64 {
Utc::now().timestamp_millis()
}
}
pub struct ManualClock {
timestamp: AtomicI64,
}
impl Default for ManualClock {
fn default() -> Self {
Self::new(EPOCH)
}
}
impl ClockSource for ManualClock {
fn current_timestamp(&self) -> i64 {
self.timestamp.load(Ordering::SeqCst)
}
}
impl ManualClock {
pub fn new(timestamp: i64) -> Self {
Self {
timestamp: AtomicI64::new(timestamp),
}
}
pub fn set_current_timestamp(&self, timestamp: i64) {
self.timestamp.store(timestamp, Ordering::SeqCst);
}
}