From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/chrono/tests/dateutils.rs | 131 +++++++++++++++++++++++++++++++++++++++ vendor/chrono/tests/wasm.rs | 128 +++++++++++++++++++++----------------- 2 files changed, 202 insertions(+), 57 deletions(-) create mode 100644 vendor/chrono/tests/dateutils.rs (limited to 'vendor/chrono/tests') diff --git a/vendor/chrono/tests/dateutils.rs b/vendor/chrono/tests/dateutils.rs new file mode 100644 index 000000000..dec6bfe11 --- /dev/null +++ b/vendor/chrono/tests/dateutils.rs @@ -0,0 +1,131 @@ +use chrono::offset::TimeZone; +use chrono::Local; +use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike}; + +use std::{path, process}; + +#[cfg(unix)] +fn verify_against_date_command_local(path: &'static str, dt: NaiveDateTime) { + let output = process::Command::new(path) + .arg("-d") + .arg(format!("{}-{:02}-{:02} {:02}:05:01", dt.year(), dt.month(), dt.day(), dt.hour())) + .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 + // .with_ymd_and_hms(year, month, day, 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); + // } + + // This is used while a decision is made wheter the `date` output needs to + // be exactly matched, or whether LocalResult::Ambigious should be handled + // differently + + let date = NaiveDate::from_ymd_opt(dt.year(), dt.month(), dt.day()).unwrap(); + match Local.from_local_datetime(&date.and_hms_opt(dt.hour(), 5, 1).unwrap()) { + chrono::LocalResult::Ambiguous(a, b) => assert!( + format!("{}\n", a) == date_command_str || format!("{}\n", b) == date_command_str + ), + chrono::LocalResult::Single(a) => { + assert_eq!(format!("{}\n", a), date_command_str); + } + chrono::LocalResult::None => { + assert_eq!("", date_command_str); + } + } +} + +#[test] +#[cfg(unix)] +fn try_verify_against_date_command() { + let date_path = "/usr/bin/date"; + + if !path::Path::new(date_path).exists() { + // date command not found, skipping + // avoid running this on macOS, which has path /bin/date + // as the required CLI arguments are not present in the + // macOS build. + return; + } + + let mut date = NaiveDate::from_ymd_opt(1975, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(); + + while date.year() < 2078 { + if (1975..=1977).contains(&date.year()) + || (2020..=2022).contains(&date.year()) + || (2073..=2077).contains(&date.year()) + { + verify_against_date_command_local(date_path, date); + } + + date += chrono::Duration::hours(1); + } +} + +#[cfg(target_os = "linux")] +fn verify_against_date_command_format_local(path: &'static str, dt: NaiveDateTime) { + let required_format = + "d%d D%D F%F H%H I%I j%j k%k l%l m%m M%M S%S T%T u%u U%U w%w W%W X%X y%y Y%Y z%:z"; + // a%a - depends from localization + // A%A - depends from localization + // b%b - depends from localization + // B%B - depends from localization + // h%h - depends from localization + // c%c - depends from localization + // p%p - depends from localization + // r%r - depends from localization + // x%x - fails, date is dd/mm/yyyy, chrono is dd/mm/yy, same as %D + // Z%Z - too many ways to represent it, will most likely fail + + let output = process::Command::new(path) + .arg("-d") + .arg(format!( + "{}-{:02}-{:02} {:02}:{:02}:{:02}", + dt.year(), + dt.month(), + dt.day(), + dt.hour(), + dt.minute(), + dt.second() + )) + .arg(format!("+{}", required_format)) + .output() + .unwrap(); + + let date_command_str = String::from_utf8(output.stdout).unwrap(); + let date = NaiveDate::from_ymd_opt(dt.year(), dt.month(), dt.day()).unwrap(); + let ldt = Local + .from_local_datetime(&date.and_hms_opt(dt.hour(), dt.minute(), dt.second()).unwrap()) + .unwrap(); + let formated_date = format!("{}\n", ldt.format(required_format)); + assert_eq!(date_command_str, formated_date); +} + +#[test] +#[cfg(target_os = "linux")] +fn try_verify_against_date_command_format() { + let date_path = "/usr/bin/date"; + + if !path::Path::new(date_path).exists() { + // date command not found, skipping + return; + } + let mut date = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_opt(12, 11, 13).unwrap(); + while date.year() < 2008 { + verify_against_date_command_format_local(date_path, date); + date += chrono::Duration::days(55); + } +} diff --git a/vendor/chrono/tests/wasm.rs b/vendor/chrono/tests/wasm.rs index 275d120d3..f003d4db9 100644 --- a/vendor/chrono/tests/wasm.rs +++ b/vendor/chrono/tests/wasm.rs @@ -1,67 +1,81 @@ -#[cfg(all(test, feature = "wasmbind"))] -mod test { - extern crate chrono; - extern crate wasm_bindgen_test; +#![cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) +))] - use self::chrono::prelude::*; - use self::wasm_bindgen_test::*; +use self::chrono::prelude::*; +use self::wasm_bindgen_test::*; - #[wasm_bindgen_test] - fn now() { - let utc: DateTime = Utc::now(); - let local: DateTime = Local::now(); +#[wasm_bindgen_test] +fn now() { + let utc: DateTime = Utc::now(); + let local: DateTime = Local::now(); - // Ensure time set by the test script is correct - let now = env!("NOW"); - let actual = Utc.datetime_from_str(&now, "%s").unwrap(); - let diff = utc - actual; - assert!( - diff < chrono::Duration::minutes(5), - "expected {} - {} == {} < 5m (env var: {})", - utc, - actual, - diff, - now, - ); + // Ensure time set by the test script is correct + let now = env!("NOW"); + let actual = Utc.datetime_from_str(&now, "%s").unwrap(); + let diff = utc - actual; + assert!( + diff < chrono::Duration::minutes(5), + "expected {} - {} == {} < 5m (env var: {})", + utc, + actual, + diff, + now, + ); - let tz = env!("TZ"); - eprintln!("testing with tz={}", tz); + let tz = env!("TZ"); + eprintln!("testing with tz={}", tz); - // Ensure offset retrieved when getting local time is correct - let expected_offset = match tz { - "ACST-9:30" => FixedOffset::east(19 * 30 * 60), - "Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully - "EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60), - "EST" | "-0500" => FixedOffset::east(-5 * 60 * 60), - "UTC0" | "+0000" => FixedOffset::east(0), - tz => panic!("unexpected TZ {}", tz), - }; - assert_eq!( - &expected_offset, - local.offset(), - "expected: {:?} local: {:?}", - expected_offset, - local.offset(), - ); - } + // Ensure offset retrieved when getting local time is correct + let expected_offset = match tz { + "ACST-9:30" => FixedOffset::east_opt(19 * 30 * 60).unwrap(), + "Asia/Katmandu" => FixedOffset::east_opt(23 * 15 * 60).unwrap(), // No DST thankfully + "EDT" | "EST4" | "-0400" => FixedOffset::east_opt(-4 * 60 * 60).unwrap(), + "EST" | "-0500" => FixedOffset::east_opt(-5 * 60 * 60).unwrap(), + "UTC0" | "+0000" => FixedOffset::east_opt(0).unwrap(), + tz => panic!("unexpected TZ {}", tz), + }; + assert_eq!( + &expected_offset, + local.offset(), + "expected: {:?} local: {:?}", + expected_offset, + local.offset(), + ); +} + +#[wasm_bindgen_test] +fn from_is_exact() { + let now = js_sys::Date::new_0(); - #[wasm_bindgen_test] - fn from_is_exact() { - let now = js_sys::Date::new_0(); + let dt = DateTime::::from(now.clone()); - let dt = DateTime::::from(now.clone()); + assert_eq!(now.get_time() as i64, dt.timestamp_millis_opt().unwrap()); +} + +#[wasm_bindgen_test] +fn local_from_local_datetime() { + let now = Local::now(); + let ndt = now.naive_local(); + let res = match Local.from_local_datetime(&ndt).single() { + Some(v) => v, + None => panic! {"Required for test!"}, + }; + assert_eq!(now, res); +} - assert_eq!(now.get_time() as i64, dt.timestamp_millis()); - } +#[wasm_bindgen_test] +fn convert_all_parts_with_milliseconds() { + let time: DateTime = "2020-12-01T03:01:55.974Z".parse().unwrap(); + let js_date = js_sys::Date::from(time); - #[wasm_bindgen_test] - fn local_from_local_datetime() { - let now = Local::now(); - let ndt = now.naive_local(); - let res = match Local.from_local_datetime(&ndt).single() { - Some(v) => v, - None => panic! {"Required for test!"}, - }; - assert_eq!(now, res); - } + assert_eq!(js_date.get_utc_full_year(), 2020); + assert_eq!(js_date.get_utc_month(), 12); + assert_eq!(js_date.get_utc_date(), 1); + assert_eq!(js_date.get_utc_hours(), 3); + assert_eq!(js_date.get_utc_minutes(), 1); + assert_eq!(js_date.get_utc_seconds(), 55); + assert_eq!(js_date.get_utc_milliseconds(), 974); } -- cgit v1.2.3