Skip to content

Commit

Permalink
other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 27, 2023
1 parent b3f843e commit ffee507
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ffee507

Please sign in to comment.