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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extern crate chrono;
use chrono::{DateTime, Utc};
use std::cmp;
use std::time::Duration;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Timer {
NotStarted,
Started {
last_marked: DateTime<Utc>,
},
}
impl Timer {
pub fn new() -> Self {
Timer::NotStarted
}
pub fn mark(&mut self) -> Duration {
Duration::from_millis(self.mark_millis())
}
pub fn mark_millis(&mut self) -> u64 {
match *self {
Timer::NotStarted => {
*self = Timer::Started { last_marked: Utc::now() };
0
},
Timer::Started { ref mut last_marked } => {
let now = Utc::now();
let delta_ms = cmp::max(now.signed_duration_since(*last_marked).num_milliseconds(), 0);
*last_marked = now;
cmp::max(delta_ms, 0) as u64
},
}
}
pub fn mark_secs(&mut self) -> f64 {
self.mark_millis() as f64 / 1000.0
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn first_mark_is_zero_seconds() {
assert_eq!(0.0, Timer::new().mark());
}
}