From 2bf3302ce58aadaae06a8a1fb6342295292863da Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 21 Feb 2024 08:09:32 +0100 Subject: [PATCH] Tests: replace `TimeDelta::seconds` with `try_seconds` --- src/datetime/tests.rs | 12 ++-- src/lib.rs | 16 ++--- src/naive/date/mod.rs | 14 +++-- src/naive/date/tests.rs | 4 +- src/naive/datetime/mod.rs | 86 +++++++++++++++----------- src/naive/datetime/tests.rs | 27 +++++---- src/naive/time/mod.rs | 97 ++++++++++++++++++----------- src/naive/time/tests.rs | 8 +-- src/time_delta.rs | 118 +++++++++++++++++++----------------- 9 files changed, 219 insertions(+), 163 deletions(-) diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 9f37050648..5adf16f33c 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -580,12 +580,12 @@ fn test_datetime_offset() { let dt = Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap(); assert_eq!(dt, edt.with_ymd_and_hms(2014, 5, 6, 3, 8, 9).unwrap()); assert_eq!( - dt + TimeDelta::seconds(3600 + 60 + 1), + dt + TimeDelta::try_seconds(3600 + 60 + 1).unwrap(), Utc.with_ymd_and_hms(2014, 5, 6, 8, 9, 10).unwrap() ); assert_eq!( dt.signed_duration_since(edt.with_ymd_and_hms(2014, 5, 6, 10, 11, 12).unwrap()), - TimeDelta::seconds(-7 * 3600 - 3 * 60 - 3) + TimeDelta::try_seconds(-7 * 3600 - 3 * 60 - 3).unwrap() ); assert_eq!(*Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), Utc); @@ -1474,20 +1474,20 @@ fn test_datetime_add_assign() { let datetime = naivedatetime.and_utc(); let mut datetime_add = datetime; - datetime_add += TimeDelta::seconds(60); - assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); + datetime_add += TimeDelta::try_seconds(60).unwrap(); + assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap()); let timezone = FixedOffset::east_opt(60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_add = datetime_add.with_timezone(&timezone); - assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); + assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap()); let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_add = datetime_add.with_timezone(&timezone); - assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); + assert_eq!(datetime_add, datetime + TimeDelta::try_seconds(60).unwrap()); } #[test] diff --git a/src/lib.rs b/src/lib.rs index ad2312b877..917b39691b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -257,16 +257,12 @@ //! // arithmetic operations //! let dt1 = Utc.with_ymd_and_hms(2014, 11, 14, 8, 9, 10).unwrap(); //! let dt2 = Utc.with_ymd_and_hms(2014, 11, 14, 10, 9, 8).unwrap(); -//! assert_eq!(dt1.signed_duration_since(dt2), TimeDelta::seconds(-2 * 3600 + 2)); -//! assert_eq!(dt2.signed_duration_since(dt1), TimeDelta::seconds(2 * 3600 - 2)); -//! assert_eq!( -//! Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + TimeDelta::seconds(1_000_000_000), -//! Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap() -//! ); -//! assert_eq!( -//! Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() - TimeDelta::seconds(1_000_000_000), -//! Utc.with_ymd_and_hms(1938, 4, 24, 22, 13, 20).unwrap() -//! ); +//! assert_eq!(dt1.signed_duration_since(dt2), TimeDelta::try_seconds(-2 * 3600 + 2).unwrap()); +//! assert_eq!(dt2.signed_duration_since(dt1), TimeDelta::try_seconds(2 * 3600 - 2).unwrap()); +//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + TimeDelta::try_seconds(1_000_000_000).unwrap(), +//! Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap()); +//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() - TimeDelta::try_seconds(1_000_000_000).unwrap(), +//! Utc.with_ymd_and_hms(1938, 4, 24, 22, 13, 20).unwrap()); //! ``` //! //! ### Formatting and Parsing diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 1ca2cd0b80..9c6d04b480 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -1907,8 +1907,11 @@ impl Datelike for NaiveDate { /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// 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::try_seconds(86399).unwrap(), from_ymd(2014, 1, 1)); +/// assert_eq!( +/// from_ymd(2014, 1, 1) + TimeDelta::try_seconds(-86399).unwrap(), +/// from_ymd(2014, 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)); @@ -2056,8 +2059,11 @@ impl Sub for NaiveDate { /// let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// /// 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::try_seconds(86399).unwrap(), from_ymd(2014, 1, 1)); +/// assert_eq!( +/// from_ymd(2014, 1, 1) - TimeDelta::try_seconds(-86399).unwrap(), +/// from_ymd(2014, 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)); diff --git a/src/naive/date/tests.rs b/src/naive/date/tests.rs index cde973daf4..6d8c299896 100644 --- a/src/naive/date/tests.rs +++ b/src/naive/date/tests.rs @@ -454,9 +454,9 @@ fn test_date_add() { } check((2014, 1, 1), TimeDelta::zero(), Some((2014, 1, 1))); - check((2014, 1, 1), TimeDelta::seconds(86399), Some((2014, 1, 1))); + check((2014, 1, 1), TimeDelta::try_seconds(86399).unwrap(), Some((2014, 1, 1))); // always round towards zero - check((2014, 1, 1), TimeDelta::seconds(-86399), Some((2014, 1, 1))); + check((2014, 1, 1), TimeDelta::try_seconds(-86399).unwrap(), Some((2014, 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))); diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 8fac7fed5b..b310fdcb24 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -471,11 +471,20 @@ impl NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::zero()), Some(hms(3, 5, 7))); - /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(1)), Some(hms(3, 5, 8))); - /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(-1)), Some(hms(3, 5, 6))); - /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(3600 + 60)), Some(hms(4, 6, 7))); /// assert_eq!( - /// hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(86_400)), + /// hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(1).unwrap()), + /// Some(hms(3, 5, 8)) + /// ); + /// assert_eq!( + /// hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(-1).unwrap()), + /// Some(hms(3, 5, 6)) + /// ); + /// assert_eq!( + /// hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(3600 + 60).unwrap()), + /// Some(hms(4, 6, 7)) + /// ); + /// assert_eq!( + /// hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(86_400).unwrap()), /// Some(from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap()) /// ); /// @@ -510,9 +519,9 @@ impl NaiveDateTime { /// Some(hmsm(3, 5, 59, 1_800))); /// assert_eq!(leap.checked_add_signed(TimeDelta::milliseconds(800)), /// Some(hmsm(3, 6, 0, 100))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(10)), + /// assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(10).unwrap()), /// Some(hmsm(3, 6, 9, 300))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(-10)), + /// assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(-10).unwrap()), /// Some(hmsm(3, 5, 50, 300))); /// 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())); @@ -646,11 +655,20 @@ impl NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::zero()), Some(hms(3, 5, 7))); - /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(1)), Some(hms(3, 5, 6))); - /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(-1)), Some(hms(3, 5, 8))); - /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(3600 + 60)), Some(hms(2, 4, 7))); /// assert_eq!( - /// hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(86_400)), + /// hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(1).unwrap()), + /// Some(hms(3, 5, 6)) + /// ); + /// assert_eq!( + /// hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(-1).unwrap()), + /// Some(hms(3, 5, 8)) + /// ); + /// assert_eq!( + /// hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(3600 + 60).unwrap()), + /// Some(hms(2, 4, 7)) + /// ); + /// assert_eq!( + /// hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(86_400).unwrap()), /// Some(from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap()) /// ); /// @@ -683,7 +701,7 @@ impl NaiveDateTime { /// Some(hmsm(3, 5, 59, 1_100))); /// assert_eq!(leap.checked_sub_signed(TimeDelta::milliseconds(500)), /// Some(hmsm(3, 5, 59, 800))); - /// assert_eq!(leap.checked_sub_signed(TimeDelta::seconds(60)), + /// assert_eq!(leap.checked_sub_signed(TimeDelta::try_seconds(60).unwrap()), /// Some(hmsm(3, 5, 0, 300))); /// 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())); @@ -767,7 +785,7 @@ impl NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// assert_eq!( /// d.and_hms_opt(3, 5, 7).unwrap().signed_duration_since(d.and_hms_opt(2, 4, 6).unwrap()), - /// TimeDelta::seconds(3600 + 60 + 1) + /// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() /// ); /// /// // July 8 is 190th day in the year 2016 @@ -776,7 +794,7 @@ impl NaiveDateTime { /// d.and_hms_milli_opt(0, 7, 6, 500) /// .unwrap() /// .signed_duration_since(d0.and_hms_opt(0, 0, 0).unwrap()), - /// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500) + /// TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap() + TimeDelta::milliseconds(500) /// ); /// ``` /// @@ -789,11 +807,11 @@ impl NaiveDateTime { /// let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap(); /// assert_eq!( /// leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap()), - /// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500) + /// TimeDelta::try_seconds(3600).unwrap() + TimeDelta::milliseconds(500) /// ); /// assert_eq!( /// from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap().signed_duration_since(leap), - /// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500) + /// TimeDelta::try_seconds(3600).unwrap() - TimeDelta::milliseconds(500) /// ); /// ``` #[must_use] @@ -1578,11 +1596,11 @@ impl Timelike for NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); /// assert_eq!(hms(3, 5, 7) + TimeDelta::zero(), hms(3, 5, 7)); -/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(1), hms(3, 5, 8)); -/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(-1), hms(3, 5, 6)); -/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(3600 + 60), hms(4, 6, 7)); +/// assert_eq!(hms(3, 5, 7) + TimeDelta::try_seconds(1).unwrap(), hms(3, 5, 8)); +/// assert_eq!(hms(3, 5, 7) + TimeDelta::try_seconds(-1).unwrap(), hms(3, 5, 6)); +/// assert_eq!(hms(3, 5, 7) + TimeDelta::try_seconds(3600 + 60).unwrap(), hms(4, 6, 7)); /// assert_eq!( -/// hms(3, 5, 7) + TimeDelta::seconds(86_400), +/// hms(3, 5, 7) + TimeDelta::try_seconds(86_400).unwrap(), /// from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap() /// ); /// assert_eq!( @@ -1602,12 +1620,12 @@ impl Timelike for NaiveDateTime { /// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s, milli).unwrap(); /// let leap = hmsm(3, 5, 59, 1_300); -/// assert_eq!(leap + TimeDelta::zero(), hmsm(3, 5, 59, 1_300)); +/// assert_eq!(leap + TimeDelta::zero(), hmsm(3, 5, 59, 1_300)); /// assert_eq!(leap + TimeDelta::milliseconds(-500), hmsm(3, 5, 59, 800)); -/// assert_eq!(leap + TimeDelta::milliseconds(500), hmsm(3, 5, 59, 1_800)); -/// 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::milliseconds(500), hmsm(3, 5, 59, 1_800)); +/// assert_eq!(leap + TimeDelta::milliseconds(800), hmsm(3, 6, 0, 100)); +/// assert_eq!(leap + TimeDelta::try_seconds(10).unwrap(), hmsm(3, 6, 9, 300)); +/// assert_eq!(leap + TimeDelta::try_seconds(-10).unwrap(), hmsm(3, 5, 50, 300)); /// assert_eq!(leap + TimeDelta::try_days(1).unwrap(), /// from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap()); /// ``` @@ -1768,11 +1786,11 @@ impl Add for NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap(); /// assert_eq!(hms(3, 5, 7) - TimeDelta::zero(), hms(3, 5, 7)); -/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(1), hms(3, 5, 6)); -/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(-1), hms(3, 5, 8)); -/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(3600 + 60), hms(2, 4, 7)); +/// assert_eq!(hms(3, 5, 7) - TimeDelta::try_seconds(1).unwrap(), hms(3, 5, 6)); +/// assert_eq!(hms(3, 5, 7) - TimeDelta::try_seconds(-1).unwrap(), hms(3, 5, 8)); +/// assert_eq!(hms(3, 5, 7) - TimeDelta::try_seconds(3600 + 60).unwrap(), hms(2, 4, 7)); /// assert_eq!( -/// hms(3, 5, 7) - TimeDelta::seconds(86_400), +/// hms(3, 5, 7) - TimeDelta::try_seconds(86_400).unwrap(), /// from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap() /// ); /// assert_eq!( @@ -1792,10 +1810,10 @@ impl Add for NaiveDateTime { /// # let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap(); /// # let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s, milli).unwrap(); /// let leap = hmsm(3, 5, 59, 1_300); -/// assert_eq!(leap - TimeDelta::zero(), hmsm(3, 5, 59, 1_300)); +/// assert_eq!(leap - TimeDelta::zero(), hmsm(3, 5, 59, 1_300)); /// 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::try_seconds(60).unwrap(), hmsm(3, 5, 0, 300)); /// assert_eq!(leap - TimeDelta::try_days(1).unwrap(), /// from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap()); /// ``` @@ -1942,14 +1960,14 @@ impl Sub for NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// assert_eq!( /// d.and_hms_opt(3, 5, 7).unwrap() - d.and_hms_opt(2, 4, 6).unwrap(), -/// TimeDelta::seconds(3600 + 60 + 1) +/// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() /// ); /// /// // July 8 is 190th day in the year 2016 /// let d0 = from_ymd(2016, 1, 1); /// assert_eq!( /// d.and_hms_milli_opt(0, 7, 6, 500).unwrap() - d0.and_hms_opt(0, 0, 0).unwrap(), -/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500) +/// TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap() + TimeDelta::milliseconds(500) /// ); /// ``` /// @@ -1962,11 +1980,11 @@ impl Sub for NaiveDateTime { /// let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap(); /// assert_eq!( /// leap - from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap(), -/// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500) +/// TimeDelta::try_seconds(3600).unwrap() + TimeDelta::milliseconds(500) /// ); /// assert_eq!( /// from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap() - leap, -/// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500) +/// TimeDelta::try_seconds(3600).unwrap() - TimeDelta::milliseconds(500) /// ); /// ``` impl Sub for NaiveDateTime { diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 38509f4549..27e884c036 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -15,13 +15,14 @@ fn test_datetime_add() { assert_eq!(lhs.checked_add_signed(rhs), sum); assert_eq!(lhs.checked_sub_signed(-rhs), sum); } + let seconds = |s| TimeDelta::try_seconds(s).unwrap(); - check((2014, 5, 6, 7, 8, 9), TimeDelta::seconds(3600 + 60 + 1), Some((2014, 5, 6, 8, 9, 10))); - check((2014, 5, 6, 7, 8, 9), TimeDelta::seconds(-(3600 + 60 + 1)), Some((2014, 5, 6, 6, 7, 8))); - check((2014, 5, 6, 7, 8, 9), TimeDelta::seconds(86399), Some((2014, 5, 7, 7, 8, 8))); - check((2014, 5, 6, 7, 8, 9), TimeDelta::seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9))); - check((2014, 5, 6, 7, 8, 9), TimeDelta::seconds(-86_400 * 10), Some((2014, 4, 26, 7, 8, 9))); - check((2014, 5, 6, 7, 8, 9), TimeDelta::seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9))); + check((2014, 5, 6, 7, 8, 9), seconds(3600 + 60 + 1), Some((2014, 5, 6, 8, 9, 10))); + check((2014, 5, 6, 7, 8, 9), seconds(-(3600 + 60 + 1)), Some((2014, 5, 6, 6, 7, 8))); + check((2014, 5, 6, 7, 8, 9), seconds(86399), Some((2014, 5, 7, 7, 8, 8))); + check((2014, 5, 6, 7, 8, 9), seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9))); + check((2014, 5, 6, 7, 8, 9), seconds(-86_400 * 10), Some((2014, 4, 26, 7, 8, 9))); + check((2014, 5, 6, 7, 8, 9), seconds(86_400 * 10), Some((2014, 5, 16, 7, 8, 9))); // overflow check // assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`. @@ -31,16 +32,16 @@ fn test_datetime_add() { check((0, 1, 1, 0, 0, 0), max_days_from_year_0, Some((NaiveDate::MAX.year(), 12, 31, 0, 0, 0))); check( (0, 1, 1, 0, 0, 0), - max_days_from_year_0 + TimeDelta::seconds(86399), + max_days_from_year_0 + seconds(86399), Some((NaiveDate::MAX.year(), 12, 31, 23, 59, 59)), ); - check((0, 1, 1, 0, 0, 0), max_days_from_year_0 + TimeDelta::seconds(86_400), None); + check((0, 1, 1, 0, 0, 0), max_days_from_year_0 + seconds(86_400), None); check((0, 1, 1, 0, 0, 0), TimeDelta::max_value(), None); let min_days_from_year_0 = NaiveDate::MIN.signed_duration_since(NaiveDate::from_ymd_opt(0, 1, 1).unwrap()); check((0, 1, 1, 0, 0, 0), min_days_from_year_0, Some((NaiveDate::MIN.year(), 1, 1, 0, 0, 0))); - check((0, 1, 1, 0, 0, 0), min_days_from_year_0 - TimeDelta::seconds(1), None); + check((0, 1, 1, 0, 0, 0), min_days_from_year_0 - seconds(1), None); check((0, 1, 1, 0, 0, 0), TimeDelta::min_value(), None); } @@ -52,19 +53,19 @@ fn test_datetime_sub() { assert_eq!(since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 9)), TimeDelta::zero()); assert_eq!( since(ymdhms(2014, 5, 6, 7, 8, 10), ymdhms(2014, 5, 6, 7, 8, 9)), - TimeDelta::seconds(1) + TimeDelta::try_seconds(1).unwrap() ); assert_eq!( since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)), - TimeDelta::seconds(-1) + TimeDelta::try_seconds(-1).unwrap() ); assert_eq!( since(ymdhms(2014, 5, 7, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)), - TimeDelta::seconds(86399) + TimeDelta::try_seconds(86399).unwrap() ); assert_eq!( since(ymdhms(2001, 9, 9, 1, 46, 39), ymdhms(1970, 1, 1, 0, 0, 0)), - TimeDelta::seconds(999_999_999) + TimeDelta::try_seconds(999_999_999).unwrap() ); } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 61a97e1128..778d415e7b 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -702,16 +702,25 @@ impl NaiveTime { /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)), /// TimeDelta::milliseconds(975) /// ); - /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)), TimeDelta::seconds(7)); - /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)), TimeDelta::seconds(5 * 60)); + /// assert_eq!( + /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)), + /// TimeDelta::try_seconds(7).unwrap() + /// ); + /// assert_eq!( + /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)), + /// TimeDelta::try_seconds(5 * 60).unwrap() + /// ); /// assert_eq!( /// since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)), - /// TimeDelta::seconds(3 * 3600) + /// TimeDelta::try_seconds(3 * 3600).unwrap() + /// ); + /// assert_eq!( + /// since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)), + /// TimeDelta::try_seconds(-3600).unwrap() /// ); - /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)), TimeDelta::seconds(-3600)); /// assert_eq!( /// since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)), - /// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100) + /// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::milliseconds(100) /// ); /// ``` /// @@ -723,15 +732,15 @@ impl NaiveTime { /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// # let since = NaiveTime::signed_duration_since; /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)), - /// TimeDelta::seconds(1)); + /// TimeDelta::try_seconds(1).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)), /// TimeDelta::milliseconds(1500)); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)), - /// TimeDelta::seconds(60)); + /// TimeDelta::try_seconds(60).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)), - /// TimeDelta::seconds(1)); + /// TimeDelta::try_seconds(1).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)), - /// TimeDelta::seconds(61)); + /// TimeDelta::try_seconds(61).unwrap()); /// ``` #[must_use] pub const fn signed_duration_since(self, rhs: NaiveTime) -> TimeDelta { @@ -1147,11 +1156,14 @@ 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::zero(), from_hmsm(3, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(1), from_hmsm(3, 5, 8, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-1), from_hmsm(3, 5, 6, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(60 + 4), from_hmsm(3, 6, 11, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(1).unwrap(), from_hmsm(3, 5, 8, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(-1).unwrap(), from_hmsm(3, 5, 6, 0)); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(60 + 4).unwrap(), +/// from_hmsm(3, 6, 11, 0) +/// ); /// assert_eq!( -/// from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(7 * 60 * 60 - 6 * 60), +/// from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(7 * 60 * 60 - 6 * 60).unwrap(), /// from_hmsm(9, 59, 7, 0) /// ); /// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::milliseconds(80), from_hmsm(3, 5, 7, 80)); @@ -1164,8 +1176,8 @@ impl Timelike 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(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::try_seconds(22*60*60).unwrap(), from_hmsm(1, 5, 7, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(-8*60*60).unwrap(), from_hmsm(19, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0)); /// ``` /// @@ -1175,12 +1187,12 @@ impl Timelike for NaiveTime { /// # use chrono::{TimeDelta, NaiveTime}; /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() }; /// let leap = from_hmsm(3, 5, 59, 1_300); -/// assert_eq!(leap + TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300)); +/// assert_eq!(leap + TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300)); /// assert_eq!(leap + TimeDelta::milliseconds(-500), from_hmsm(3, 5, 59, 800)); -/// assert_eq!(leap + TimeDelta::milliseconds(500), from_hmsm(3, 5, 59, 1_800)); -/// 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::milliseconds(500), from_hmsm(3, 5, 59, 1_800)); +/// assert_eq!(leap + TimeDelta::milliseconds(800), from_hmsm(3, 6, 0, 100)); +/// assert_eq!(leap + TimeDelta::try_seconds(10).unwrap(), from_hmsm(3, 6, 9, 300)); +/// assert_eq!(leap + TimeDelta::try_seconds(-10).unwrap(), from_hmsm(3, 5, 50, 300)); /// assert_eq!(leap + TimeDelta::try_days(1).unwrap(), from_hmsm(3, 5, 59, 300)); /// ``` /// @@ -1265,10 +1277,13 @@ impl Add 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::zero(), from_hmsm(3, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(1), from_hmsm(3, 5, 6, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(60 + 5), from_hmsm(3, 4, 2, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(1).unwrap(), from_hmsm(3, 5, 6, 0)); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(60 + 5).unwrap(), +/// from_hmsm(3, 4, 2, 0) +/// ); /// assert_eq!( -/// from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(2 * 60 * 60 + 6 * 60), +/// from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(2 * 60 * 60 + 6 * 60).unwrap(), /// from_hmsm(0, 59, 7, 0) /// ); /// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::milliseconds(80), from_hmsm(3, 5, 6, 920)); @@ -1280,7 +1295,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::try_seconds(8*60*60).unwrap(), from_hmsm(19, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0)); /// ``` /// @@ -1290,10 +1305,10 @@ 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() }; /// let leap = from_hmsm(3, 5, 59, 1_300); -/// assert_eq!(leap - TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300)); +/// assert_eq!(leap - TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300)); /// 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::try_seconds(60).unwrap(), from_hmsm(3, 5, 0, 300)); /// assert_eq!(leap - TimeDelta::try_days(1).unwrap(), from_hmsm(3, 6, 0, 300)); /// ``` /// @@ -1383,13 +1398,25 @@ impl Sub for NaiveTime { /// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 900), TimeDelta::zero()); /// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 875), TimeDelta::milliseconds(25)); /// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 6, 925), TimeDelta::milliseconds(975)); -/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 0, 900), TimeDelta::seconds(7)); -/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 0, 7, 900), TimeDelta::seconds(5 * 60)); -/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(0, 5, 7, 900), TimeDelta::seconds(3 * 3600)); -/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(4, 5, 7, 900), TimeDelta::seconds(-3600)); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 0, 900), +/// TimeDelta::try_seconds(7).unwrap() +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 900) - from_hmsm(3, 0, 7, 900), +/// TimeDelta::try_seconds(5 * 60).unwrap() +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 900) - from_hmsm(0, 5, 7, 900), +/// TimeDelta::try_seconds(3 * 3600).unwrap() +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 900) - from_hmsm(4, 5, 7, 900), +/// TimeDelta::try_seconds(-3600).unwrap() +/// ); /// assert_eq!( /// from_hmsm(3, 5, 7, 900) - from_hmsm(2, 4, 6, 800), -/// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100) +/// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::milliseconds(100) /// ); /// ``` /// @@ -1399,13 +1426,13 @@ impl Sub 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, 0, 59, 1_000) - from_hmsm(3, 0, 59, 0), TimeDelta::seconds(1)); +/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 59, 0), TimeDelta::try_seconds(1).unwrap()); /// assert_eq!(from_hmsm(3, 0, 59, 1_500) - from_hmsm(3, 0, 59, 0), /// TimeDelta::milliseconds(1500)); -/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 0, 0), TimeDelta::seconds(60)); -/// assert_eq!(from_hmsm(3, 0, 0, 0) - from_hmsm(2, 59, 59, 1_000), TimeDelta::seconds(1)); +/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 0, 0), TimeDelta::try_seconds(60).unwrap()); +/// assert_eq!(from_hmsm(3, 0, 0, 0) - from_hmsm(2, 59, 59, 1_000), TimeDelta::try_seconds(1).unwrap()); /// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(2, 59, 59, 1_000), -/// TimeDelta::seconds(61)); +/// TimeDelta::try_seconds(61).unwrap()); /// ``` impl Sub for NaiveTime { type Output = TimeDelta; diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index 94d30264bf..6ecdaed94e 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -101,8 +101,8 @@ fn test_time_add() { check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(100), hmsm(3, 5, 59, 1_400)); check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(800), hmsm(3, 6, 0, 100)); 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::try_seconds(86399).unwrap(), hmsm(3, 5, 58, 900)); // overwrap + check!(hmsm(3, 5, 59, 900), TimeDelta::try_seconds(-86399).unwrap(), hmsm(3, 6, 0, 900)); 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)); @@ -174,11 +174,11 @@ fn test_time_sub() { check!(hmsm(3, 5, 7, 900), hmsm(3, 5, 7, 900), TimeDelta::zero()); check!(hmsm(3, 5, 7, 900), hmsm(3, 5, 7, 600), TimeDelta::milliseconds(300)); - check!(hmsm(3, 5, 7, 200), hmsm(2, 4, 6, 200), TimeDelta::seconds(3600 + 60 + 1)); + check!(hmsm(3, 5, 7, 200), hmsm(2, 4, 6, 200), TimeDelta::try_seconds(3600 + 60 + 1).unwrap()); check!( hmsm(3, 5, 7, 200), hmsm(2, 4, 6, 300), - TimeDelta::seconds(3600 + 60) + TimeDelta::milliseconds(900) + TimeDelta::try_seconds(3600 + 60).unwrap() + TimeDelta::milliseconds(900) ); // treats the leap second as if it coincides with the prior non-leap second, diff --git a/src/time_delta.rs b/src/time_delta.rs index 988850804e..1258c475cc 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -622,26 +622,24 @@ mod tests { #[test] fn test_duration() { let days = |d| TimeDelta::try_days(d).unwrap(); + let seconds = |s| TimeDelta::try_seconds(s).unwrap(); - assert!(TimeDelta::seconds(1) != TimeDelta::zero()); - assert_eq!(TimeDelta::seconds(1) + TimeDelta::seconds(2), TimeDelta::seconds(3)); + assert!(seconds(1) != TimeDelta::zero()); + assert_eq!(seconds(1) + seconds(2), seconds(3)); + assert_eq!(seconds(86_399) + seconds(4), days(1) + seconds(3)); + assert_eq!(days(10) - seconds(1000), seconds(863_000)); + assert_eq!(days(10) - seconds(1_000_000), seconds(-136_000)); assert_eq!( - TimeDelta::seconds(86_399) + TimeDelta::seconds(4), - 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!( - days(2) + TimeDelta::seconds(86_399) + TimeDelta::nanoseconds(1_234_567_890), + days(2) + 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)); + assert_eq!(-(days(3) + seconds(70)), days(-4) + seconds(86_400 - 70)); let mut d = TimeDelta::default(); d += TimeDelta::try_minutes(1).unwrap(); - d -= TimeDelta::seconds(30); - assert_eq!(d, TimeDelta::seconds(30)); + d -= seconds(30); + assert_eq!(d, seconds(30)); } #[test] @@ -649,10 +647,10 @@ mod tests { assert_eq!(TimeDelta::zero().num_days(), 0); 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::try_seconds(86_399).unwrap().num_days(), 0); + assert_eq!(TimeDelta::try_seconds(86_401).unwrap().num_days(), 1); + assert_eq!(TimeDelta::try_seconds(-86_399).unwrap().num_days(), 0); + assert_eq!(TimeDelta::try_seconds(-86_401).unwrap().num_days(), -1); 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); } @@ -660,8 +658,8 @@ mod tests { #[test] fn test_duration_num_seconds() { assert_eq!(TimeDelta::zero().num_seconds(), 0); - assert_eq!(TimeDelta::seconds(1).num_seconds(), 1); - assert_eq!(TimeDelta::seconds(-1).num_seconds(), -1); + assert_eq!(TimeDelta::try_seconds(1).unwrap().num_seconds(), 1); + assert_eq!(TimeDelta::try_seconds(-1).unwrap().num_seconds(), -1); assert_eq!(TimeDelta::milliseconds(999).num_seconds(), 0); assert_eq!(TimeDelta::milliseconds(1001).num_seconds(), 1); assert_eq!(TimeDelta::milliseconds(-999).num_seconds(), 0); @@ -670,7 +668,7 @@ mod tests { #[test] fn test_duration_seconds_max_allowed() { - let duration = TimeDelta::seconds(i64::MAX / 1_000); + let duration = TimeDelta::try_seconds(i64::MAX / 1_000).unwrap(); assert_eq!(duration.num_seconds(), i64::MAX / 1_000); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -691,7 +689,7 @@ mod tests { #[test] fn test_duration_seconds_min_allowed() { - let duration = TimeDelta::seconds(i64::MIN / 1_000); // Same as -i64::MAX / 1_000 due to rounding + let duration = TimeDelta::try_seconds(i64::MIN / 1_000).unwrap(); // Same as -i64::MAX / 1_000 due to rounding assert_eq!(duration.num_seconds(), i64::MIN / 1_000); // Same as -i64::MAX / 1_000 due to rounding assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -1079,20 +1077,24 @@ mod tests { assert_eq!(TimeDelta::zero() * i32::MIN, TimeDelta::zero()); assert_eq!(TimeDelta::nanoseconds(1) * 0, TimeDelta::zero()); assert_eq!(TimeDelta::nanoseconds(1) * 1, TimeDelta::nanoseconds(1)); - assert_eq!(TimeDelta::nanoseconds(1) * 1_000_000_000, TimeDelta::seconds(1)); - assert_eq!(TimeDelta::nanoseconds(1) * -1_000_000_000, -TimeDelta::seconds(1)); - assert_eq!(-TimeDelta::nanoseconds(1) * 1_000_000_000, -TimeDelta::seconds(1)); + assert_eq!(TimeDelta::nanoseconds(1) * 1_000_000_000, TimeDelta::try_seconds(1).unwrap()); + assert_eq!(TimeDelta::nanoseconds(1) * -1_000_000_000, -TimeDelta::try_seconds(1).unwrap()); + assert_eq!(-TimeDelta::nanoseconds(1) * 1_000_000_000, -TimeDelta::try_seconds(1).unwrap()); assert_eq!( TimeDelta::nanoseconds(30) * 333_333_333, - TimeDelta::seconds(10) - TimeDelta::nanoseconds(10) + TimeDelta::try_seconds(10).unwrap() - TimeDelta::nanoseconds(10) ); assert_eq!( - (TimeDelta::nanoseconds(1) + TimeDelta::seconds(1) + TimeDelta::try_days(1).unwrap()) + (TimeDelta::nanoseconds(1) + + TimeDelta::try_seconds(1).unwrap() + + TimeDelta::try_days(1).unwrap()) * 3, - TimeDelta::nanoseconds(3) + TimeDelta::seconds(3) + TimeDelta::try_days(3).unwrap() + TimeDelta::nanoseconds(3) + + TimeDelta::try_seconds(3).unwrap() + + TimeDelta::try_days(3).unwrap() ); - assert_eq!(TimeDelta::milliseconds(1500) * -2, TimeDelta::seconds(-3)); - assert_eq!(TimeDelta::milliseconds(-1500) * 2, TimeDelta::seconds(-3)); + assert_eq!(TimeDelta::milliseconds(1500) * -2, TimeDelta::try_seconds(-3).unwrap()); + assert_eq!(TimeDelta::milliseconds(-1500) * 2, TimeDelta::try_seconds(-3).unwrap()); } #[test] @@ -1103,38 +1105,38 @@ mod tests { assert_eq!(TimeDelta::nanoseconds(123_456_789) / -1, -TimeDelta::nanoseconds(123_456_789)); assert_eq!(-TimeDelta::nanoseconds(123_456_789) / -1, TimeDelta::nanoseconds(123_456_789)); assert_eq!(-TimeDelta::nanoseconds(123_456_789) / 1, -TimeDelta::nanoseconds(123_456_789)); - assert_eq!(TimeDelta::seconds(1) / 3, TimeDelta::nanoseconds(333_333_333)); - assert_eq!(TimeDelta::seconds(4) / 3, TimeDelta::nanoseconds(1_333_333_333)); - assert_eq!(TimeDelta::seconds(-1) / 2, TimeDelta::milliseconds(-500)); - assert_eq!(TimeDelta::seconds(1) / -2, TimeDelta::milliseconds(-500)); - assert_eq!(TimeDelta::seconds(-1) / -2, TimeDelta::milliseconds(500)); - assert_eq!(TimeDelta::seconds(-4) / 3, TimeDelta::nanoseconds(-1_333_333_333)); - assert_eq!(TimeDelta::seconds(-4) / -3, TimeDelta::nanoseconds(1_333_333_333)); + assert_eq!(TimeDelta::try_seconds(1).unwrap() / 3, TimeDelta::nanoseconds(333_333_333)); + assert_eq!(TimeDelta::try_seconds(4).unwrap() / 3, TimeDelta::nanoseconds(1_333_333_333)); + assert_eq!(TimeDelta::try_seconds(-1).unwrap() / 2, TimeDelta::milliseconds(-500)); + assert_eq!(TimeDelta::try_seconds(1).unwrap() / -2, TimeDelta::milliseconds(-500)); + assert_eq!(TimeDelta::try_seconds(-1).unwrap() / -2, TimeDelta::milliseconds(500)); + assert_eq!(TimeDelta::try_seconds(-4).unwrap() / 3, TimeDelta::nanoseconds(-1_333_333_333)); + assert_eq!(TimeDelta::try_seconds(-4).unwrap() / -3, TimeDelta::nanoseconds(1_333_333_333)); } #[test] fn test_duration_sum() { - let duration_list_1 = [TimeDelta::zero(), TimeDelta::seconds(1)]; + let duration_list_1 = [TimeDelta::zero(), TimeDelta::try_seconds(1).unwrap()]; let sum_1: TimeDelta = duration_list_1.iter().sum(); - assert_eq!(sum_1, TimeDelta::seconds(1)); + assert_eq!(sum_1, TimeDelta::try_seconds(1).unwrap()); let duration_list_2 = [ TimeDelta::zero(), - TimeDelta::seconds(1), - TimeDelta::seconds(6), - TimeDelta::seconds(10), + TimeDelta::try_seconds(1).unwrap(), + TimeDelta::try_seconds(6).unwrap(), + TimeDelta::try_seconds(10).unwrap(), ]; let sum_2: TimeDelta = duration_list_2.iter().sum(); - assert_eq!(sum_2, TimeDelta::seconds(17)); + assert_eq!(sum_2, TimeDelta::try_seconds(17).unwrap()); let duration_arr = [ TimeDelta::zero(), - TimeDelta::seconds(1), - TimeDelta::seconds(6), - TimeDelta::seconds(10), + TimeDelta::try_seconds(1).unwrap(), + TimeDelta::try_seconds(6).unwrap(), + TimeDelta::try_seconds(10).unwrap(), ]; let sum_3: TimeDelta = duration_arr.into_iter().sum(); - assert_eq!(sum_3, TimeDelta::seconds(17)); + assert_eq!(sum_3, TimeDelta::try_seconds(17).unwrap()); } #[test] @@ -1142,7 +1144,7 @@ mod tests { assert_eq!(TimeDelta::zero().to_string(), "P0D"); 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::try_seconds(42).unwrap().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"); @@ -1150,7 +1152,7 @@ mod tests { (TimeDelta::try_days(7).unwrap() + TimeDelta::milliseconds(6543)).to_string(), "PT604806.543S" ); - assert_eq!(TimeDelta::seconds(-86_401).to_string(), "-PT86401S"); + assert_eq!(TimeDelta::try_seconds(-86_401).unwrap().to_string(), "-PT86401S"); assert_eq!(TimeDelta::nanoseconds(-1).to_string(), "-PT0.000000001S"); // the format specifier should have no effect on `TimeDelta` @@ -1162,20 +1164,26 @@ mod tests { #[test] fn test_to_std() { - assert_eq!(TimeDelta::seconds(1).to_std(), Ok(Duration::new(1, 0))); - assert_eq!(TimeDelta::seconds(86_401).to_std(), Ok(Duration::new(86_401, 0))); + assert_eq!(TimeDelta::try_seconds(1).unwrap().to_std(), Ok(Duration::new(1, 0))); + assert_eq!(TimeDelta::try_seconds(86_401).unwrap().to_std(), Ok(Duration::new(86_401, 0))); assert_eq!(TimeDelta::milliseconds(123).to_std(), Ok(Duration::new(0, 123_000_000))); assert_eq!(TimeDelta::milliseconds(123_765).to_std(), Ok(Duration::new(123, 765_000_000))); assert_eq!(TimeDelta::nanoseconds(777).to_std(), Ok(Duration::new(0, 777))); assert_eq!(MAX.to_std(), Ok(Duration::new(9_223_372_036_854_775, 807_000_000))); - assert_eq!(TimeDelta::seconds(-1).to_std(), Err(OutOfRangeError(()))); + assert_eq!(TimeDelta::try_seconds(-1).unwrap().to_std(), Err(OutOfRangeError(()))); assert_eq!(TimeDelta::milliseconds(-1).to_std(), Err(OutOfRangeError(()))); } #[test] fn test_from_std() { - assert_eq!(Ok(TimeDelta::seconds(1)), TimeDelta::from_std(Duration::new(1, 0))); - assert_eq!(Ok(TimeDelta::seconds(86_401)), TimeDelta::from_std(Duration::new(86_401, 0))); + assert_eq!( + Ok(TimeDelta::try_seconds(1).unwrap()), + TimeDelta::from_std(Duration::new(1, 0)) + ); + assert_eq!( + Ok(TimeDelta::try_seconds(86_401).unwrap()), + TimeDelta::from_std(Duration::new(86_401, 0)) + ); assert_eq!( Ok(TimeDelta::milliseconds(123)), TimeDelta::from_std(Duration::new(0, 123_000_000)) @@ -1202,7 +1210,7 @@ mod tests { const ONE_DAY: TimeDelta = expect!(TimeDelta::try_days(1), ""); const ONE_HOUR: TimeDelta = expect!(TimeDelta::try_hours(1), ""); const ONE_MINUTE: TimeDelta = expect!(TimeDelta::try_minutes(1), ""); - const ONE_SECOND: TimeDelta = TimeDelta::seconds(1); + const ONE_SECOND: TimeDelta = expect!(TimeDelta::try_seconds(1), ""); const ONE_MILLI: TimeDelta = TimeDelta::milliseconds(1); const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1); const ONE_NANO: TimeDelta = TimeDelta::nanoseconds(1); @@ -1225,7 +1233,7 @@ mod tests { assert!(ONE_NANO != TimeDelta::zero()); assert_eq!( combo, - TimeDelta::seconds(86400 * 7 + 86400 + 3600 + 60 + 1) + TimeDelta::try_seconds(86400 * 7 + 86400 + 3600 + 60 + 1).unwrap() + TimeDelta::nanoseconds(1 + 1_000 + 1_000_000) ); } @@ -1233,7 +1241,7 @@ mod tests { #[test] #[cfg(feature = "rkyv-validation")] fn test_rkyv_validation() { - let duration = TimeDelta::seconds(1); + let duration = TimeDelta::try_seconds(1).unwrap(); let bytes = rkyv::to_bytes::<_, 16>(&duration).unwrap(); assert_eq!(rkyv::from_bytes::(&bytes).unwrap(), duration); }