diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 633913b8d4..5796e96c58 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -1530,6 +1530,61 @@ where } } +#[test] +fn test_add_sub_months() { + let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap(); + assert_eq!(utc_dt + Months::new(15), Utc.with_ymd_and_hms(2019, 12, 5, 23, 58, 0).unwrap()); + + let utc_dt = Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap(); + assert_eq!(utc_dt + Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap()); + assert_eq!(utc_dt + Months::new(2), Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap()); + + let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap(); + assert_eq!(utc_dt - Months::new(15), Utc.with_ymd_and_hms(2017, 6, 5, 23, 58, 0).unwrap()); + + let utc_dt = Utc.with_ymd_and_hms(2020, 3, 31, 23, 58, 0).unwrap(); + assert_eq!(utc_dt - Months::new(1), Utc.with_ymd_and_hms(2020, 2, 29, 23, 58, 0).unwrap()); + assert_eq!(utc_dt - Months::new(2), Utc.with_ymd_and_hms(2020, 1, 31, 23, 58, 0).unwrap()); +} + +#[test] +fn test_auto_conversion_fixedoffset_into_utc() { + let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap(); + let cdt_dt = FixedOffset::west_opt(5 * 60 * 60) + .unwrap() + .with_ymd_and_hms(2018, 9, 5, 18, 58, 0) + .unwrap(); + let utc_dt2: DateTime = cdt_dt.into(); + assert_eq!(utc_dt, utc_dt2); +} + +#[test] +fn test_auto_conversion_utc_into_fixedoffset() { + let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap(); + let cdt_dt = FixedOffset::west_opt(5 * 60 * 60) + .unwrap() + .with_ymd_and_hms(2018, 9, 5, 18, 58, 0) + .unwrap(); + let cdt_dt2: DateTime = utc_dt.into(); + assert_eq!(cdt_dt, cdt_dt2); +} + +#[cfg(feature = "clock")] +#[test] +fn test_auto_conversion_local_into_utc() { + let loc_dt = Local.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).unwrap(); + let utc_dt: DateTime = loc_dt.into(); + assert_eq!(utc_dt, loc_dt); +} + +#[cfg(feature = "clock")] +#[test] +fn test_auto_conversion_utc_into_local() { + let utc_dt = Utc.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).unwrap(); + let loc_dt: DateTime = utc_dt.into(); + assert_eq!(utc_dt, loc_dt); +} + #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_encodable_json(to_string_utc: FUtc, to_string_fixed: FFixed) where