33use std:: time:: { SystemTime , Instant } ;
44
55fn main ( ) {
6+ // Check `SystemTime`.
67 let now1 = SystemTime :: now ( ) ;
78 // Do some work to make time pass.
89 for _ in 0 ..10 { drop ( vec ! [ 42 ] ) ; }
910 let now2 = SystemTime :: now ( ) ;
1011 assert ! ( now2 > now1) ;
1112 let diff = now2. duration_since ( now1) . unwrap ( ) ;
12- assert ! ( diff. as_micros( ) > 0 ) ;
1313 assert_eq ! ( now1 + diff, now2) ;
1414 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 ) ;
1520
21+ // Check `Instant`.
1622 #[ cfg( not( windows) ) ] // `Instant` shims not yet implemented on Windows
1723 {
1824 let now1 = Instant :: now ( ) ;
@@ -24,9 +30,11 @@ fn main() {
2430 #[ cfg( target_os = "linux" ) ] // TODO: macOS does not support Instant subtraction
2531 {
2632 let diff = now2. duration_since ( now1) ;
27- assert ! ( diff. as_micros( ) > 0 ) ;
2833 assert_eq ! ( now1 + diff, now2) ;
2934 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 ) ;
3038 }
3139 }
3240}
0 commit comments