use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[derive(Debug)]
pub struct TimeProvider;
impl Default for TimeProvider {
fn default() -> Self {
Self::new()
}
}
impl TimeProvider {
pub fn new() -> Self {
Self
}
pub fn now_micros(&self) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|_| Duration::from_secs(0))
.as_micros() as u64
}
pub fn now_millis(&self) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|_| Duration::from_secs(0))
.as_millis() as u64
}
pub fn now_secs(&self) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|_| Duration::from_secs(0))
.as_secs()
}
pub fn now(&self) -> SystemTime {
SystemTime::now()
}
pub fn duration_from_millis(&self, millis: u64) -> Duration {
Duration::from_millis(millis)
}
pub fn duration_from_secs(&self, secs: u64) -> Duration {
Duration::from_secs(secs)
}
}