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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::epoch::Epoch;
use ::std::time::Duration;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MonotonicClock {
epoch: Epoch, start: ::std::time::Instant,
stop: Option<::std::time::Instant>,
}
impl Default for MonotonicClock {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl MonotonicClock {
#[inline]
pub fn new() -> MonotonicClock {
MonotonicClock {
epoch: Epoch::from_unix(),
start: ::std::time::Instant::now(),
stop: None,
}
}
#[inline]
pub fn reset(&mut self) {
self.start = ::std::time::Instant::now();
self.stop = None;
}
#[inline]
pub fn start(&mut self) {
self.start = ::std::time::Instant::now();
self.stop = None;
}
#[inline]
pub fn resume(&mut self) -> Option<Duration> {
if let Some(stop) = self.stop {
self.stop = None;
::std::time::Instant::now().checked_duration_since(stop)
}
else {
Some(Duration::new(0, 0))
}
}
#[inline]
pub fn stop(&mut self) -> Option<Duration> {
if self.stop.is_none() {
self.stop = Some(::std::time::Instant::now());
}
self.stop.map(|stop| stop - self.start)
}
#[inline]
pub fn now(&self) -> Duration {
if let Some(stop) = self.stop {
stop.duration_since(self.start)
} else {
::std::time::Instant::now().duration_since(self.start)
}
}
#[inline]
pub fn time(&self) -> Duration {
self.now() + *self.epoch
}
#[inline]
pub fn time_as_float(&self) -> f64 {
let time = self.time();
time.as_secs() as f64 + time.subsec_nanos() as f64 * 1e-9
}
#[inline]
pub fn as_float(&self) -> f64 {
self.time_as_float()
}
#[inline]
pub fn is_ticking(&self) -> bool {
self.stop.is_none()
}
}
impl ::std::fmt::Display for MonotonicClock {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self.time_as_float())
}
}
impl ::core::convert::Into<Duration> for MonotonicClock {
fn into(self) -> Duration {
self.time()
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert2::assert;
#[test]
fn test_monotonic_clock() {
let mut clock = MonotonicClock::new();
assert!(clock.now() < Duration::from_secs(1));
::std::thread::sleep(Duration::from_secs(1));
assert!(clock.now() > Duration::from_secs(1));
::std::thread::sleep(Duration::from_secs(2));
let stopped_at = clock.stop().unwrap();
assert!(clock.now() > Duration::from_secs(2));
assert!(clock.now() == stopped_at);
::std::thread::sleep(Duration::from_secs(2));
assert!(clock.now() > Duration::from_secs(2));
assert!(clock.now() == stopped_at);
clock.resume();
::std::thread::sleep(Duration::from_secs(1));
assert!(clock.now() > stopped_at);
clock.reset();
assert!(clock.now() < Duration::from_secs(1));
}
#[test]
fn test_monotonic_clock_since_unix_epoch() {
let clock = MonotonicClock::new();
eprintln!("clock.epoch = {:?}", clock.epoch);
eprintln!("clock.now() = {:?}", clock.time());
}
}