on master help sign in
Used checked_add for years and months
commit584a31cd9047ce8f93b9eab9233c1b62a9278054 authorJason Rodney Hansen <jasonrodneyhansen@gmail.com> dateMon 29 Nov 2021, 18:19 (5 years ago) parentc74cd48
1 file changed+2−2
helix-core/src/increment/date_time.rs+2−2
helix-core/src/increment/date_time.rs+2−2
@@ -340,7 +340,7 @@ fn ndays_in_month(year: i32, month: u32) -> u32 {
340340}
341341
342342fn add_months(date_time: NaiveDateTime, amount: i64) -> Option<NaiveDateTime> {
343− let month = date_time.month0() as i64 + amount;
343+ let month = (date_time.month0() as i64).checked_add(amount)?;
344344 let year = date_time.year() + i32::try_from(month / 12).ok()?;
345345 let year = if month.is_negative() { year - 1 } else { year };
346346
@@ -359,7 +359,7 @@ fn add_months(date_time: NaiveDateTime, amount: i64) -> Option<NaiveDateTime> {
359359}
360360
361361fn add_years(date_time: NaiveDateTime, amount: i64) -> Option<NaiveDateTime> {
362− let year = i32::try_from(date_time.year() as i64 + amount).ok()?;
362+ let year = i32::try_from((date_time.year() as i64).checked_add(amount)?).ok()?;
363363 let ndays = ndays_in_month(year, date_time.month());
364364
365365 if date_time.day() > ndays {