Skip to content

Commit 0601202

Browse files
committed
Uphold guarantee that the resulting NaiveDateTime is always valid
1 parent e9ce5db commit 0601202

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/naive/date.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ impl NaiveDate {
605605
/// ```
606606
#[must_use]
607607
pub fn checked_add_months(self, months: Months) -> Option<Self> {
608-
if months.0 == 0 {
608+
// To be useable by `DateTime` this method should guarantee the result is valid, even if
609+
// the input `self` is out of range. So only short-circuit if in range.
610+
if months.0 == 0 && self >= Self::MIN && self <= Self::MAX {
609611
return Some(self);
610612
}
611613

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

0 commit comments

Comments
 (0)