Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert checked_(add|sub)_signed to return Result #1514

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[inline]
#[must_use]
pub fn checked_add_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
let datetime = self.datetime.checked_add_signed(rhs)?;
let datetime = self.datetime.checked_add_signed(rhs).ok()?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
}
Expand Down Expand Up @@ -349,7 +349,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[inline]
#[must_use]
pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
let datetime = self.datetime.checked_sub_signed(rhs)?;
let datetime = self.datetime.checked_sub_signed(rhs).ok()?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
}
Expand Down
57 changes: 22 additions & 35 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,66 +869,53 @@ impl NaiveDate {
///
/// # 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::{NaiveDate, TimeDelta};
/// use chrono::{Error, NaiveDate, TimeDelta};
///
/// let d = NaiveDate::from_ymd(2015, 9, 5).unwrap();
/// assert_eq!(
/// d.checked_add_signed(TimeDelta::days(40)),
/// Some(NaiveDate::from_ymd(2015, 10, 15).unwrap())
/// );
/// assert_eq!(
/// d.checked_add_signed(TimeDelta::days(-40)),
/// Some(NaiveDate::from_ymd(2015, 7, 27).unwrap())
/// );
/// assert_eq!(d.checked_add_signed(TimeDelta::days(1_000_000_000)), None);
/// assert_eq!(d.checked_add_signed(TimeDelta::days(-1_000_000_000)), None);
/// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::days(1)), None);
/// let d = NaiveDate::from_ymd(2015, 9, 5)?;
/// assert_eq!(d.checked_add_signed(TimeDelta::days(40)), NaiveDate::from_ymd(2015, 10, 15));
/// assert_eq!(d.checked_add_signed(TimeDelta::days(-40)), NaiveDate::from_ymd(2015, 7, 27));
/// assert_eq!(d.checked_add_signed(TimeDelta::days(1_000_000_000)), Err(Error::OutOfRange));
/// assert_eq!(d.checked_add_signed(TimeDelta::days(-1_000_000_000)), Err(Error::OutOfRange));
/// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::days(1)), Err(Error::OutOfRange));
/// # Ok::<(), Error>(())
/// ```
#[must_use]
pub const fn checked_add_signed(self, rhs: TimeDelta) -> Option<NaiveDate> {
pub const fn checked_add_signed(self, rhs: TimeDelta) -> Result<NaiveDate, Error> {
let days = rhs.num_days();
if days < i32::MIN as i64 || days > i32::MAX as i64 {
return None;
return Err(Error::OutOfRange);
}
ok!(self.add_days(days as i32))
self.add_days(days as i32)
}

/// Subtracts the number of whole days in the given `TimeDelta` from the current date.
///
/// # 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::{NaiveDate, TimeDelta};
/// use chrono::{Error, NaiveDate, TimeDelta};
///
/// let d = NaiveDate::from_ymd(2015, 9, 5).unwrap();
/// assert_eq!(
/// d.checked_sub_signed(TimeDelta::days(40)),
/// Some(NaiveDate::from_ymd(2015, 7, 27).unwrap())
/// );
/// assert_eq!(
/// d.checked_sub_signed(TimeDelta::days(-40)),
/// Some(NaiveDate::from_ymd(2015, 10, 15).unwrap())
/// );
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(1_000_000_000)), None);
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(-1_000_000_000)), None);
/// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::days(1)), None);
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(40)), NaiveDate::from_ymd(2015, 7, 27));
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(-40)), NaiveDate::from_ymd(2015, 10, 15));
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(1_000_000_000)), Err(Error::OutOfRange));
/// assert_eq!(d.checked_sub_signed(TimeDelta::days(-1_000_000_000)), Err(Error::OutOfRange));
/// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::days(1)), Err(Error::OutOfRange));
/// ```
#[must_use]
pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Option<NaiveDate> {
pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Result<NaiveDate, Error> {
let days = -rhs.num_days();
if days < i32::MIN as i64 || days > i32::MAX as i64 {
return None;
return Err(Error::OutOfRange);
}
ok!(self.add_days(days as i32))
self.add_days(days as i32)
}

/// Subtracts another `NaiveDate` from the current date.
Expand Down
12 changes: 6 additions & 6 deletions src/naive/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ fn test_date_pred() -> Result<(), Error> {

#[test]
fn test_date_checked_add_signed() {
fn check(lhs: Option<NaiveDate>, delta: TimeDelta, rhs: Option<NaiveDate>) {
fn check(lhs: Result<NaiveDate, Error>, delta: TimeDelta, rhs: Result<NaiveDate, Error>) {
assert_eq!(lhs.unwrap().checked_add_signed(delta), rhs);
assert_eq!(lhs.unwrap().checked_sub_signed(-delta), rhs);
}
let ymd = |y, m, d| NaiveDate::from_ymd(y, m, d).ok();
let ymd = NaiveDate::from_ymd;

check(ymd(2014, 1, 1), TimeDelta::zero(), ymd(2014, 1, 1));
check(ymd(2014, 1, 1), TimeDelta::seconds(86399), ymd(2014, 1, 1));
Expand All @@ -467,11 +467,11 @@ fn test_date_checked_add_signed() {

// overflow check
check(ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0), ymd(MAX_YEAR, 12, 31));
check(ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 + 1), None);
check(ymd(0, 1, 1), TimeDelta::max_value(), None);
check(ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 + 1), Err(Error::OutOfRange));
check(ymd(0, 1, 1), TimeDelta::max_value(), Err(Error::OutOfRange));
check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0), ymd(MIN_YEAR, 1, 1));
check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 - 1), None);
check(ymd(0, 1, 1), TimeDelta::min_value(), None);
check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 - 1), Err(Error::OutOfRange));
check(ymd(0, 1, 1), TimeDelta::min_value(), Err(Error::OutOfRange));
}

#[test]
Expand Down
Loading
Loading