1use std::fmt::Display;
2use std::time::{Duration, Instant};
3
4pub struct Clock {
5 pub durantion: Duration,
6 pub started_at: Instant,
7}
8
9impl Clock {
10 pub fn new(duration: Duration) -> Self {
11 Self {
12 durantion: duration,
13 started_at: Instant::now(),
14 }
15 }
16
17 pub fn has_ended(&self) -> bool {
18 self.started_at.elapsed() >= self.durantion
19 }
20}
21
22impl Display for Clock {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 let elapsed = self.started_at.elapsed();
25 let remaining = self.durantion.checked_sub(elapsed).unwrap_or_default();
26
27 write!(f, "{}", humantime::format_duration(remaining))
28 }
29}