Skip to content

Commit

Permalink
Convert NaiveDateTime::checked_(add|add)_months to return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 22, 2024
1 parent 45cad27 commit 7b5811d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
// the resulting date, with which we can return `Some` even for an out of range local
// datetime.
self.overflowing_naive_local()
.checked_add_months(months)?
.checked_add_months(months)
.ok()?
.in_timezone(self.timezone())
.single()
}
Expand Down Expand Up @@ -373,7 +374,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
// the resulting date, with which we can return `Some` even for an out of range local
// datetime.
self.overflowing_naive_local()
.checked_sub_months(months)?
.checked_sub_months(months)
.ok()?
.in_timezone(self.timezone())
.single()
}
Expand Down
51 changes: 18 additions & 33 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::format::{Fixed, Item, Numeric, Pad};
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime};
use crate::offset::Utc;
use crate::{
expect, ok, try_err, try_ok_or, try_opt, DateTime, Datelike, Error, FixedOffset,
MappedLocalTime, Months, TimeDelta, TimeZone, Timelike, Weekday,
expect, ok, try_err, try_ok_or, DateTime, Datelike, Error, FixedOffset, MappedLocalTime,
Months, TimeDelta, TimeZone, Timelike, Weekday,
};

/// Tools to help serializing/deserializing `NaiveDateTime`s
Expand Down Expand Up @@ -320,34 +320,27 @@ impl NaiveDateTime {
///
/// # Errors
///
/// Returns `None` if the resulting date would be out of range.
/// Returns [`Error::OutOfRange`] if the resulting date would be out of range.
///
/// # Example
///
/// ```
/// use chrono::{Months, NaiveDate};
///
/// assert_eq!(
/// NaiveDate::from_ymd(2014, 1, 1)
/// .unwrap()
/// .at_hms(1, 0, 0)
/// .unwrap()
/// .checked_add_months(Months::new(1)),
/// Some(NaiveDate::from_ymd(2014, 2, 1).unwrap().at_hms(1, 0, 0).unwrap())
/// NaiveDate::from_ymd(2014, 1, 1)?.at_hms(1, 0, 0)?.checked_add_months(Months::new(1)),
/// NaiveDate::from_ymd(2014, 2, 1)?.at_hms(1, 0, 0)
/// );
///
/// assert_eq!(
/// NaiveDate::from_ymd(2014, 1, 1)
/// .unwrap()
/// .at_hms(1, 0, 0)
/// .unwrap()
/// .checked_add_months(Months::new(core::i32::MAX as u32 + 1)),
/// None
/// NaiveDate::from_ymd(2022, 7, 31)?.at_hms(1, 0, 0)?.checked_add_months(Months::new(2)),
/// NaiveDate::from_ymd(2022, 9, 30)?.at_hms(1, 0, 0)
/// );
/// # Ok::<(), chrono::Error>(())
/// ```
#[must_use]
pub const fn checked_add_months(self, rhs: Months) -> Option<NaiveDateTime> {
Some(Self { date: try_opt!(ok!(self.date.checked_add_months(rhs))), time: self.time })
pub const fn checked_add_months(self, rhs: Months) -> Result<NaiveDateTime, Error> {
Ok(Self { date: try_err!(self.date.checked_add_months(rhs)), time: self.time })
}

/// Adds given `FixedOffset` to the current datetime.
Expand Down Expand Up @@ -503,34 +496,26 @@ impl NaiveDateTime {
///
/// # Errors
///
/// Returns `None` if the resulting date would be out of range.
/// Returns [`Error::OutOfRange`] if the resulting date would be out of range.
///
/// # Example
///
/// ```
/// use chrono::{Months, NaiveDate};
///
/// assert_eq!(
/// NaiveDate::from_ymd(2014, 1, 1)
/// .unwrap()
/// .at_hms(1, 0, 0)
/// .unwrap()
/// .checked_sub_months(Months::new(1)),
/// Some(NaiveDate::from_ymd(2013, 12, 1).unwrap().at_hms(1, 0, 0).unwrap())
/// NaiveDate::from_ymd(2014, 1, 1)?.at_hms(1, 0, 0)?.checked_sub_months(Months::new(1)),
/// NaiveDate::from_ymd(2013, 12, 1)?.at_hms(1, 0, 0)
/// );
///
/// assert_eq!(
/// NaiveDate::from_ymd(2014, 1, 1)
/// .unwrap()
/// .at_hms(1, 0, 0)
/// .unwrap()
/// .checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
/// None
/// NaiveDate::from_ymd(2022, 7, 31)?.at_hms(1, 0, 0)?.checked_sub_months(Months::new(3)),
/// NaiveDate::from_ymd(2022, 4, 30)?.at_hms(1, 0, 0)
/// );
/// # Ok::<(), chrono::Error>(())
/// ```
#[must_use]
pub const fn checked_sub_months(self, rhs: Months) -> Option<NaiveDateTime> {
Some(Self { date: try_opt!(ok!(self.date.checked_sub_months(rhs))), time: self.time })
pub const fn checked_sub_months(self, rhs: Months) -> Result<NaiveDateTime, Error> {
Ok(Self { date: try_err!(self.date.checked_sub_months(rhs)), time: self.time })
}

/// Add a duration in [`Days`] to the date part of the `NaiveDateTime`
Expand Down

0 comments on commit 7b5811d

Please sign in to comment.