Skip to content

Commit

Permalink
Tests: replace TimeDelta::milliseconds with try_milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 6, 2024
1 parent 2bf3302 commit 2c1b0be
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn test_nanosecond_range() {
// Just beyond range
let maximum = "2262-04-11T23:47:16.854775804UTC";
let parsed: DateTime<Utc> = 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
Expand Down
42 changes: 22 additions & 20 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,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))
/// );
/// ```
Expand All @@ -513,11 +513,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)));
Expand Down Expand Up @@ -674,7 +674,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))
/// );
/// ```
Expand All @@ -697,9 +697,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)));
Expand Down Expand Up @@ -794,7 +794,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()
/// );
/// ```
///
Expand All @@ -807,11 +808,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]
Expand Down Expand Up @@ -1609,7 +1610,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,
Expand All @@ -1621,9 +1622,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(),
Expand Down Expand Up @@ -1799,7 +1800,7 @@ impl Add<Months> 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,
Expand All @@ -1811,8 +1812,8 @@ impl Add<Months> 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());
Expand Down Expand Up @@ -1967,7 +1968,8 @@ impl Sub<Months> 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()
/// );
/// ```
///
Expand All @@ -1980,11 +1982,11 @@ impl Sub<Months> 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<NaiveDateTime> for NaiveDateTime {
Expand Down
57 changes: 39 additions & 18 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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()
/// );
/// ```
///
Expand All @@ -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)),
Expand Down Expand Up @@ -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.
Expand All @@ -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));
Expand Down Expand Up @@ -1286,8 +1295,14 @@ impl Add<FixedOffset> 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.
Expand All @@ -1306,8 +1321,8 @@ impl Add<FixedOffset> 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));
/// ```
Expand Down Expand Up @@ -1396,8 +1411,14 @@ impl Sub<FixedOffset> 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()
Expand All @@ -1416,7 +1437,7 @@ impl Sub<FixedOffset> 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()
/// );
/// ```
///
Expand All @@ -1428,7 +1449,7 @@ impl Sub<FixedOffset> 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),
Expand Down
36 changes: 20 additions & 16 deletions src/naive/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,26 @@ 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));
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));
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]
Expand Down Expand Up @@ -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]
Expand Down
Loading

0 comments on commit 2c1b0be

Please sign in to comment.