2020-03-19 17:00:02 -05:00
|
|
|
// compile-flags: -Zmiri-disable-isolation
|
|
|
|
|
|
|
|
use std::time::{SystemTime, Instant};
|
|
|
|
|
|
|
|
fn main() {
|
2020-04-01 13:12:51 -05:00
|
|
|
// Check `SystemTime`.
|
2020-03-19 17:00:02 -05:00
|
|
|
let now1 = SystemTime::now();
|
|
|
|
// Do some work to make time pass.
|
|
|
|
for _ in 0..10 { drop(vec![42]); }
|
|
|
|
let now2 = SystemTime::now();
|
|
|
|
assert!(now2 > now1);
|
2020-03-31 10:50:10 -05:00
|
|
|
let diff = now2.duration_since(now1).unwrap();
|
|
|
|
assert_eq!(now1 + diff, now2);
|
|
|
|
assert_eq!(now2 - diff, now1);
|
2020-04-01 13:12:51 -05:00
|
|
|
// Sanity-check the time we got.
|
|
|
|
let seconds_since_epoch = now1.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
|
|
|
let years_since_epoch = seconds_since_epoch / 3600 / 24 / 365;
|
|
|
|
let year = 1970 + years_since_epoch;
|
|
|
|
assert!(2020 <= year && year < 2100);
|
2020-03-19 17:00:02 -05:00
|
|
|
|
2020-04-01 13:12:51 -05:00
|
|
|
// Check `Instant`.
|
2020-03-31 10:26:32 -05:00
|
|
|
#[cfg(not(windows))] // `Instant` shims not yet implemented on Windows
|
2020-03-31 10:50:10 -05:00
|
|
|
{
|
2020-03-31 10:26:32 -05:00
|
|
|
let now1 = Instant::now();
|
|
|
|
// Do some work to make time pass.
|
|
|
|
for _ in 0..10 { drop(vec![42]); }
|
|
|
|
let now2 = Instant::now();
|
|
|
|
assert!(now2 > now1);
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")] // TODO: macOS does not support Instant subtraction
|
|
|
|
{
|
|
|
|
let diff = now2.duration_since(now1);
|
|
|
|
assert_eq!(now1 + diff, now2);
|
|
|
|
assert_eq!(now2 - diff, now1);
|
2020-04-01 13:12:51 -05:00
|
|
|
// Sanity-check the difference we got.
|
|
|
|
assert!(diff.as_micros() > 1);
|
|
|
|
assert!(diff.as_micros() < 1_000_000);
|
2020-03-31 10:26:32 -05:00
|
|
|
}
|
2020-03-31 10:50:10 -05:00
|
|
|
}
|
2020-03-19 17:00:02 -05:00
|
|
|
}
|