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
use time::{SteadyTime, Timespec, get_time};
use std::ops::Add;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Time(u64);
impl Add<Duration> for Time {
type Output = Time;
fn add(self, rhs: Duration) -> Time {
Time(self.0 + rhs.as_secs()*1000 + (rhs.subsec_nanos()/1000000) as u64)
}
}
impl Time {
pub fn zero() -> Time {
Time(1)
}
}
pub fn make_time(base: SteadyTime, now: SteadyTime) -> Time {
Time((now - base).num_milliseconds() as u64
+ 1)
}
pub fn mio_timeout_ms(now: Time, event: Time) -> u64 {
if event.0 > now.0 {
event.0 - now.0 + 1
} else {
0
}
}
pub fn estimate_timespec(now: Time, value: Time) -> Timespec {
get_time() + ::time::Duration::milliseconds(value.0 as i64 - now.0 as i64)
}
#[cfg(test)]
mod test {
use super::Time;
use std::time::Duration;
#[test]
fn test_add_duration() {
let tm = Time::zero();
assert_eq!(tm + Duration::new(10, 0), Time(10001));
assert_eq!(tm + Duration::from_millis(150), Time(151));
assert_eq!(tm + Duration::from_millis(12345), Time(12346));
assert_eq!(tm + Duration::new(5, 0) + Duration::from_millis(20),
Time(5021));
}
}