1use dcs::coordination::{Stopwatch, Timer};
2use std::thread::sleep;
3use std::time::{Duration, Instant, SystemTime};
4
5pub struct OsClock {
6 delta: Duration,
7 last_instant: Instant,
8}
9
10impl Default for OsClock {
11 fn default() -> Self {
12 Self { delta: Default::default(), last_instant: Instant::now() }
13 }
14}
15
16impl Stopwatch for OsClock {
17 fn from_millis(millis: u64) -> Self {
18 Self { delta: Duration::from_millis(millis), last_instant: Instant::now() }
19 }
20
21 fn as_secs(&self) -> u64 {
22 self.delta.as_secs()
23 }
24
25 fn restart(&mut self) {
26 self.last_instant = Instant::now();
27 }
28
29 fn is_timeout(&self) -> bool {
30 self.last_instant + self.delta < Instant::now()
31 }
32
33 fn current_time_as_secs(&mut self) -> u64 {
34 SystemTime::now()
35 .duration_since(SystemTime::UNIX_EPOCH)
36 .unwrap()
37 .as_secs()
38 }
39}
40
41pub struct OsTimer {
42 delta: Duration,
43}
44
45impl Timer for OsTimer {
46 fn from_millis(millis: u64) -> Self {
47 Self { delta: Duration::from_millis(millis) }
48 }
49
50 fn as_secs(&self) -> u64 {
51 self.delta.as_secs()
52 }
53
54 fn wait(&self) {
55 sleep(self.delta);
56 }
57}