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

Test Local functions against date command #682

Merged
merged 3 commits into from
May 1, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
144 changes: 143 additions & 1 deletion src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,149 @@ impl TimeZone for Local {
mod tests {
use super::Local;
use crate::offset::TimeZone;
use crate::Datelike;
use crate::{Datelike, Duration, NaiveDate};

use std::{path, process};

#[cfg(unix)]
fn verify_against_date_command_local(
path: &'static str,
year: i32,
month: u32,
day: u32,
hour: u32,
) {
let output = process::Command::new(path)
.arg("-d")
.arg(format!("{year}-{month:02}-{day:02} {hour:02}:05:01"))
.arg("+%Y-%m-%d %H:%M:%S %:z")
.output()
.unwrap();

let date_command_str = String::from_utf8(output.stdout).unwrap();

// The below would be preferred. At this stage neither earliest() or latest()
// seems to be consistent with the output of the `date` command, so we simply
// compare both.
// let local = Local
// .from_local_datetime(&NaiveDate::from_ymd(year, month, day).and_hms(hour, 5, 1))
// // looks like the "date" command always returns a given time when it is ambiguous
// .earliest();

// if let Some(local) = local {
// assert_eq!(format!("{}\n", local), date_command_str);
// } else {
// // we are in a "Spring forward gap" due to DST, and so date also returns ""
// assert_eq!("", date_command_str);
// }

match Local.from_local_datetime(&NaiveDate::from_ymd(year, month, day).and_hms(hour, 5, 1))
{
crate::LocalResult::Ambiguous(a, b) => {
assert!(
format!("{}\n", a) == date_command_str
|| format!("{}\n", b) == date_command_str
)
}
crate::LocalResult::Single(a) => {
assert_eq!(format!("{}\n", a), date_command_str);
}
crate::LocalResult::None => {
assert_eq!("", date_command_str);
}
}
}

#[test]
#[cfg(unix)]
fn try_verify_against_date_command() {
// #TODO: investigate /bin/date command behaviour on macOS
// avoid running this on macOS, temporarily
// for date_path in ["/usr/bin/date", "/bin/date"] {
for date_path in ["/usr/bin/date"] {
if path::Path::new(date_path).exists() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this use an early continue? That would help reduce rightward drift.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, the code was a bit gnarly, I've made some improvements by using NaiveDateTime instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the loop across date commands as I tried this out on macOS, but I couldn't find the same command line arguments and so wasn't able the replicate the behavior desired to find the local timezone

for year in 1975..=1977 {
for month in 1..=12 {
for day in 1..28 {
for hour in 0..23 {
verify_against_date_command_local(
date_path, year, month, day, hour,
);
}
}
}
}

for year in 2020..=2022 {
for month in 1..=12 {
for day in 1..28 {
for hour in 0..23 {
verify_against_date_command_local(
date_path, year, month, day, hour,
);
}
}
}
}

for year in 2073..=2075 {
for month in 1..=12 {
for day in 1..28 {
for hour in 0..23 {
verify_against_date_command_local(
date_path, year, month, day, hour,
);
}
}
}
}
}
}
// date command not found, skipping
}

#[test]
fn verify_correct_offsets() {
let now = Local::now();
let from_local = Local.from_local_datetime(&now.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&now.naive_utc());

assert_eq!(now.offset().local_minus_utc(), from_local.offset().local_minus_utc());
assert_eq!(now.offset().local_minus_utc(), from_utc.offset().local_minus_utc());

assert_eq!(now, from_local);
assert_eq!(now, from_utc);
}

#[test]
fn verify_correct_offsets_distant_past() {
// let distant_past = Local::now() - Duration::days(365 * 100);
let distant_past = Local::now() - Duration::days(250 * 31);
let from_local = Local.from_local_datetime(&distant_past.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&distant_past.naive_utc());

assert_eq!(distant_past.offset().local_minus_utc(), from_local.offset().local_minus_utc());
assert_eq!(distant_past.offset().local_minus_utc(), from_utc.offset().local_minus_utc());

assert_eq!(distant_past, from_local);
assert_eq!(distant_past, from_utc);
}

#[test]
fn verify_correct_offsets_distant_future() {
let distant_future = Local::now() + Duration::days(250 * 31);
let from_local = Local.from_local_datetime(&distant_future.naive_local()).unwrap();
let from_utc = Local.from_utc_datetime(&distant_future.naive_utc());

assert_eq!(
distant_future.offset().local_minus_utc(),
from_local.offset().local_minus_utc()
);
assert_eq!(distant_future.offset().local_minus_utc(), from_utc.offset().local_minus_utc());

assert_eq!(distant_future, from_local);
assert_eq!(distant_future, from_utc);
}

#[test]
fn test_local_date_sanity_check() {
Expand Down