3
3
use std:: time:: { SystemTime , Instant } ;
4
4
5
5
fn main ( ) {
6
+ // Check `SystemTime`.
6
7
let now1 = SystemTime :: now ( ) ;
7
8
// Do some work to make time pass.
8
9
for _ in 0 ..10 { drop ( vec ! [ 42 ] ) ; }
9
10
let now2 = SystemTime :: now ( ) ;
10
11
assert ! ( now2 > now1) ;
11
12
let diff = now2. duration_since ( now1) . unwrap ( ) ;
12
- assert ! ( diff. as_micros( ) > 0 ) ;
13
13
assert_eq ! ( now1 + diff, now2) ;
14
14
assert_eq ! ( now2 - diff, now1) ;
15
+ // Sanity-check the time we got.
16
+ let seconds_since_epoch = now1. duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) ;
17
+ let years_since_epoch = seconds_since_epoch / 3600 / 24 / 365 ;
18
+ let year = 1970 + years_since_epoch;
19
+ assert ! ( 2020 <= year && year < 2100 ) ;
15
20
21
+ // Check `Instant`.
16
22
#[ cfg( not( windows) ) ] // `Instant` shims not yet implemented on Windows
17
23
{
18
24
let now1 = Instant :: now ( ) ;
@@ -24,9 +30,11 @@ fn main() {
24
30
#[ cfg( target_os = "linux" ) ] // TODO: macOS does not support Instant subtraction
25
31
{
26
32
let diff = now2. duration_since ( now1) ;
27
- assert ! ( diff. as_micros( ) > 0 ) ;
28
33
assert_eq ! ( now1 + diff, now2) ;
29
34
assert_eq ! ( now2 - diff, now1) ;
35
+ // Sanity-check the difference we got.
36
+ assert ! ( diff. as_micros( ) > 1 ) ;
37
+ assert ! ( diff. as_micros( ) < 1_000_000 ) ;
30
38
}
31
39
}
32
40
}
0 commit comments