1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::fmt::Display;
use std::time::{Duration, Instant};

pub struct Clock {
    pub durantion: Duration,
    pub started_at: Instant,
}

impl Clock {
    pub fn new(duration: Duration) -> Self {
        Self {
            durantion: duration,
            started_at: Instant::now(),
        }
    }

    pub fn has_ended(&self) -> bool {
        self.started_at.elapsed() >= self.durantion
    }
}

impl Display for Clock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let elapsed = self.started_at.elapsed();
        let remaining = self.durantion.checked_sub(elapsed).unwrap_or_default();

        write!(f, "{}", humantime::format_duration(remaining))
    }
}