diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index b25058ffe6..76c544daba 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -1500,8 +1500,8 @@ fn test_datetime_add_assign_local() { // ensure we cross a DST transition for i in 1..=365 { - datetime_add += TimeDelta::days(1); - assert_eq!(datetime_add, datetime + TimeDelta::days(i)) + datetime_add += TimeDelta::try_days(1).unwrap(); + assert_eq!(datetime_add, datetime + TimeDelta::try_days(i).unwrap()) } } @@ -1709,8 +1709,8 @@ fn test_datetime_sub_assign_local() { // ensure we cross a DST transition for i in 1..=365 { - datetime_sub -= TimeDelta::days(1); - assert_eq!(datetime_sub, datetime - TimeDelta::days(i)) + datetime_sub -= TimeDelta::try_days(1).unwrap(); + assert_eq!(datetime_sub, datetime - TimeDelta::try_days(i).unwrap()) } } diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 6961449783..1ca2cd0b80 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -1057,16 +1057,16 @@ impl NaiveDate { /// /// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap(); /// assert_eq!( - /// d.checked_add_signed(TimeDelta::days(40)), + /// d.checked_add_signed(TimeDelta::try_days(40).unwrap()), /// Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap()) /// ); /// assert_eq!( - /// d.checked_add_signed(TimeDelta::days(-40)), + /// d.checked_add_signed(TimeDelta::try_days(-40).unwrap()), /// Some(NaiveDate::from_ymd_opt(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); + /// assert_eq!(d.checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None); + /// assert_eq!(d.checked_add_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None); + /// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::try_days(1).unwrap()), None); /// ``` #[must_use] pub const fn checked_add_signed(self, rhs: TimeDelta) -> Option { @@ -1090,16 +1090,16 @@ impl NaiveDate { /// /// let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap(); /// assert_eq!( - /// d.checked_sub_signed(TimeDelta::days(40)), + /// d.checked_sub_signed(TimeDelta::try_days(40).unwrap()), /// Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap()) /// ); /// assert_eq!( - /// d.checked_sub_signed(TimeDelta::days(-40)), + /// d.checked_sub_signed(TimeDelta::try_days(-40).unwrap()), /// Some(NaiveDate::from_ymd_opt(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::try_days(1_000_000_000).unwrap()), None); + /// assert_eq!(d.checked_sub_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None); + /// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::try_days(1).unwrap()), None); /// ``` #[must_use] pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Option { @@ -1125,12 +1125,27 @@ impl NaiveDate { /// let since = NaiveDate::signed_duration_since; /// /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), TimeDelta::zero()); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)), TimeDelta::days(1)); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)), TimeDelta::days(-1)); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)), TimeDelta::days(100)); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)), TimeDelta::days(365)); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), TimeDelta::days(365 * 4 + 1)); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), TimeDelta::days(365 * 400 + 97)); + /// assert_eq!( + /// since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)), + /// TimeDelta::try_days(1).unwrap() + /// ); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)), TimeDelta::try_days(-1).unwrap()); + /// assert_eq!( + /// since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)), + /// TimeDelta::try_days(100).unwrap() + /// ); + /// assert_eq!( + /// since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)), + /// TimeDelta::try_days(365).unwrap() + /// ); + /// assert_eq!( + /// since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), + /// TimeDelta::try_days(365 * 4 + 1).unwrap() + /// ); + /// assert_eq!( + /// since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), + /// TimeDelta::try_days(365 * 400 + 97).unwrap() + /// ); /// ``` #[must_use] pub const fn signed_duration_since(self, rhs: NaiveDate) -> TimeDelta { @@ -1894,11 +1909,17 @@ impl Datelike for NaiveDate { /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::zero(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(86399), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(-86399), from_ymd(2014, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(1), from_ymd(2014, 1, 2)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1), from_ymd(2013, 12, 31)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364), from_ymd(2014, 12, 31)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 4 + 1), from_ymd(2018, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 400 + 97), from_ymd(2414, 1, 1)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_days(1).unwrap(), from_ymd(2014, 1, 2)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_days(-1).unwrap(), from_ymd(2013, 12, 31)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_days(364).unwrap(), from_ymd(2014, 12, 31)); +/// assert_eq!( +/// from_ymd(2014, 1, 1) + TimeDelta::try_days(365 * 4 + 1).unwrap(), +/// from_ymd(2018, 1, 1) +/// ); +/// assert_eq!( +/// from_ymd(2014, 1, 1) + TimeDelta::try_days(365 * 400 + 97).unwrap(), +/// from_ymd(2414, 1, 1) +/// ); /// ``` /// /// [`NaiveDate::checked_add_signed`]: crate::NaiveDate::checked_add_signed @@ -2037,11 +2058,17 @@ impl Sub for NaiveDate { /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::zero(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(86399), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(-86399), from_ymd(2014, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(1), from_ymd(2013, 12, 31)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1), from_ymd(2014, 1, 2)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364), from_ymd(2013, 1, 2)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 4 + 1), from_ymd(2010, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 400 + 97), from_ymd(1614, 1, 1)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_days(1).unwrap(), from_ymd(2013, 12, 31)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_days(-1).unwrap(), from_ymd(2014, 1, 2)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_days(364).unwrap(), from_ymd(2013, 1, 2)); +/// assert_eq!( +/// from_ymd(2014, 1, 1) - TimeDelta::try_days(365 * 4 + 1).unwrap(), +/// from_ymd(2010, 1, 1) +/// ); +/// assert_eq!( +/// from_ymd(2014, 1, 1) - TimeDelta::try_days(365 * 400 + 97).unwrap(), +/// from_ymd(1614, 1, 1) +/// ); /// ``` /// /// [`NaiveDate::checked_sub_signed`]: crate::NaiveDate::checked_sub_signed @@ -2088,12 +2115,18 @@ impl SubAssign for NaiveDate { /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 1), TimeDelta::zero()); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 12, 31), TimeDelta::days(1)); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 2), TimeDelta::days(-1)); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 9, 23), TimeDelta::days(100)); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 1, 1), TimeDelta::days(365)); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2010, 1, 1), TimeDelta::days(365 * 4 + 1)); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(1614, 1, 1), TimeDelta::days(365 * 400 + 97)); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 12, 31), TimeDelta::try_days(1).unwrap()); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 2), TimeDelta::try_days(-1).unwrap()); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 9, 23), TimeDelta::try_days(100).unwrap()); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 1, 1), TimeDelta::try_days(365).unwrap()); +/// assert_eq!( +/// from_ymd(2014, 1, 1) - from_ymd(2010, 1, 1), +/// TimeDelta::try_days(365 * 4 + 1).unwrap() +/// ); +/// assert_eq!( +/// from_ymd(2014, 1, 1) - from_ymd(1614, 1, 1), +/// TimeDelta::try_days(365 * 400 + 97).unwrap() +/// ); /// ``` impl Sub for NaiveDate { type Output = TimeDelta; diff --git a/src/naive/date/tests.rs b/src/naive/date/tests.rs index 361a88ef59..cde973daf4 100644 --- a/src/naive/date/tests.rs +++ b/src/naive/date/tests.rs @@ -457,20 +457,28 @@ fn test_date_add() { check((2014, 1, 1), TimeDelta::seconds(86399), Some((2014, 1, 1))); // always round towards zero check((2014, 1, 1), TimeDelta::seconds(-86399), Some((2014, 1, 1))); - check((2014, 1, 1), TimeDelta::days(1), Some((2014, 1, 2))); - check((2014, 1, 1), TimeDelta::days(-1), Some((2013, 12, 31))); - check((2014, 1, 1), TimeDelta::days(364), Some((2014, 12, 31))); - check((2014, 1, 1), TimeDelta::days(365 * 4 + 1), Some((2018, 1, 1))); - check((2014, 1, 1), TimeDelta::days(365 * 400 + 97), Some((2414, 1, 1))); + check((2014, 1, 1), TimeDelta::try_days(1).unwrap(), Some((2014, 1, 2))); + check((2014, 1, 1), TimeDelta::try_days(-1).unwrap(), Some((2013, 12, 31))); + check((2014, 1, 1), TimeDelta::try_days(364).unwrap(), Some((2014, 12, 31))); + check((2014, 1, 1), TimeDelta::try_days(365 * 4 + 1).unwrap(), Some((2018, 1, 1))); + check((2014, 1, 1), TimeDelta::try_days(365 * 400 + 97).unwrap(), Some((2414, 1, 1))); - check((-7, 1, 1), TimeDelta::days(365 * 12 + 3), Some((5, 1, 1))); + check((-7, 1, 1), TimeDelta::try_days(365 * 12 + 3).unwrap(), Some((5, 1, 1))); // overflow check - check((0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 as i64), Some((MAX_YEAR, 12, 31))); - check((0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 as i64 + 1), None); + check( + (0, 1, 1), + TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64).unwrap(), + Some((MAX_YEAR, 12, 31)), + ); + check((0, 1, 1), TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64 + 1).unwrap(), None); check((0, 1, 1), TimeDelta::max_value(), None); - check((0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 as i64), Some((MIN_YEAR, 1, 1))); - check((0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 as i64 - 1), None); + check( + (0, 1, 1), + TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap(), + Some((MIN_YEAR, 1, 1)), + ); + check((0, 1, 1), TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64 - 1).unwrap(), None); check((0, 1, 1), TimeDelta::min_value(), None); } @@ -484,14 +492,14 @@ fn test_date_sub() { } check((2014, 1, 1), (2014, 1, 1), TimeDelta::zero()); - check((2014, 1, 2), (2014, 1, 1), TimeDelta::days(1)); - check((2014, 12, 31), (2014, 1, 1), TimeDelta::days(364)); - check((2015, 1, 3), (2014, 1, 1), TimeDelta::days(365 + 2)); - check((2018, 1, 1), (2014, 1, 1), TimeDelta::days(365 * 4 + 1)); - check((2414, 1, 1), (2014, 1, 1), TimeDelta::days(365 * 400 + 97)); + check((2014, 1, 2), (2014, 1, 1), TimeDelta::try_days(1).unwrap()); + check((2014, 12, 31), (2014, 1, 1), TimeDelta::try_days(364).unwrap()); + check((2015, 1, 3), (2014, 1, 1), TimeDelta::try_days(365 + 2).unwrap()); + check((2018, 1, 1), (2014, 1, 1), TimeDelta::try_days(365 * 4 + 1).unwrap()); + check((2414, 1, 1), (2014, 1, 1), TimeDelta::try_days(365 * 400 + 97).unwrap()); - check((MAX_YEAR, 12, 31), (0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 as i64)); - check((MIN_YEAR, 1, 1), (0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 as i64)); + check((MAX_YEAR, 12, 31), (0, 1, 1), TimeDelta::try_days(MAX_DAYS_FROM_YEAR_0 as i64).unwrap()); + check((MIN_YEAR, 1, 1), (0, 1, 1), TimeDelta::try_days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap()); } #[test] @@ -539,9 +547,9 @@ fn test_date_sub_days() { fn test_date_addassignment() { let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); let mut date = ymd(2016, 10, 1); - date += TimeDelta::days(10); + date += TimeDelta::try_days(10).unwrap(); assert_eq!(date, ymd(2016, 10, 11)); - date += TimeDelta::days(30); + date += TimeDelta::try_days(30).unwrap(); assert_eq!(date, ymd(2016, 11, 10)); } @@ -549,9 +557,9 @@ fn test_date_addassignment() { fn test_date_subassignment() { let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); let mut date = ymd(2016, 10, 11); - date -= TimeDelta::days(10); + date -= TimeDelta::try_days(10).unwrap(); assert_eq!(date, ymd(2016, 10, 1)); - date -= TimeDelta::days(2); + date -= TimeDelta::try_days(2).unwrap(); assert_eq!(date, ymd(2016, 9, 29)); } diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 13e9a1d00b..8fac7fed5b 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -491,7 +491,7 @@ impl NaiveDateTime { /// ``` /// # use chrono::{TimeDelta, NaiveDate}; /// # let hms = |h, m, s| NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(h, m, s).unwrap(); - /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::days(1_000_000_000)), None); + /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None); /// ``` /// /// Leap seconds are handled, @@ -514,7 +514,7 @@ impl NaiveDateTime { /// Some(hmsm(3, 6, 9, 300))); /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(-10)), /// Some(hmsm(3, 5, 50, 300))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::days(1)), + /// assert_eq!(leap.checked_add_signed(TimeDelta::try_days(1).unwrap()), /// Some(from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap())); /// ``` #[must_use] @@ -666,7 +666,7 @@ impl NaiveDateTime { /// ``` /// # use chrono::{TimeDelta, NaiveDate}; /// # let hms = |h, m, s| NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(h, m, s).unwrap(); - /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::days(1_000_000_000)), None); + /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None); /// ``` /// /// Leap seconds are handled, @@ -685,7 +685,7 @@ impl NaiveDateTime { /// Some(hmsm(3, 5, 59, 800))); /// assert_eq!(leap.checked_sub_signed(TimeDelta::seconds(60)), /// Some(hmsm(3, 5, 0, 300))); - /// assert_eq!(leap.checked_sub_signed(TimeDelta::days(1)), + /// assert_eq!(leap.checked_sub_signed(TimeDelta::try_days(1).unwrap()), /// Some(from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap())); /// ``` #[must_use] @@ -1586,7 +1586,7 @@ impl Timelike for NaiveDateTime { /// from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap() /// ); /// assert_eq!( -/// hms(3, 5, 7) + TimeDelta::days(365), +/// hms(3, 5, 7) + TimeDelta::try_days(365).unwrap(), /// from_ymd(2017, 7, 8).and_hms_opt(3, 5, 7).unwrap() /// ); /// @@ -1608,7 +1608,7 @@ impl Timelike for NaiveDateTime { /// assert_eq!(leap + TimeDelta::milliseconds(800), hmsm(3, 6, 0, 100)); /// assert_eq!(leap + TimeDelta::seconds(10), hmsm(3, 6, 9, 300)); /// assert_eq!(leap + TimeDelta::seconds(-10), hmsm(3, 5, 50, 300)); -/// assert_eq!(leap + TimeDelta::days(1), +/// assert_eq!(leap + TimeDelta::try_days(1).unwrap(), /// from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap()); /// ``` /// @@ -1776,7 +1776,7 @@ impl Add for NaiveDateTime { /// from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap() /// ); /// assert_eq!( -/// hms(3, 5, 7) - TimeDelta::days(365), +/// hms(3, 5, 7) - TimeDelta::try_days(365).unwrap(), /// from_ymd(2015, 7, 9).and_hms_opt(3, 5, 7).unwrap() /// ); /// @@ -1796,7 +1796,7 @@ impl Add for NaiveDateTime { /// assert_eq!(leap - TimeDelta::milliseconds(200), hmsm(3, 5, 59, 1_100)); /// assert_eq!(leap - TimeDelta::milliseconds(500), hmsm(3, 5, 59, 800)); /// assert_eq!(leap - TimeDelta::seconds(60), hmsm(3, 5, 0, 300)); -/// assert_eq!(leap - TimeDelta::days(1), +/// assert_eq!(leap - TimeDelta::try_days(1).unwrap(), /// from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap()); /// ``` /// diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 751ff7e696..f350ce0122 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -75,7 +75,7 @@ fn test_datetime_addassignment() { let mut date = ymdhms(2016, 10, 1, 10, 10, 10); date += TimeDelta::minutes(10_000_000); assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10)); - date += TimeDelta::days(10); + date += TimeDelta::try_days(10).unwrap(); assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10)); } @@ -86,7 +86,7 @@ fn test_datetime_subassignment() { let mut date = ymdhms(2016, 10, 1, 10, 10, 10); date -= TimeDelta::minutes(10_000_000); assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10)); - date -= TimeDelta::days(10); + date -= TimeDelta::try_days(10).unwrap(); assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10)); } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 57c4f5ca03..a6a7bd7e86 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -1166,7 +1166,7 @@ impl Timelike for NaiveTime { /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(22*60*60), from_hmsm(1, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-8*60*60), from_hmsm(19, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::days(800), from_hmsm(3, 5, 7, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0)); /// ``` /// /// Leap seconds are handled, but the addition assumes that it is the only leap second happened. @@ -1181,7 +1181,7 @@ impl Timelike for NaiveTime { /// assert_eq!(leap + TimeDelta::milliseconds(800), from_hmsm(3, 6, 0, 100)); /// assert_eq!(leap + TimeDelta::seconds(10), from_hmsm(3, 6, 9, 300)); /// assert_eq!(leap + TimeDelta::seconds(-10), from_hmsm(3, 5, 50, 300)); -/// assert_eq!(leap + TimeDelta::days(1), from_hmsm(3, 5, 59, 300)); +/// assert_eq!(leap + TimeDelta::try_days(1).unwrap(), from_hmsm(3, 5, 59, 300)); /// ``` /// /// [leap second handling]: crate::NaiveTime#leap-second-handling @@ -1281,7 +1281,7 @@ impl Add for NaiveTime { /// # use chrono::{TimeDelta, NaiveTime}; /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(8*60*60), from_hmsm(19, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::days(800), from_hmsm(3, 5, 7, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0)); /// ``` /// /// Leap seconds are handled, but the subtraction assumes that it is the only leap second happened. @@ -1294,7 +1294,7 @@ impl Add for NaiveTime { /// assert_eq!(leap - TimeDelta::milliseconds(200), from_hmsm(3, 5, 59, 1_100)); /// assert_eq!(leap - TimeDelta::milliseconds(500), from_hmsm(3, 5, 59, 800)); /// assert_eq!(leap - TimeDelta::seconds(60), from_hmsm(3, 5, 0, 300)); -/// assert_eq!(leap - TimeDelta::days(1), from_hmsm(3, 6, 0, 300)); +/// assert_eq!(leap - TimeDelta::try_days(1).unwrap(), from_hmsm(3, 6, 0, 300)); /// ``` /// /// [leap second handling]: crate::NaiveTime#leap-second-handling diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index a991117a7f..84acca5adf 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -103,9 +103,9 @@ fn test_time_add() { check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(1800), hmsm(3, 6, 1, 100)); check!(hmsm(3, 5, 59, 900), TimeDelta::seconds(86399), hmsm(3, 5, 58, 900)); // overwrap check!(hmsm(3, 5, 59, 900), TimeDelta::seconds(-86399), hmsm(3, 6, 0, 900)); - check!(hmsm(3, 5, 59, 900), TimeDelta::days(12345), hmsm(3, 5, 59, 900)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(1), hmsm(3, 5, 59, 300)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(-1), hmsm(3, 6, 0, 300)); + check!(hmsm(3, 5, 59, 900), TimeDelta::try_days(12345).unwrap(), hmsm(3, 5, 59, 900)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_days(1).unwrap(), hmsm(3, 5, 59, 300)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_days(-1).unwrap(), hmsm(3, 6, 0, 300)); // regression tests for #37 check!(hmsm(0, 0, 0, 0), TimeDelta::milliseconds(-990), hmsm(23, 59, 59, 10)); @@ -131,11 +131,11 @@ fn test_time_overflowing_add() { // overflowing_add_signed with leap seconds may be counter-intuitive assert_eq!( - hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::days(1)), + hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::try_days(1).unwrap()), (hmsm(3, 4, 59, 678), 86_400) ); assert_eq!( - hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::days(-1)), + hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::try_days(-1).unwrap()), (hmsm(3, 5, 0, 678), -86_400) ); } diff --git a/src/round.rs b/src/round.rs index f30422e3e9..53e7d06234 100644 --- a/src/round.rs +++ b/src/round.rs @@ -131,7 +131,7 @@ pub trait DurationRound: Sized { /// "2018-01-11 12:00:00.150 UTC" /// ); /// assert_eq!( - /// dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), + /// dt.duration_round(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), /// "2018-01-12 00:00:00 UTC" /// ); /// ``` @@ -153,7 +153,7 @@ pub trait DurationRound: Sized { /// "2018-01-11 12:00:00.150 UTC" /// ); /// assert_eq!( - /// dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), + /// dt.duration_trunc(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), /// "2018-01-11 00:00:00 UTC" /// ); /// ``` @@ -268,7 +268,7 @@ pub enum RoundingError { /// .unwrap(); /// /// assert_eq!( - /// dt.duration_round(TimeDelta::days(300 * 365)), + /// dt.duration_round(TimeDelta::try_days(300 * 365).unwrap()), /// Err(RoundingError::DurationExceedsLimit) /// ); /// ``` @@ -280,7 +280,10 @@ pub enum RoundingError { /// # use chrono::{DurationRound, TimeDelta, RoundingError, TimeZone, Utc}; /// let dt = Utc.with_ymd_and_hms(2300, 12, 12, 0, 0, 0).unwrap(); /// - /// assert_eq!(dt.duration_round(TimeDelta::days(1)), Err(RoundingError::TimestampExceedsLimit),); + /// assert_eq!( + /// dt.duration_round(TimeDelta::try_days(1).unwrap()), + /// Err(RoundingError::TimestampExceedsLimit) + /// ); /// ``` TimestampExceedsLimit, } @@ -505,7 +508,7 @@ mod tests { "2012-12-12 18:00:00 UTC" ); assert_eq!( - dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_round(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2012-12-13 00:00:00 UTC" ); @@ -513,7 +516,7 @@ mod tests { let dt = FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_round(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2020-10-28 00:00:00 +01:00" ); assert_eq!( @@ -525,7 +528,7 @@ mod tests { let dt = FixedOffset::west_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_round(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2020-10-28 00:00:00 -01:00" ); assert_eq!( @@ -598,7 +601,7 @@ mod tests { "2012-12-12 18:00:00" ); assert_eq!( - dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_round(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2012-12-13 00:00:00" ); } @@ -667,7 +670,7 @@ mod tests { "2012-12-12 18:00:00 UTC" ); assert_eq!( - dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_trunc(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2012-12-12 00:00:00 UTC" ); @@ -675,7 +678,7 @@ mod tests { let dt = FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_trunc(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2020-10-27 00:00:00 +01:00" ); assert_eq!( @@ -687,7 +690,7 @@ mod tests { let dt = FixedOffset::west_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_trunc(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2020-10-27 00:00:00 -01:00" ); assert_eq!( @@ -754,7 +757,7 @@ mod tests { "2012-12-12 18:00:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), + dt.duration_trunc(TimeDelta::try_days(1).unwrap()).unwrap().to_string(), "2012-12-12 00:00:00" ); } diff --git a/src/time_delta.rs b/src/time_delta.rs index 9f5b7b6a65..fdf2050db2 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -621,26 +621,22 @@ mod tests { #[test] fn test_duration() { + let days = |d| TimeDelta::try_days(d).unwrap(); + assert!(TimeDelta::seconds(1) != TimeDelta::zero()); assert_eq!(TimeDelta::seconds(1) + TimeDelta::seconds(2), TimeDelta::seconds(3)); assert_eq!( TimeDelta::seconds(86_399) + TimeDelta::seconds(4), - TimeDelta::days(1) + TimeDelta::seconds(3) - ); - assert_eq!(TimeDelta::days(10) - TimeDelta::seconds(1000), TimeDelta::seconds(863_000)); - assert_eq!( - TimeDelta::days(10) - TimeDelta::seconds(1_000_000), - TimeDelta::seconds(-136_000) + days(1) + TimeDelta::seconds(3) ); + assert_eq!(days(10) - TimeDelta::seconds(1000), TimeDelta::seconds(863_000)); + assert_eq!(days(10) - TimeDelta::seconds(1_000_000), TimeDelta::seconds(-136_000)); assert_eq!( - TimeDelta::days(2) + TimeDelta::seconds(86_399) + TimeDelta::nanoseconds(1_234_567_890), - TimeDelta::days(3) + TimeDelta::nanoseconds(234_567_890) - ); - assert_eq!(-TimeDelta::days(3), TimeDelta::days(-3)); - assert_eq!( - -(TimeDelta::days(3) + TimeDelta::seconds(70)), - TimeDelta::days(-4) + TimeDelta::seconds(86_400 - 70) + days(2) + TimeDelta::seconds(86_399) + TimeDelta::nanoseconds(1_234_567_890), + days(3) + TimeDelta::nanoseconds(234_567_890) ); + assert_eq!(-days(3), days(-3)); + assert_eq!(-(days(3) + TimeDelta::seconds(70)), days(-4) + TimeDelta::seconds(86_400 - 70)); let mut d = TimeDelta::default(); d += TimeDelta::minutes(1); @@ -651,14 +647,14 @@ mod tests { #[test] fn test_duration_num_days() { assert_eq!(TimeDelta::zero().num_days(), 0); - assert_eq!(TimeDelta::days(1).num_days(), 1); - assert_eq!(TimeDelta::days(-1).num_days(), -1); + assert_eq!(TimeDelta::try_days(1).unwrap().num_days(), 1); + assert_eq!(TimeDelta::try_days(-1).unwrap().num_days(), -1); assert_eq!(TimeDelta::seconds(86_399).num_days(), 0); assert_eq!(TimeDelta::seconds(86_401).num_days(), 1); assert_eq!(TimeDelta::seconds(-86_399).num_days(), 0); assert_eq!(TimeDelta::seconds(-86_401).num_days(), -1); - assert_eq!(TimeDelta::days(i32::MAX as i64).num_days(), i32::MAX as i64); - assert_eq!(TimeDelta::days(i32::MIN as i64).num_days(), i32::MIN as i64); + assert_eq!(TimeDelta::try_days(i32::MAX as i64).unwrap().num_days(), i32::MAX as i64); + assert_eq!(TimeDelta::try_days(i32::MIN as i64).unwrap().num_days(), i32::MIN as i64); } #[test] @@ -791,15 +787,21 @@ mod tests { // overflow checks const MICROS_PER_DAY: i64 = 86_400_000_000; assert_eq!( - TimeDelta::days(i64::MAX / MICROS_PER_DAY).num_microseconds(), + TimeDelta::try_days(i64::MAX / MICROS_PER_DAY).unwrap().num_microseconds(), Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY) ); assert_eq!( - TimeDelta::days(-i64::MAX / MICROS_PER_DAY).num_microseconds(), + TimeDelta::try_days(-i64::MAX / MICROS_PER_DAY).unwrap().num_microseconds(), Some(-i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY) ); - assert_eq!(TimeDelta::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None); - assert_eq!(TimeDelta::days(-i64::MAX / MICROS_PER_DAY - 1).num_microseconds(), None); + assert_eq!( + TimeDelta::try_days(i64::MAX / MICROS_PER_DAY + 1).unwrap().num_microseconds(), + None + ); + assert_eq!( + TimeDelta::try_days(-i64::MAX / MICROS_PER_DAY - 1).unwrap().num_microseconds(), + None + ); } #[test] fn test_duration_microseconds_max_allowed() { @@ -887,15 +889,21 @@ mod tests { // overflow checks const NANOS_PER_DAY: i64 = 86_400_000_000_000; assert_eq!( - TimeDelta::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(), + TimeDelta::try_days(i64::MAX / NANOS_PER_DAY).unwrap().num_nanoseconds(), Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY) ); assert_eq!( - TimeDelta::days(-i64::MAX / NANOS_PER_DAY).num_nanoseconds(), + TimeDelta::try_days(-i64::MAX / NANOS_PER_DAY).unwrap().num_nanoseconds(), Some(-i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY) ); - assert_eq!(TimeDelta::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None); - assert_eq!(TimeDelta::days(-i64::MAX / NANOS_PER_DAY - 1).num_nanoseconds(), None); + assert_eq!( + TimeDelta::try_days(i64::MAX / NANOS_PER_DAY + 1).unwrap().num_nanoseconds(), + None + ); + assert_eq!( + TimeDelta::try_days(-i64::MAX / NANOS_PER_DAY - 1).unwrap().num_nanoseconds(), + None + ); } #[test] fn test_duration_nanoseconds_max_allowed() { @@ -1079,8 +1087,9 @@ mod tests { TimeDelta::seconds(10) - TimeDelta::nanoseconds(10) ); assert_eq!( - (TimeDelta::nanoseconds(1) + TimeDelta::seconds(1) + TimeDelta::days(1)) * 3, - TimeDelta::nanoseconds(3) + TimeDelta::seconds(3) + TimeDelta::days(3) + (TimeDelta::nanoseconds(1) + TimeDelta::seconds(1) + TimeDelta::try_days(1).unwrap()) + * 3, + TimeDelta::nanoseconds(3) + TimeDelta::seconds(3) + TimeDelta::try_days(3).unwrap() ); assert_eq!(TimeDelta::milliseconds(1500) * -2, TimeDelta::seconds(-3)); assert_eq!(TimeDelta::milliseconds(-1500) * 2, TimeDelta::seconds(-3)); @@ -1131,14 +1140,14 @@ mod tests { #[test] fn test_duration_fmt() { assert_eq!(TimeDelta::zero().to_string(), "P0D"); - assert_eq!(TimeDelta::days(42).to_string(), "PT3628800S"); - assert_eq!(TimeDelta::days(-42).to_string(), "-PT3628800S"); + assert_eq!(TimeDelta::try_days(42).unwrap().to_string(), "PT3628800S"); + assert_eq!(TimeDelta::try_days(-42).unwrap().to_string(), "-PT3628800S"); assert_eq!(TimeDelta::seconds(42).to_string(), "PT42S"); assert_eq!(TimeDelta::milliseconds(42).to_string(), "PT0.042S"); assert_eq!(TimeDelta::microseconds(42).to_string(), "PT0.000042S"); assert_eq!(TimeDelta::nanoseconds(42).to_string(), "PT0.000000042S"); assert_eq!( - (TimeDelta::days(7) + TimeDelta::milliseconds(6543)).to_string(), + (TimeDelta::try_days(7).unwrap() + TimeDelta::milliseconds(6543)).to_string(), "PT604806.543S" ); assert_eq!(TimeDelta::seconds(-86_401).to_string(), "-PT86401S"); @@ -1146,7 +1155,7 @@ mod tests { // the format specifier should have no effect on `TimeDelta` assert_eq!( - format!("{:30}", TimeDelta::days(1) + TimeDelta::milliseconds(2345)), + format!("{:30}", TimeDelta::try_days(1).unwrap() + TimeDelta::milliseconds(2345)), "PT86402.345S" ); } @@ -1190,7 +1199,7 @@ mod tests { #[test] fn test_duration_const() { const ONE_WEEK: TimeDelta = expect!(TimeDelta::try_weeks(1), ""); - const ONE_DAY: TimeDelta = TimeDelta::days(1); + const ONE_DAY: TimeDelta = expect!(TimeDelta::try_days(1), ""); const ONE_HOUR: TimeDelta = TimeDelta::hours(1); const ONE_MINUTE: TimeDelta = TimeDelta::minutes(1); const ONE_SECOND: TimeDelta = TimeDelta::seconds(1); diff --git a/tests/dateutils.rs b/tests/dateutils.rs index c833c9fe20..34f4ac7e3a 100644 --- a/tests/dateutils.rs +++ b/tests/dateutils.rs @@ -1,6 +1,6 @@ #![cfg(all(unix, feature = "clock", feature = "std"))] -use chrono::{Datelike, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike}; +use chrono::{Datelike, Days, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike}; use std::{path, process, thread}; fn verify_against_date_command_local(path: &'static str, dt: NaiveDateTime) { @@ -94,7 +94,7 @@ fn try_verify_against_date_command() { let end = NaiveDate::from_ymd_opt(*year + 1, 1, 1).unwrap().and_time(NaiveTime::MIN); while date <= end { verify_against_date_command_local(DATE_PATH, date); - date += chrono::TimeDelta::hours(1); + date += chrono::TimeDelta::try_hours(1).unwrap(); } })); }