Skip to content

Commit de26fe3

Browse files
committed
Convert NaiveDateTime::with_day* to return Result
1 parent a2aec45 commit de26fe3

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/datetime/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
621621
/// daylight saving time transition.
622622
#[inline]
623623
pub fn with_day(&self, day: u32) -> Option<DateTime<Tz>> {
624-
map_local(self, |datetime| datetime.with_day(day))
624+
map_local(self, |datetime| datetime.with_day(day).ok())
625625
}
626626

627627
/// Makes a new `DateTime` with the day of month (starting from 0) changed.
@@ -637,7 +637,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
637637
/// daylight saving time transition.
638638
#[inline]
639639
pub fn with_day0(&self, day0: u32) -> Option<DateTime<Tz>> {
640-
map_local(self, |datetime| datetime.with_day0(day0))
640+
map_local(self, |datetime| datetime.with_day0(day0).ok())
641641
}
642642

643643
/// Makes a new `DateTime` with the day of year (starting from 1) changed.

src/naive/datetime/mod.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -807,25 +807,24 @@ impl NaiveDateTime {
807807
///
808808
/// # Errors
809809
///
810-
/// Returns `None` if:
811-
/// - The resulting date does not exist (for example `day(31)` in April).
812-
/// - The value for `day` is invalid.
810+
/// This method returns:
811+
/// - [`Error::DoesNotExist`] if the resulting date does not exist (for example `day(31)` in
812+
/// April).
813+
/// - [`Error::InvalidArgument`] if the value for `day` is invalid.
813814
///
814815
/// # Example
815816
///
816817
/// ```
817-
/// use chrono::{NaiveDate, NaiveDateTime};
818+
/// use chrono::{Error, NaiveDate, NaiveDateTime};
818819
///
819-
/// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).unwrap().and_hms(12, 34, 56).unwrap();
820-
/// assert_eq!(
821-
/// dt.with_day(30),
822-
/// Some(NaiveDate::from_ymd(2015, 9, 30).unwrap().and_hms(12, 34, 56).unwrap())
823-
/// );
824-
/// assert_eq!(dt.with_day(31), None); // no September 31
820+
/// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8)?.and_hms(12, 34, 56)?;
821+
/// assert_eq!(dt.with_day(30), NaiveDate::from_ymd(2015, 9, 30)?.and_hms(12, 34, 56));
822+
/// assert_eq!(dt.with_day(31), Err(Error::DoesNotExist)); // No September 31
823+
/// # Ok::<(), Error>(())
825824
/// ```
826825
#[inline]
827-
pub const fn with_day(&self, day: u32) -> Option<NaiveDateTime> {
828-
Some(NaiveDateTime { date: try_opt!(ok!(self.date.with_day(day))), ..*self })
826+
pub const fn with_day(&self, day: u32) -> Result<NaiveDateTime, Error> {
827+
Ok(NaiveDateTime { date: try_err!(self.date.with_day(day)), ..*self })
829828
}
830829

831830
/// Makes a new `NaiveDateTime` with the day of month (starting from 0) changed.
@@ -834,25 +833,24 @@ impl NaiveDateTime {
834833
///
835834
/// # Errors
836835
///
837-
/// Returns `None` if:
838-
/// - The resulting date does not exist (for example `day(30)` in April).
839-
/// - The value for `day0` is invalid.
836+
/// This method returns:
837+
/// - [`Error::DoesNotExist`] if the resulting date does not exist (for example `day0(30)` in
838+
/// April).
839+
/// - [`Error::InvalidArgument`] if the value for `day0` is invalid.
840840
///
841841
/// # Example
842842
///
843843
/// ```
844-
/// use chrono::{NaiveDate, NaiveDateTime};
844+
/// use chrono::{Error, NaiveDate, NaiveDateTime};
845845
///
846-
/// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).unwrap().and_hms(12, 34, 56).unwrap();
847-
/// assert_eq!(
848-
/// dt.with_day0(29),
849-
/// Some(NaiveDate::from_ymd(2015, 9, 30).unwrap().and_hms(12, 34, 56).unwrap())
850-
/// );
851-
/// assert_eq!(dt.with_day0(30), None); // no September 31
846+
/// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8)?.and_hms(12, 34, 56)?;
847+
/// assert_eq!(dt.with_day0(29), NaiveDate::from_ymd(2015, 9, 30)?.and_hms(12, 34, 56));
848+
/// assert_eq!(dt.with_day0(30), Err(Error::DoesNotExist)); // No September 31
849+
/// # Ok::<(), Error>(())
852850
/// ```
853851
#[inline]
854-
pub const fn with_day0(&self, day0: u32) -> Option<NaiveDateTime> {
855-
Some(NaiveDateTime { date: try_opt!(ok!(self.date.with_day0(day0))), ..*self })
852+
pub const fn with_day0(&self, day0: u32) -> Result<NaiveDateTime, Error> {
853+
Ok(NaiveDateTime { date: try_err!(self.date.with_day0(day0)), ..*self })
856854
}
857855

858856
/// Makes a new `NaiveDateTime` with the day of year (starting from 1) changed.

0 commit comments

Comments
 (0)