use dcs::coordination::{Stopwatch, Timer};
use std::thread::sleep;
use std::time::{Duration, Instant, SystemTime};
pub struct OsClock {
delta: Duration,
last_instant: Instant,
}
impl Default for OsClock {
fn default() -> Self {
Self { delta: Default::default(), last_instant: Instant::now() }
}
}
impl Stopwatch for OsClock {
fn from_millis(millis: u64) -> Self {
Self { delta: Duration::from_millis(millis), last_instant: Instant::now() }
}
fn as_secs(&self) -> u64 {
self.delta.as_secs()
}
fn restart(&mut self) {
self.last_instant = Instant::now();
}
fn is_timeout(&self) -> bool {
self.last_instant + self.delta < Instant::now()
}
fn current_time_as_secs(&mut self) -> u64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
}
}
pub struct OsTimer {
delta: Duration,
}
impl Timer for OsTimer {
fn from_millis(millis: u64) -> Self {
Self { delta: Duration::from_millis(millis) }
}
fn as_secs(&self) -> u64 {
self.delta.as_secs()
}
fn wait(&self) {
sleep(self.delta);
}
}