From c08b49fc3a50b6d05770d9b4267ca26c04d41e65 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Fri, 22 Mar 2024 18:38:16 +0100 Subject: [PATCH] Convert `NaiveDateTime::checked_(add|add)_months` to return `Result` --- src/datetime/mod.rs | 6 +++-- src/naive/datetime/mod.rs | 49 ++++++++++++++------------------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index da94f5037d..ac4b240a3c 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -336,7 +336,8 @@ impl DateTime { // 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() } @@ -373,7 +374,8 @@ impl DateTime { // 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() } diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 56504adb51..df053ebbdf 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -20,7 +20,7 @@ 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, + expect, ok, try_err, try_ok_or, DateTime, Datelike, Error, FixedOffset, MappedLocalTime, Months, TimeDelta, TimeZone, Timelike, Weekday, }; @@ -320,7 +320,7 @@ 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 /// @@ -328,26 +328,19 @@ impl NaiveDateTime { /// 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 { - 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 { + Ok(Self { date: try_err!(self.date.checked_add_months(rhs)), time: self.time }) } /// Adds given `FixedOffset` to the current datetime. @@ -503,7 +496,7 @@ 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 /// @@ -511,26 +504,18 @@ impl NaiveDateTime { /// 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 { - 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 { + 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`