Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanity check time values a bit more #1292

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions tests/run-pass/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
use std::time::{SystemTime, Instant};

fn main() {
// Check `SystemTime`.
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);
let diff = now2.duration_since(now1).unwrap();
assert!(diff.as_micros() > 0);
assert_eq!(now1 + diff, now2);
assert_eq!(now2 - diff, now1);
// 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);

// Check `Instant`.
#[cfg(not(windows))] // `Instant` shims not yet implemented on Windows
{
let now1 = Instant::now();
Expand All @@ -24,9 +30,11 @@ fn main() {
#[cfg(target_os = "linux")] // TODO: macOS does not support Instant subtraction
{
let diff = now2.duration_since(now1);
assert!(diff.as_micros() > 0);
assert_eq!(now1 + diff, now2);
assert_eq!(now2 - diff, now1);
// Sanity-check the difference we got.
assert!(diff.as_micros() > 1);
assert!(diff.as_micros() < 1_000_000);
}
}
}