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
#[cfg(not(disable_faketime))]
pub mod faketime;
pub mod system;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
mod wasm;
#[cfg(not(disable_faketime))]
pub use crate::faketime::{disable, enable, millis_tempfile, unix_time, write_millis};
#[cfg(disable_faketime)]
pub use crate::system::unix_time;
pub fn unix_time_as_millis() -> u64 {
let duration = unix_time();
duration.as_secs() * 1000 + u64::from(duration.subsec_millis())
}
#[cfg(test)]
mod tests {
use crate::*;
#[cfg(disable_faketime)]
#[test]
fn test_system() {
let system_now = system::unix_time();
let now = unix_time();
assert!((now - system_now).as_secs() < 60);
}
#[cfg(not(disable_faketime))]
#[test]
fn test_faketime() {
let faketime_file = millis_tempfile(123_456).expect("create faketime file");
enable(&faketime_file);
let now = unix_time();
assert_eq!(123, now.as_secs());
assert_eq!(456, now.subsec_millis());
}
}