use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Timestamp(pub i64);
impl Timestamp {
pub fn as_i64(&self) -> i64 {
self.0
}
pub fn from_millis(ms: i64) -> Self {
Self(ms)
}
}
pub trait Clock {
fn now(&self) -> Timestamp;
}
pub struct SystemClock;
impl Clock for SystemClock {
fn now(&self) -> Timestamp {
let ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as i64;
Timestamp(ms)
}
}
pub struct FixedClock {
timestamp: Timestamp,
}
impl FixedClock {
pub fn new(timestamp: Timestamp) -> Self {
Self { timestamp }
}
pub fn set(&mut self, timestamp: Timestamp) {
self.timestamp = timestamp;
}
}
impl Clock for FixedClock {
fn now(&self) -> Timestamp {
self.timestamp
}
}