Skip to content

Commit

Permalink
Fix panic in from_num_days_from_ce_opt
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker authored and djc committed May 8, 2023
1 parent a20a9b7 commit f659719
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/naive/date.rs
Expand Up @@ -448,7 +448,7 @@ impl NaiveDate {
/// ```
#[must_use]
pub fn from_num_days_from_ce_opt(days: i32) -> Option<NaiveDate> {
let days = days + 365; // make December 31, 1 BCE equal to day 0
let days = days.checked_add(365)?; // make December 31, 1 BCE equal to day 0
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
Expand Down Expand Up @@ -2500,6 +2500,9 @@ mod tests {
assert_eq!(from_ndays_from_ce(NaiveDate::MIN.num_days_from_ce() - 1), None);
assert_eq!(from_ndays_from_ce(NaiveDate::MAX.num_days_from_ce()), Some(NaiveDate::MAX));
assert_eq!(from_ndays_from_ce(NaiveDate::MAX.num_days_from_ce() + 1), None);

assert_eq!(from_ndays_from_ce(i32::MIN), None);
assert_eq!(from_ndays_from_ce(i32::MAX), None);
}

#[test]
Expand Down

0 comments on commit f659719

Please sign in to comment.