@@ -646,6 +646,10 @@ impl NaiveDate {
646
646
/// NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_days(Days::new(2)),
647
647
/// Some(NaiveDate::from_ymd_opt(2022, 8, 2).unwrap())
648
648
/// );
649
+ /// assert_eq!(
650
+ /// NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_days(Days::new(1000000000000)),
651
+ /// None
652
+ /// );
649
653
/// ```
650
654
pub fn checked_add_days ( self , days : Days ) -> Option < Self > {
651
655
if days. 0 == 0 {
@@ -665,6 +669,10 @@ impl NaiveDate {
665
669
/// NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_days(Days::new(6)),
666
670
/// Some(NaiveDate::from_ymd_opt(2022, 2, 14).unwrap())
667
671
/// );
672
+ /// assert_eq!(
673
+ /// NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_days(Days::new(1000000000000)),
674
+ /// None
675
+ /// );
668
676
/// ```
669
677
pub fn checked_sub_days ( self , days : Days ) -> Option < Self > {
670
678
if days. 0 == 0 {
@@ -675,7 +683,11 @@ impl NaiveDate {
675
683
}
676
684
677
685
fn diff_days ( self , days : i64 ) -> Option < Self > {
678
- self . checked_add_signed ( Duration :: days ( days) )
686
+ let secs = days. checked_mul ( 86400 ) ?; // 86400 seconds in one day
687
+ if secs >= std:: i64:: MAX / 1000 || secs <= std:: i64:: MIN / 1000 {
688
+ return None ; // See the `time` 0.1 crate. Outside these bounds, `Duration::seconds` will panic
689
+ }
690
+ self . checked_add_signed ( Duration :: seconds ( secs) )
679
691
}
680
692
681
693
/// Makes a new `NaiveDateTime` from the current date and given `NaiveTime`.
0 commit comments