Skip to content

Commit 45d22e8

Browse files
committed
Convert NaiveDateTime::with_day* to return Result
1 parent ad34c82 commit 45d22e8

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
@@ -808,25 +808,24 @@ impl NaiveDateTime {
808808
///
809809
/// # Errors
810810
///
811-
/// Returns `None` if:
812-
/// - The resulting date does not exist (for example `day(31)` in April).
813-
/// - The value for `day` is invalid.
811+
/// This method returns:
812+
/// - [`Error::DoesNotExist`] if the resulting date does not exist (for example `day(31)` in
813+
/// April).
814+
/// - [`Error::InvalidArgument`] if the value for `day` is invalid.
814815
///
815816
/// # Example
816817
///
817818
/// ```
818-
/// use chrono::{NaiveDate, NaiveDateTime};
819+
/// use chrono::{Error, NaiveDate, NaiveDateTime};
819820
///
820-
/// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).unwrap().and_hms(12, 34, 56).unwrap();
821-
/// assert_eq!(
822-
/// dt.with_day(30),
823-
/// Some(NaiveDate::from_ymd(2015, 9, 30).unwrap().and_hms(12, 34, 56).unwrap())
824-
/// );
825-
/// assert_eq!(dt.with_day(31), None); // no September 31
821+
/// let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8)?.and_hms(12, 34, 56)?;
822+
/// assert_eq!(dt.with_day(30), NaiveDate::from_ymd(2015, 9, 30)?.and_hms(12, 34, 56));
823+
/// assert_eq!(dt.with_day(31), Err(Error::DoesNotExist)); // No September 31
824+
/// # Ok::<(), Error>(())
826825
/// ```
827826
#[inline]
828-
pub const fn with_day(&self, day: u32) -> Option<NaiveDateTime> {
829-
Some(NaiveDateTime { date: try_opt!(ok!(self.date.with_day(day))), ..*self })
827+
pub const fn with_day(&self, day: u32) -> Result<NaiveDateTime, Error> {
828+
Ok(NaiveDateTime { date: try_err!(self.date.with_day(day)), ..*self })
830829
}
831830

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

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

0 commit comments

Comments
 (0)