Skip to content

Commit

Permalink
Uphold guarantee that the resulting NaiveDateTime is always valid
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed May 19, 2023
1 parent e9ce5db commit 0601202
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ impl NaiveDate {
/// ```
#[must_use]
pub fn checked_add_months(self, months: Months) -> Option<Self> {
if months.0 == 0 {
// To be useable by `DateTime` this method should guarantee the result is valid, even if
// the input `self` is out of range. So only short-circuit if in range.
if months.0 == 0 && self >= Self::MIN && self <= Self::MAX {
return Some(self);
}

Expand Down Expand Up @@ -636,7 +638,9 @@ impl NaiveDate {
/// ```
#[must_use]
pub fn checked_sub_months(self, months: Months) -> Option<Self> {
if months.0 == 0 {
// To be useable by `DateTime` this method should guarantee the result is valid, even if
// the input `self` is out of range. So only short-circuit if in range.
if months.0 == 0 && self >= Self::MIN && self <= Self::MAX {
return Some(self);
}

Expand Down

0 comments on commit 0601202

Please sign in to comment.