diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index efcff63625..24c3a0d270 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -706,7 +706,7 @@ impl NaiveDateTime { /// /// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap(); /// assert_eq!( - /// hmsm(3, 5, 7, 980).checked_add_signed(TimeDelta::milliseconds(450)), + /// hmsm(3, 5, 7, 980).checked_add_signed(TimeDelta::try_milliseconds(450).unwrap()), /// Some(hmsm(3, 5, 8, 430)) /// ); /// ``` @@ -729,11 +729,11 @@ impl NaiveDateTime { /// let leap = hmsm(3, 5, 59, 1_300); /// assert_eq!(leap.checked_add_signed(TimeDelta::zero()), /// Some(hmsm(3, 5, 59, 1_300))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::milliseconds(-500)), + /// assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(-500).unwrap()), /// Some(hmsm(3, 5, 59, 800))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::milliseconds(500)), + /// assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(500).unwrap()), /// Some(hmsm(3, 5, 59, 1_800))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::milliseconds(800)), + /// assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(800).unwrap()), /// Some(hmsm(3, 6, 0, 100))); /// assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(10).unwrap()), /// Some(hmsm(3, 6, 9, 300))); @@ -890,7 +890,7 @@ impl NaiveDateTime { /// /// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap(); /// assert_eq!( - /// hmsm(3, 5, 7, 450).checked_sub_signed(TimeDelta::milliseconds(670)), + /// hmsm(3, 5, 7, 450).checked_sub_signed(TimeDelta::try_milliseconds(670).unwrap()), /// Some(hmsm(3, 5, 6, 780)) /// ); /// ``` @@ -913,9 +913,9 @@ impl NaiveDateTime { /// let leap = hmsm(3, 5, 59, 1_300); /// assert_eq!(leap.checked_sub_signed(TimeDelta::zero()), /// Some(hmsm(3, 5, 59, 1_300))); - /// assert_eq!(leap.checked_sub_signed(TimeDelta::milliseconds(200)), + /// assert_eq!(leap.checked_sub_signed(TimeDelta::try_milliseconds(200).unwrap()), /// Some(hmsm(3, 5, 59, 1_100))); - /// assert_eq!(leap.checked_sub_signed(TimeDelta::milliseconds(500)), + /// assert_eq!(leap.checked_sub_signed(TimeDelta::try_milliseconds(500).unwrap()), /// Some(hmsm(3, 5, 59, 800))); /// assert_eq!(leap.checked_sub_signed(TimeDelta::try_seconds(60).unwrap()), /// Some(hmsm(3, 5, 0, 300))); @@ -1010,7 +1010,8 @@ impl NaiveDateTime { /// d.and_hms_milli_opt(0, 7, 6, 500) /// .unwrap() /// .signed_duration_since(d0.and_hms_opt(0, 0, 0).unwrap()), - /// TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap() + TimeDelta::milliseconds(500) + /// TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap() + /// + TimeDelta::try_milliseconds(500).unwrap() /// ); /// ``` /// @@ -1023,11 +1024,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::try_seconds(3600).unwrap() + TimeDelta::milliseconds(500) + /// TimeDelta::try_seconds(3600).unwrap() + TimeDelta::try_milliseconds(500).unwrap() /// ); /// assert_eq!( /// from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap().signed_duration_since(leap), - /// TimeDelta::try_seconds(3600).unwrap() - TimeDelta::milliseconds(500) + /// TimeDelta::try_seconds(3600).unwrap() - TimeDelta::try_milliseconds(500).unwrap() /// ); /// ``` #[must_use] @@ -1813,7 +1814,7 @@ impl Timelike for NaiveDateTime { /// ); /// /// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap(); -/// assert_eq!(hmsm(3, 5, 7, 980) + TimeDelta::milliseconds(450), hmsm(3, 5, 8, 430)); +/// assert_eq!(hmsm(3, 5, 7, 980) + TimeDelta::try_milliseconds(450).unwrap(), hmsm(3, 5, 8, 430)); /// ``` /// /// Leap seconds are handled, @@ -1825,9 +1826,9 @@ impl Timelike for NaiveDateTime { /// # 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::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::try_milliseconds(-500).unwrap(), hmsm(3, 5, 59, 800)); +/// assert_eq!(leap + TimeDelta::try_milliseconds(500).unwrap(), hmsm(3, 5, 59, 1_800)); +/// assert_eq!(leap + TimeDelta::try_milliseconds(800).unwrap(), 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(), @@ -2003,7 +2004,7 @@ impl Add for NaiveDateTime { /// ); /// /// let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap(); -/// assert_eq!(hmsm(3, 5, 7, 450) - TimeDelta::milliseconds(670), hmsm(3, 5, 6, 780)); +/// assert_eq!(hmsm(3, 5, 7, 450) - TimeDelta::try_milliseconds(670).unwrap(), hmsm(3, 5, 6, 780)); /// ``` /// /// Leap seconds are handled, @@ -2015,8 +2016,8 @@ impl Add for NaiveDateTime { /// # 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::milliseconds(200), hmsm(3, 5, 59, 1_100)); -/// assert_eq!(leap - TimeDelta::milliseconds(500), hmsm(3, 5, 59, 800)); +/// assert_eq!(leap - TimeDelta::try_milliseconds(200).unwrap(), hmsm(3, 5, 59, 1_100)); +/// assert_eq!(leap - TimeDelta::try_milliseconds(500).unwrap(), hmsm(3, 5, 59, 800)); /// 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()); @@ -2171,7 +2172,8 @@ impl Sub for NaiveDateTime { /// 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::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap() + TimeDelta::milliseconds(500) +/// TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap() +/// + TimeDelta::try_milliseconds(500).unwrap() /// ); /// ``` /// @@ -2184,11 +2186,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::try_seconds(3600).unwrap() + TimeDelta::milliseconds(500) +/// TimeDelta::try_seconds(3600).unwrap() + TimeDelta::try_milliseconds(500).unwrap() /// ); /// assert_eq!( /// from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap() - leap, -/// TimeDelta::try_seconds(3600).unwrap() - TimeDelta::milliseconds(500) +/// TimeDelta::try_seconds(3600).unwrap() - TimeDelta::try_milliseconds(500).unwrap() /// ); /// ``` impl Sub for NaiveDateTime { diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 5ca259f971..f4fbe757fb 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -446,7 +446,7 @@ fn test_nanosecond_range() { // Just beyond range let maximum = "2262-04-11T23:47:16.854775804"; let parsed: NaiveDateTime = maximum.parse().unwrap(); - let beyond_max = parsed + TimeDelta::milliseconds(300); + let beyond_max = parsed + TimeDelta::try_milliseconds(300).unwrap(); assert!(beyond_max.timestamp_nanos_opt().is_none()); // Far beyond range diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 778d415e7b..23b1cb0e55 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -696,11 +696,11 @@ impl NaiveTime { /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)), TimeDelta::zero()); /// assert_eq!( /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)), - /// TimeDelta::milliseconds(25) + /// TimeDelta::try_milliseconds(25).unwrap() /// ); /// assert_eq!( /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)), - /// TimeDelta::milliseconds(975) + /// TimeDelta::try_milliseconds(975).unwrap() /// ); /// assert_eq!( /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)), @@ -720,7 +720,7 @@ impl NaiveTime { /// ); /// assert_eq!( /// since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)), - /// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::milliseconds(100) + /// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::try_milliseconds(100).unwrap() /// ); /// ``` /// @@ -734,7 +734,7 @@ impl NaiveTime { /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)), /// TimeDelta::try_seconds(1).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)), - /// TimeDelta::milliseconds(1500)); + /// TimeDelta::try_milliseconds(1500).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)), /// TimeDelta::try_seconds(60).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)), @@ -1166,9 +1166,18 @@ impl Timelike for NaiveTime { /// 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)); -/// assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(280), from_hmsm(3, 5, 8, 230)); -/// assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(-980), from_hmsm(3, 5, 6, 970)); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 0) + TimeDelta::try_milliseconds(80).unwrap(), +/// from_hmsm(3, 5, 7, 80) +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 950) + TimeDelta::try_milliseconds(280).unwrap(), +/// from_hmsm(3, 5, 8, 230) +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 950) + TimeDelta::try_milliseconds(-980).unwrap(), +/// from_hmsm(3, 5, 6, 970) +/// ); /// ``` /// /// The addition wraps around. @@ -1188,9 +1197,9 @@ impl Timelike for 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::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::try_milliseconds(-500).unwrap(), from_hmsm(3, 5, 59, 800)); +/// assert_eq!(leap + TimeDelta::try_milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 1_800)); +/// assert_eq!(leap + TimeDelta::try_milliseconds(800).unwrap(), 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)); @@ -1286,8 +1295,14 @@ impl Add for NaiveTime { /// 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)); -/// assert_eq!(from_hmsm(3, 5, 7, 950) - TimeDelta::milliseconds(280), from_hmsm(3, 5, 7, 670)); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 0) - TimeDelta::try_milliseconds(80).unwrap(), +/// from_hmsm(3, 5, 6, 920) +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 950) - TimeDelta::try_milliseconds(280).unwrap(), +/// from_hmsm(3, 5, 7, 670) +/// ); /// ``` /// /// The subtraction wraps around. @@ -1306,8 +1321,8 @@ impl Add for 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::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::try_milliseconds(200).unwrap(), from_hmsm(3, 5, 59, 1_100)); +/// assert_eq!(leap - TimeDelta::try_milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 800)); /// 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)); /// ``` @@ -1396,8 +1411,14 @@ impl Sub 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, 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, 7, 875), +/// TimeDelta::try_milliseconds(25).unwrap() +/// ); +/// assert_eq!( +/// from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 6, 925), +/// TimeDelta::try_milliseconds(975).unwrap() +/// ); /// assert_eq!( /// from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 0, 900), /// TimeDelta::try_seconds(7).unwrap() @@ -1416,7 +1437,7 @@ impl Sub for NaiveTime { /// ); /// assert_eq!( /// from_hmsm(3, 5, 7, 900) - from_hmsm(2, 4, 6, 800), -/// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::milliseconds(100) +/// TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::try_milliseconds(100).unwrap() /// ); /// ``` /// @@ -1428,7 +1449,7 @@ impl Sub for 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::try_seconds(1).unwrap()); /// assert_eq!(from_hmsm(3, 0, 59, 1_500) - from_hmsm(3, 0, 59, 0), -/// TimeDelta::milliseconds(1500)); +/// TimeDelta::try_milliseconds(1500).unwrap()); /// 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), diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index 6ecdaed94e..a8754aebd6 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -94,13 +94,17 @@ fn test_time_add() { let hmsm = |h, m, s, ms| NaiveTime::from_hms_milli_opt(h, m, s, ms).unwrap(); check!(hmsm(3, 5, 59, 900), TimeDelta::zero(), hmsm(3, 5, 59, 900)); - check!(hmsm(3, 5, 59, 900), TimeDelta::milliseconds(100), hmsm(3, 6, 0, 0)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(-1800), hmsm(3, 5, 58, 500)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(-800), hmsm(3, 5, 59, 500)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(-100), hmsm(3, 5, 59, 1_200)); - 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::try_milliseconds(100).unwrap(), hmsm(3, 6, 0, 0)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_milliseconds(-1800).unwrap(), hmsm(3, 5, 58, 500)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_milliseconds(-800).unwrap(), hmsm(3, 5, 59, 500)); + check!( + hmsm(3, 5, 59, 1_300), + TimeDelta::try_milliseconds(-100).unwrap(), + hmsm(3, 5, 59, 1_200) + ); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_milliseconds(100).unwrap(), hmsm(3, 5, 59, 1_400)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_milliseconds(800).unwrap(), hmsm(3, 6, 0, 100)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::try_milliseconds(1800).unwrap(), hmsm(3, 6, 1, 100)); 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)); @@ -108,8 +112,8 @@ fn test_time_add() { 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)); - check!(hmsm(0, 0, 0, 0), TimeDelta::milliseconds(-9990), hmsm(23, 59, 50, 10)); + check!(hmsm(0, 0, 0, 0), TimeDelta::try_milliseconds(-990).unwrap(), hmsm(23, 59, 59, 10)); + check!(hmsm(0, 0, 0, 0), TimeDelta::try_milliseconds(-9990).unwrap(), hmsm(23, 59, 50, 10)); } #[test] @@ -173,24 +177,24 @@ fn test_time_sub() { let hmsm = |h, m, s, ms| NaiveTime::from_hms_milli_opt(h, m, s, ms).unwrap(); 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, 900), hmsm(3, 5, 7, 600), TimeDelta::try_milliseconds(300).unwrap()); 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::try_seconds(3600 + 60).unwrap() + TimeDelta::milliseconds(900) + TimeDelta::try_seconds(3600 + 60).unwrap() + TimeDelta::try_milliseconds(900).unwrap() ); // treats the leap second as if it coincides with the prior non-leap second, // as required by `time1 - time2 = duration` and `time2 - time1 = -duration` equivalence. - check!(hmsm(3, 6, 0, 200), hmsm(3, 5, 59, 1_800), TimeDelta::milliseconds(400)); - //check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 1_800), TimeDelta::milliseconds(1400)); - //check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 800), TimeDelta::milliseconds(1400)); + check!(hmsm(3, 6, 0, 200), hmsm(3, 5, 59, 1_800), TimeDelta::try_milliseconds(400).unwrap()); + //check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 1_800), TimeDelta::try_milliseconds(1400).unwrap()); + //check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 800), TimeDelta::try_milliseconds(1400).unwrap()); // additional equality: `time1 + duration = time2` is equivalent to // `time2 - time1 = duration` IF AND ONLY IF `time2` represents a non-leap second. - assert_eq!(hmsm(3, 5, 6, 800) + TimeDelta::milliseconds(400), hmsm(3, 5, 7, 200)); - //assert_eq!(hmsm(3, 5, 6, 1_800) + TimeDelta::milliseconds(400), hmsm(3, 5, 7, 200)); + assert_eq!(hmsm(3, 5, 6, 800) + TimeDelta::try_milliseconds(400).unwrap(), hmsm(3, 5, 7, 200)); + //assert_eq!(hmsm(3, 5, 6, 1_800) + TimeDelta::try_milliseconds(400).unwrap(), hmsm(3, 5, 7, 200)); } #[test] diff --git a/src/round.rs b/src/round.rs index f707d5e0a0..865025ebf5 100644 --- a/src/round.rs +++ b/src/round.rs @@ -127,7 +127,7 @@ pub trait DurationRound: Sized { /// .and_local_timezone(Utc) /// .unwrap(); /// assert_eq!( - /// dt.duration_round(TimeDelta::milliseconds(10)).unwrap().to_string(), + /// dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(), /// "2018-01-11 12:00:00.150 UTC" /// ); /// assert_eq!( @@ -149,7 +149,7 @@ pub trait DurationRound: Sized { /// .and_local_timezone(Utc) /// .unwrap(); /// assert_eq!( - /// dt.duration_trunc(TimeDelta::milliseconds(10)).unwrap().to_string(), + /// dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(), /// "2018-01-11 12:00:00.150 UTC" /// ); /// assert_eq!( @@ -462,7 +462,7 @@ mod tests { ); assert_eq!( - dt.duration_round(TimeDelta::milliseconds(10)).unwrap().to_string(), + dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(), "2016-12-31 23:59:59.180 UTC" ); @@ -553,7 +553,7 @@ mod tests { ); assert_eq!( - dt.duration_round(TimeDelta::milliseconds(10)).unwrap().to_string(), + dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(), "2016-12-31 23:59:59.180" ); @@ -625,7 +625,7 @@ mod tests { .unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::milliseconds(10)).unwrap().to_string(), + dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(), "2016-12-31 23:59:59.170 UTC" ); @@ -710,7 +710,7 @@ mod tests { .naive_utc(); assert_eq!( - dt.duration_trunc(TimeDelta::milliseconds(10)).unwrap().to_string(), + dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(), "2016-12-31 23:59:59.170" ); diff --git a/src/time_delta.rs b/src/time_delta.rs index 1258c475cc..b279995e25 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -227,6 +227,7 @@ impl TimeDelta { /// Panics when the `TimeDelta` would be out of bounds, i.e. when `milliseconds` is more than /// `i64::MAX` or less than `-i64::MAX`. Notably, this is not the same as `i64::MIN`. #[inline] + #[deprecated] pub const fn milliseconds(milliseconds: i64) -> TimeDelta { expect!(TimeDelta::try_milliseconds(milliseconds), "TimeDelta::milliseconds out of bounds") } @@ -660,10 +661,10 @@ mod tests { assert_eq!(TimeDelta::zero().num_seconds(), 0); 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); - assert_eq!(TimeDelta::milliseconds(-1001).num_seconds(), -1); + assert_eq!(TimeDelta::try_milliseconds(999).unwrap().num_seconds(), 0); + assert_eq!(TimeDelta::try_milliseconds(1001).unwrap().num_seconds(), 1); + assert_eq!(TimeDelta::try_milliseconds(-999).unwrap().num_seconds(), 0); + assert_eq!(TimeDelta::try_milliseconds(-1001).unwrap().num_seconds(), -1); } #[test] @@ -711,8 +712,8 @@ mod tests { #[test] fn test_duration_num_milliseconds() { assert_eq!(TimeDelta::zero().num_milliseconds(), 0); - assert_eq!(TimeDelta::milliseconds(1).num_milliseconds(), 1); - assert_eq!(TimeDelta::milliseconds(-1).num_milliseconds(), -1); + assert_eq!(TimeDelta::try_milliseconds(1).unwrap().num_milliseconds(), 1); + assert_eq!(TimeDelta::try_milliseconds(-1).unwrap().num_milliseconds(), -1); assert_eq!(TimeDelta::microseconds(999).num_milliseconds(), 0); assert_eq!(TimeDelta::microseconds(1001).num_milliseconds(), 1); assert_eq!(TimeDelta::microseconds(-999).num_milliseconds(), 0); @@ -723,7 +724,7 @@ mod tests { fn test_duration_milliseconds_max_allowed() { // The maximum number of milliseconds acceptable through the constructor is // equal to the number that can be stored in a TimeDelta. - let duration = TimeDelta::milliseconds(i64::MAX); + let duration = TimeDelta::try_milliseconds(i64::MAX).unwrap(); assert_eq!(duration.num_milliseconds(), i64::MAX); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -735,8 +736,9 @@ mod tests { fn test_duration_milliseconds_max_overflow() { // Here we ensure that trying to add one millisecond to the maximum storable // value will fail. - assert!(TimeDelta::milliseconds(i64::MAX) - .checked_add(&TimeDelta::milliseconds(1)) + assert!(TimeDelta::try_milliseconds(i64::MAX) + .unwrap() + .checked_add(&TimeDelta::try_milliseconds(1).unwrap()) .is_none()); } @@ -745,7 +747,7 @@ mod tests { // The minimum number of milliseconds acceptable through the constructor is // not equal to the number that can be stored in a TimeDelta - there is a // difference of one (i64::MIN vs -i64::MAX). - let duration = TimeDelta::milliseconds(-i64::MAX); + let duration = TimeDelta::try_milliseconds(-i64::MAX).unwrap(); assert_eq!(duration.num_milliseconds(), -i64::MAX); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -757,8 +759,9 @@ mod tests { fn test_duration_milliseconds_min_underflow() { // Here we ensure that trying to subtract one millisecond from the minimum // storable value will fail. - assert!(TimeDelta::milliseconds(-i64::MAX) - .checked_sub(&TimeDelta::milliseconds(1)) + assert!(TimeDelta::try_milliseconds(-i64::MAX) + .unwrap() + .checked_sub(&TimeDelta::try_milliseconds(1).unwrap()) .is_none()); } @@ -816,7 +819,7 @@ mod tests { // microseconds by creating a TimeDelta with the maximum number of // milliseconds and then checking that the number of microseconds matches // the storage limit. - let duration = TimeDelta::milliseconds(i64::MAX); + let duration = TimeDelta::try_milliseconds(i64::MAX).unwrap(); assert!(duration.num_microseconds().is_none()); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -835,7 +838,8 @@ mod tests { ); // Here we ensure that trying to add one microsecond to the maximum storable // value will fail. - assert!(TimeDelta::milliseconds(i64::MAX) + assert!(TimeDelta::try_milliseconds(i64::MAX) + .unwrap() .checked_add(&TimeDelta::microseconds(1)) .is_none()); } @@ -854,7 +858,7 @@ mod tests { // microseconds by creating a TimeDelta with the minimum number of // milliseconds and then checking that the number of microseconds matches // the storage limit. - let duration = TimeDelta::milliseconds(-i64::MAX); + let duration = TimeDelta::try_milliseconds(-i64::MAX).unwrap(); assert!(duration.num_microseconds().is_none()); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -873,7 +877,8 @@ mod tests { ); // Here we ensure that trying to subtract one microsecond from the minimum // storable value will fail. - assert!(TimeDelta::milliseconds(-i64::MAX) + assert!(TimeDelta::try_milliseconds(-i64::MAX) + .unwrap() .checked_sub(&TimeDelta::microseconds(1)) .is_none()); } @@ -917,7 +922,7 @@ mod tests { // Here we create a TimeDelta with the maximum possible number of nanoseconds // by creating a TimeDelta with the maximum number of milliseconds and then // checking that the number of nanoseconds matches the storage limit. - let duration = TimeDelta::milliseconds(i64::MAX); + let duration = TimeDelta::try_milliseconds(i64::MAX).unwrap(); assert!(duration.num_nanoseconds().is_none()); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -937,7 +942,8 @@ mod tests { ); // Here we ensure that trying to add one nanosecond to the maximum storable // value will fail. - assert!(TimeDelta::milliseconds(i64::MAX) + assert!(TimeDelta::try_milliseconds(i64::MAX) + .unwrap() .checked_add(&TimeDelta::nanoseconds(1)) .is_none()); } @@ -956,7 +962,7 @@ mod tests { // Here we create a TimeDelta with the minimum possible number of nanoseconds // by creating a TimeDelta with the minimum number of milliseconds and then // checking that the number of nanoseconds matches the storage limit. - let duration = TimeDelta::milliseconds(-i64::MAX); + let duration = TimeDelta::try_milliseconds(-i64::MAX).unwrap(); assert!(duration.num_nanoseconds().is_none()); assert_eq!( duration.secs as i128 * 1_000_000_000 + duration.nanos as i128, @@ -976,7 +982,8 @@ mod tests { ); // Here we ensure that trying to subtract one nanosecond from the minimum // storable value will fail. - assert!(TimeDelta::milliseconds(-i64::MAX) + assert!(TimeDelta::try_milliseconds(-i64::MAX) + .unwrap() .checked_sub(&TimeDelta::nanoseconds(1)) .is_none()); } @@ -987,7 +994,7 @@ mod tests { MAX.secs as i128 * 1_000_000_000 + MAX.nanos as i128, i64::MAX as i128 * 1_000_000 ); - assert_eq!(MAX, TimeDelta::milliseconds(i64::MAX)); + assert_eq!(MAX, TimeDelta::try_milliseconds(i64::MAX).unwrap()); assert_eq!(MAX.num_milliseconds(), i64::MAX); assert_eq!(MAX.num_microseconds(), None); assert_eq!(MAX.num_nanoseconds(), None); @@ -999,7 +1006,7 @@ mod tests { MIN.secs as i128 * 1_000_000_000 + MIN.nanos as i128, -i64::MAX as i128 * 1_000_000 ); - assert_eq!(MIN, TimeDelta::milliseconds(-i64::MAX)); + assert_eq!(MIN, TimeDelta::try_milliseconds(-i64::MAX).unwrap()); assert_eq!(MIN.num_milliseconds(), -i64::MAX); assert_eq!(MIN.num_microseconds(), None); assert_eq!(MIN.num_nanoseconds(), None); @@ -1007,67 +1014,65 @@ mod tests { #[test] fn test_duration_ord() { - assert!(TimeDelta::milliseconds(1) < TimeDelta::milliseconds(2)); - assert!(TimeDelta::milliseconds(2) > TimeDelta::milliseconds(1)); - assert!(TimeDelta::milliseconds(-1) > TimeDelta::milliseconds(-2)); - assert!(TimeDelta::milliseconds(-2) < TimeDelta::milliseconds(-1)); - assert!(TimeDelta::milliseconds(-1) < TimeDelta::milliseconds(1)); - assert!(TimeDelta::milliseconds(1) > TimeDelta::milliseconds(-1)); - assert!(TimeDelta::milliseconds(0) < TimeDelta::milliseconds(1)); - assert!(TimeDelta::milliseconds(0) > TimeDelta::milliseconds(-1)); - assert!(TimeDelta::milliseconds(1_001) < TimeDelta::milliseconds(1_002)); - assert!(TimeDelta::milliseconds(-1_001) > TimeDelta::milliseconds(-1_002)); + let milliseconds = |ms| TimeDelta::try_milliseconds(ms).unwrap(); + + assert!(milliseconds(1) < milliseconds(2)); + assert!(milliseconds(2) > milliseconds(1)); + assert!(milliseconds(-1) > milliseconds(-2)); + assert!(milliseconds(-2) < milliseconds(-1)); + assert!(milliseconds(-1) < milliseconds(1)); + assert!(milliseconds(1) > milliseconds(-1)); + assert!(milliseconds(0) < milliseconds(1)); + assert!(milliseconds(0) > milliseconds(-1)); + assert!(milliseconds(1_001) < milliseconds(1_002)); + assert!(milliseconds(-1_001) > milliseconds(-1_002)); assert!(TimeDelta::nanoseconds(1_234_567_890) < TimeDelta::nanoseconds(1_234_567_891)); assert!(TimeDelta::nanoseconds(-1_234_567_890) > TimeDelta::nanoseconds(-1_234_567_891)); - assert!(TimeDelta::milliseconds(i64::MAX) > TimeDelta::milliseconds(i64::MAX - 1)); - assert!(TimeDelta::milliseconds(-i64::MAX) < TimeDelta::milliseconds(-i64::MAX + 1)); + assert!(milliseconds(i64::MAX) > milliseconds(i64::MAX - 1)); + assert!(milliseconds(-i64::MAX) < milliseconds(-i64::MAX + 1)); } #[test] fn test_duration_checked_ops() { + let milliseconds = |ms| TimeDelta::try_milliseconds(ms).unwrap(); + assert_eq!( - TimeDelta::milliseconds(i64::MAX).checked_add(&TimeDelta::milliseconds(0)), - Some(TimeDelta::milliseconds(i64::MAX)) + milliseconds(i64::MAX).checked_add(&milliseconds(0)), + Some(milliseconds(i64::MAX)) ); assert_eq!( - TimeDelta::milliseconds(i64::MAX - 1).checked_add(&TimeDelta::microseconds(999)), - Some(TimeDelta::milliseconds(i64::MAX - 2) + TimeDelta::microseconds(1999)) + milliseconds(i64::MAX - 1).checked_add(&TimeDelta::microseconds(999)), + Some(milliseconds(i64::MAX - 2) + TimeDelta::microseconds(1999)) ); - assert!(TimeDelta::milliseconds(i64::MAX) - .checked_add(&TimeDelta::microseconds(1000)) - .is_none()); - assert!(TimeDelta::milliseconds(i64::MAX) - .checked_add(&TimeDelta::nanoseconds(1)) - .is_none()); + assert!(milliseconds(i64::MAX).checked_add(&TimeDelta::microseconds(1000)).is_none()); + assert!(milliseconds(i64::MAX).checked_add(&TimeDelta::nanoseconds(1)).is_none()); assert_eq!( - TimeDelta::milliseconds(-i64::MAX).checked_sub(&TimeDelta::milliseconds(0)), - Some(TimeDelta::milliseconds(-i64::MAX)) + milliseconds(-i64::MAX).checked_sub(&milliseconds(0)), + Some(milliseconds(-i64::MAX)) ); assert_eq!( - TimeDelta::milliseconds(-i64::MAX + 1).checked_sub(&TimeDelta::microseconds(999)), - Some(TimeDelta::milliseconds(-i64::MAX + 2) - TimeDelta::microseconds(1999)) + milliseconds(-i64::MAX + 1).checked_sub(&TimeDelta::microseconds(999)), + Some(milliseconds(-i64::MAX + 2) - TimeDelta::microseconds(1999)) ); - assert!(TimeDelta::milliseconds(-i64::MAX) - .checked_sub(&TimeDelta::milliseconds(1)) - .is_none()); - assert!(TimeDelta::milliseconds(-i64::MAX) - .checked_sub(&TimeDelta::nanoseconds(1)) - .is_none()); + assert!(milliseconds(-i64::MAX).checked_sub(&milliseconds(1)).is_none()); + assert!(milliseconds(-i64::MAX).checked_sub(&TimeDelta::nanoseconds(1)).is_none()); } #[test] fn test_duration_abs() { - assert_eq!(TimeDelta::milliseconds(1300).abs(), TimeDelta::milliseconds(1300)); - assert_eq!(TimeDelta::milliseconds(1000).abs(), TimeDelta::milliseconds(1000)); - assert_eq!(TimeDelta::milliseconds(300).abs(), TimeDelta::milliseconds(300)); - assert_eq!(TimeDelta::milliseconds(0).abs(), TimeDelta::milliseconds(0)); - assert_eq!(TimeDelta::milliseconds(-300).abs(), TimeDelta::milliseconds(300)); - assert_eq!(TimeDelta::milliseconds(-700).abs(), TimeDelta::milliseconds(700)); - assert_eq!(TimeDelta::milliseconds(-1000).abs(), TimeDelta::milliseconds(1000)); - assert_eq!(TimeDelta::milliseconds(-1300).abs(), TimeDelta::milliseconds(1300)); - assert_eq!(TimeDelta::milliseconds(-1700).abs(), TimeDelta::milliseconds(1700)); - assert_eq!(TimeDelta::milliseconds(-i64::MAX).abs(), TimeDelta::milliseconds(i64::MAX)); + let milliseconds = |ms| TimeDelta::try_milliseconds(ms).unwrap(); + + assert_eq!(milliseconds(1300).abs(), milliseconds(1300)); + assert_eq!(milliseconds(1000).abs(), milliseconds(1000)); + assert_eq!(milliseconds(300).abs(), milliseconds(300)); + assert_eq!(milliseconds(0).abs(), milliseconds(0)); + assert_eq!(milliseconds(-300).abs(), milliseconds(300)); + assert_eq!(milliseconds(-700).abs(), milliseconds(700)); + assert_eq!(milliseconds(-1000).abs(), milliseconds(1000)); + assert_eq!(milliseconds(-1300).abs(), milliseconds(1300)); + assert_eq!(milliseconds(-1700).abs(), milliseconds(1700)); + assert_eq!(milliseconds(-i64::MAX).abs(), milliseconds(i64::MAX)); } #[test] @@ -1093,8 +1098,14 @@ mod tests { + TimeDelta::try_seconds(3).unwrap() + TimeDelta::try_days(3).unwrap() ); - assert_eq!(TimeDelta::milliseconds(1500) * -2, TimeDelta::try_seconds(-3).unwrap()); - assert_eq!(TimeDelta::milliseconds(-1500) * 2, TimeDelta::try_seconds(-3).unwrap()); + assert_eq!( + TimeDelta::try_milliseconds(1500).unwrap() * -2, + TimeDelta::try_seconds(-3).unwrap() + ); + assert_eq!( + TimeDelta::try_milliseconds(-1500).unwrap() * 2, + TimeDelta::try_seconds(-3).unwrap() + ); } #[test] @@ -1107,9 +1118,18 @@ mod tests { assert_eq!(-TimeDelta::nanoseconds(123_456_789) / 1, -TimeDelta::nanoseconds(123_456_789)); 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(-1).unwrap() / 2, + TimeDelta::try_milliseconds(-500).unwrap() + ); + assert_eq!( + TimeDelta::try_seconds(1).unwrap() / -2, + TimeDelta::try_milliseconds(-500).unwrap() + ); + assert_eq!( + TimeDelta::try_seconds(-1).unwrap() / -2, + TimeDelta::try_milliseconds(500).unwrap() + ); 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)); } @@ -1145,11 +1165,12 @@ mod tests { assert_eq!(TimeDelta::try_days(42).unwrap().to_string(), "PT3628800S"); assert_eq!(TimeDelta::try_days(-42).unwrap().to_string(), "-PT3628800S"); assert_eq!(TimeDelta::try_seconds(42).unwrap().to_string(), "PT42S"); - assert_eq!(TimeDelta::milliseconds(42).to_string(), "PT0.042S"); + assert_eq!(TimeDelta::try_milliseconds(42).unwrap().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::try_days(7).unwrap() + TimeDelta::milliseconds(6543)).to_string(), + (TimeDelta::try_days(7).unwrap() + TimeDelta::try_milliseconds(6543).unwrap()) + .to_string(), "PT604806.543S" ); assert_eq!(TimeDelta::try_seconds(-86_401).unwrap().to_string(), "-PT86401S"); @@ -1157,7 +1178,10 @@ mod tests { // the format specifier should have no effect on `TimeDelta` assert_eq!( - format!("{:30}", TimeDelta::try_days(1).unwrap() + TimeDelta::milliseconds(2345)), + format!( + "{:30}", + TimeDelta::try_days(1).unwrap() + TimeDelta::try_milliseconds(2345).unwrap() + ), "PT86402.345S" ); } @@ -1166,12 +1190,18 @@ mod tests { fn test_to_std() { 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::try_milliseconds(123).unwrap().to_std(), + Ok(Duration::new(0, 123_000_000)) + ); + assert_eq!( + TimeDelta::try_milliseconds(123_765).unwrap().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::try_seconds(-1).unwrap().to_std(), Err(OutOfRangeError(()))); - assert_eq!(TimeDelta::milliseconds(-1).to_std(), Err(OutOfRangeError(()))); + assert_eq!(TimeDelta::try_milliseconds(-1).unwrap().to_std(), Err(OutOfRangeError(()))); } #[test] @@ -1185,11 +1215,11 @@ mod tests { TimeDelta::from_std(Duration::new(86_401, 0)) ); assert_eq!( - Ok(TimeDelta::milliseconds(123)), + Ok(TimeDelta::try_milliseconds(123).unwrap()), TimeDelta::from_std(Duration::new(0, 123_000_000)) ); assert_eq!( - Ok(TimeDelta::milliseconds(123_765)), + Ok(TimeDelta::try_milliseconds(123_765).unwrap()), TimeDelta::from_std(Duration::new(123, 765_000_000)) ); assert_eq!(Ok(TimeDelta::nanoseconds(777)), TimeDelta::from_std(Duration::new(0, 777))); @@ -1211,7 +1241,7 @@ mod tests { const ONE_HOUR: TimeDelta = expect!(TimeDelta::try_hours(1), ""); const ONE_MINUTE: TimeDelta = expect!(TimeDelta::try_minutes(1), ""); const ONE_SECOND: TimeDelta = expect!(TimeDelta::try_seconds(1), ""); - const ONE_MILLI: TimeDelta = TimeDelta::milliseconds(1); + const ONE_MILLI: TimeDelta = expect!(TimeDelta::try_milliseconds(1), ""); const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1); const ONE_NANO: TimeDelta = TimeDelta::nanoseconds(1); let combo: TimeDelta = ONE_WEEK