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
pub trait DurationSinceEpoch {
fn as_secs(&self) -> u64;
fn as_millis(&self) -> u64;
}
#[cfg(feature = "std")]
use std::convert::From;
#[cfg(feature = "std")]
use std::time::{Duration, SystemTime};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg(feature = "std")]
pub struct StdDurationSinceEpoch(Duration);
#[cfg(feature = "std")]
impl StdDurationSinceEpoch {
pub fn now() -> Self {
StdDurationSinceEpoch(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("SystemTime should be later than UNIX_EPOCH."),
)
}
}
#[cfg(feature = "std")]
impl DurationSinceEpoch for StdDurationSinceEpoch {
fn as_secs(&self) -> u64 {
self.0.as_secs()
}
fn as_millis(&self) -> u64 {
self.0.as_secs() * 1000 + u64::from(self.0.subsec_millis())
}
}