From ffee5079c9fc969a460e8937893a3f62d98634f6 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 26 Sep 2023 12:48:36 +0200 Subject: [PATCH] other tests --- src/datetime/tests.rs | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 9e41a51096..aad0bd72e2 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -1437,6 +1437,65 @@ fn test_min_max_setters() { assert_eq!(beyond_max.with_nanosecond(beyond_max.nanosecond()), Some(beyond_max)); } +#[test] +fn test_min_max_complex() { + let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN); + let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap(); + let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX); + let max_time = NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap(); + + assert_eq!(beyond_min.checked_add_days(Days::new(0)), Some(beyond_min)); + assert_eq!( + beyond_min.checked_add_days(Days::new(1)), + Some(offset_min.from_utc_datetime(&(NaiveDate::MIN + Days(1)).and_time(NaiveTime::MIN))) + ); + assert_eq!(beyond_min.checked_sub_days(Days::new(0)), Some(beyond_min)); + assert_eq!(beyond_min.checked_sub_days(Days::new(1)), None); + assert_eq!(beyond_min.checked_add_months(Months::new(0)), Some(beyond_min)); + assert_eq!( + beyond_min.checked_add_months(Months::new(1)), + Some(offset_min.from_utc_datetime(&(NaiveDate::MIN + Months(1)).and_time(NaiveTime::MIN))) + ); + assert_eq!(beyond_min.checked_sub_months(Months::new(0)), Some(beyond_min)); + assert_eq!(beyond_min.checked_sub_months(Months::new(1)), None); + assert_eq!(beyond_min.with_year(beyond_min.year()), Some(beyond_min)); + let res = NaiveDate::MIN.with_year(2021).unwrap().and_time(NaiveTime::MIN) + offset_min; + assert_eq!(beyond_min.with_year(2020), offset_min.from_local_datetime(&res).single()); + assert_eq!( + offset_min + .from_utc_datetime( + &NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().and_time(NaiveTime::MIN) + ) + .with_year(NaiveDate::MIN.year() - 1), + Some(beyond_min) + ); + + assert_eq!(beyond_max.checked_add_days(Days::new(0)), Some(beyond_max)); + assert_eq!(beyond_max.checked_add_days(Days::new(1)), None); + assert_eq!(beyond_max.checked_sub_days(Days::new(0)), Some(beyond_max)); + assert_eq!( + beyond_max.checked_sub_days(Days::new(1)), + Some(offset_max.from_utc_datetime(&(NaiveDate::MAX - Days(1)).and_time(max_time))) + ); + assert_eq!(beyond_max.checked_add_months(Months::new(0)), Some(beyond_max)); + assert_eq!(beyond_max.checked_add_months(Months::new(1)), None); + assert_eq!(beyond_max.checked_sub_months(Months::new(0)), Some(beyond_max)); + assert_eq!( + beyond_max.checked_sub_months(Months::new(1)), + Some(offset_max.from_utc_datetime(&(NaiveDate::MAX - Months(1)).and_time(max_time))) + ); + assert_eq!(beyond_max.with_year(beyond_max.year()), Some(beyond_max)); + let res = NaiveDate::MAX.with_year(2019).unwrap().and_time(max_time) + offset_max; + assert_eq!(beyond_max.with_year(2020), offset_max.from_local_datetime(&res).single()); + assert_eq!( + offset_max + .from_utc_datetime(&NaiveDate::from_ymd_opt(2023, 12, 31).unwrap().and_time(max_time)) + .with_year(NaiveDate::MAX.year() + 1), + Some(beyond_max) + ); +} + #[test] #[should_panic] fn test_local_beyond_min_datetime() {