Skip to content

Commit

Permalink
Don't create strange leap seconds in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 12, 2023
1 parent 03a12c7 commit 60283ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/naive/datetime/mod.rs
Expand Up @@ -1460,11 +1460,11 @@ impl Timelike for NaiveDateTime {
/// ```
/// use chrono::{NaiveDate, NaiveDateTime, Timelike};
///
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
/// let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_milli_opt(12, 34, 59, 789).unwrap();
/// assert_eq!(dt.with_nanosecond(333_333_333),
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_nano_opt(12, 34, 56, 333_333_333).unwrap()));
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_nano_opt(12, 34, 59, 333_333_333).unwrap()));
/// assert_eq!(dt.with_nanosecond(1_333_333_333), // leap second
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_nano_opt(12, 34, 56, 1_333_333_333).unwrap()));
/// Some(NaiveDate::from_ymd_opt(2015, 9, 8).unwrap().and_hms_nano_opt(12, 34, 59, 1_333_333_333).unwrap()));
/// assert_eq!(dt.with_nanosecond(2_000_000_000), None);
/// ```
#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/naive/time/mod.rs
Expand Up @@ -1022,9 +1022,9 @@ impl Timelike for NaiveTime {
///
/// ```
/// # use chrono::{NaiveTime, Timelike};
/// # let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
/// assert_eq!(dt.with_nanosecond(1_333_333_333),
/// Some(NaiveTime::from_hms_nano_opt(23, 56, 4, 1_333_333_333).unwrap()));
/// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
/// let strange_leap_second = dt.with_nanosecond(1_333_333_333).unwrap();
/// assert_eq!(strange_leap_second.nanosecond(), 1_333_333_333);
/// ```
#[inline]
fn with_nanosecond(&self, nano: u32) -> Option<NaiveTime> {
Expand Down
62 changes: 31 additions & 31 deletions src/naive/time/tests.rs
Expand Up @@ -13,12 +13,12 @@ fn test_time_from_hms_milli() {
Some(NaiveTime::from_hms_nano_opt(3, 5, 7, 777_000_000).unwrap())
);
assert_eq!(
NaiveTime::from_hms_milli_opt(3, 5, 7, 1_999),
Some(NaiveTime::from_hms_nano_opt(3, 5, 7, 1_999_000_000).unwrap())
NaiveTime::from_hms_milli_opt(3, 5, 59, 1_999),
Some(NaiveTime::from_hms_nano_opt(3, 5, 59, 1_999_000_000).unwrap())
);
assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 2_000), None);
assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, 5_000), None); // overflow check
assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 7, u32::MAX), None);
assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 59, 2_000), None);
assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 59, 5_000), None); // overflow check
assert_eq!(NaiveTime::from_hms_milli_opt(3, 5, 59, u32::MAX), None);
}

#[test]
Expand All @@ -36,12 +36,12 @@ fn test_time_from_hms_micro() {
Some(NaiveTime::from_hms_nano_opt(3, 5, 7, 777_777_000).unwrap())
);
assert_eq!(
NaiveTime::from_hms_micro_opt(3, 5, 7, 1_999_999),
Some(NaiveTime::from_hms_nano_opt(3, 5, 7, 1_999_999_000).unwrap())
NaiveTime::from_hms_micro_opt(3, 5, 59, 1_999_999),
Some(NaiveTime::from_hms_nano_opt(3, 5, 59, 1_999_999_000).unwrap())
);
assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 2_000_000), None);
assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, 5_000_000), None); // overflow check
assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 7, u32::MAX), None);
assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 59, 2_000_000), None);
assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 59, 5_000_000), None); // overflow check
assert_eq!(NaiveTime::from_hms_micro_opt(3, 5, 59, u32::MAX), None);
}

#[test]
Expand Down Expand Up @@ -94,19 +94,19 @@ fn test_time_add() {

let hmsm = |h, m, s, ms| NaiveTime::from_hms_milli_opt(h, m, s, ms).unwrap();

check!(hmsm(3, 5, 7, 900), OldDuration::zero(), hmsm(3, 5, 7, 900));
check!(hmsm(3, 5, 7, 900), OldDuration::milliseconds(100), hmsm(3, 5, 8, 0));
check!(hmsm(3, 5, 7, 1_300), OldDuration::milliseconds(-1800), hmsm(3, 5, 6, 500));
check!(hmsm(3, 5, 7, 1_300), OldDuration::milliseconds(-800), hmsm(3, 5, 7, 500));
check!(hmsm(3, 5, 7, 1_300), OldDuration::milliseconds(-100), hmsm(3, 5, 7, 1_200));
check!(hmsm(3, 5, 7, 1_300), OldDuration::milliseconds(100), hmsm(3, 5, 7, 1_400));
check!(hmsm(3, 5, 7, 1_300), OldDuration::milliseconds(800), hmsm(3, 5, 8, 100));
check!(hmsm(3, 5, 7, 1_300), OldDuration::milliseconds(1800), hmsm(3, 5, 9, 100));
check!(hmsm(3, 5, 7, 900), OldDuration::seconds(86399), hmsm(3, 5, 6, 900)); // overwrap
check!(hmsm(3, 5, 7, 900), OldDuration::seconds(-86399), hmsm(3, 5, 8, 900));
check!(hmsm(3, 5, 7, 900), OldDuration::days(12345), hmsm(3, 5, 7, 900));
check!(hmsm(3, 5, 7, 1_300), OldDuration::days(1), hmsm(3, 5, 7, 300));
check!(hmsm(3, 5, 7, 1_300), OldDuration::days(-1), hmsm(3, 5, 8, 300));
check!(hmsm(3, 5, 59, 900), OldDuration::zero(), hmsm(3, 5, 59, 900));
check!(hmsm(3, 5, 59, 900), OldDuration::milliseconds(100), hmsm(3, 6, 0, 0));
check!(hmsm(3, 5, 59, 1_300), OldDuration::milliseconds(-1800), hmsm(3, 5, 58, 500));
check!(hmsm(3, 5, 59, 1_300), OldDuration::milliseconds(-800), hmsm(3, 5, 59, 500));
check!(hmsm(3, 5, 59, 1_300), OldDuration::milliseconds(-100), hmsm(3, 5, 59, 1_200));
check!(hmsm(3, 5, 59, 1_300), OldDuration::milliseconds(100), hmsm(3, 5, 59, 1_400));
check!(hmsm(3, 5, 59, 1_300), OldDuration::milliseconds(800), hmsm(3, 6, 0, 100));
check!(hmsm(3, 5, 59, 1_300), OldDuration::milliseconds(1800), hmsm(3, 6, 1, 100));
check!(hmsm(3, 5, 59, 900), OldDuration::seconds(86399), hmsm(3, 5, 58, 900)); // overwrap
check!(hmsm(3, 5, 59, 900), OldDuration::seconds(-86399), hmsm(3, 6, 0, 900));
check!(hmsm(3, 5, 59, 900), OldDuration::days(12345), hmsm(3, 5, 59, 900));
check!(hmsm(3, 5, 59, 1_300), OldDuration::days(1), hmsm(3, 5, 59, 300));
check!(hmsm(3, 5, 59, 1_300), OldDuration::days(-1), hmsm(3, 6, 0, 300));

// regression tests for #37
check!(hmsm(0, 0, 0, 0), OldDuration::milliseconds(-990), hmsm(23, 59, 59, 10));
Expand All @@ -132,12 +132,12 @@ fn test_time_overflowing_add() {

// overflowing_add_signed with leap seconds may be counter-intuitive
assert_eq!(
hmsm(3, 4, 5, 1_678).overflowing_add_signed(OldDuration::days(1)),
(hmsm(3, 4, 5, 678), 86_400)
hmsm(3, 4, 59, 1_678).overflowing_add_signed(OldDuration::days(1)),
(hmsm(3, 4, 59, 678), 86_400)
);
assert_eq!(
hmsm(3, 4, 5, 1_678).overflowing_add_signed(OldDuration::days(-1)),
(hmsm(3, 4, 6, 678), -86_400)
hmsm(3, 4, 59, 1_678).overflowing_add_signed(OldDuration::days(-1)),
(hmsm(3, 5, 0, 678), -86_400)
);
}

Expand Down Expand Up @@ -184,14 +184,14 @@ fn test_time_sub() {

// 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, 5, 7, 200), hmsm(3, 5, 6, 1_800), OldDuration::milliseconds(400));
check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 1_800), OldDuration::milliseconds(1400));
check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 800), OldDuration::milliseconds(1400));
check!(hmsm(3, 6, 0, 200), hmsm(3, 5, 59, 1_800), OldDuration::milliseconds(400));
//check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 1_800), OldDuration::milliseconds(1400));
//check!(hmsm(3, 5, 7, 1_200), hmsm(3, 5, 6, 800), OldDuration::milliseconds(1400));

// 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) + OldDuration::milliseconds(400), hmsm(3, 5, 7, 200));
assert_eq!(hmsm(3, 5, 6, 1_800) + OldDuration::milliseconds(400), hmsm(3, 5, 7, 200));
//assert_eq!(hmsm(3, 5, 6, 1_800) + OldDuration::milliseconds(400), hmsm(3, 5, 7, 200));
}

#[test]
Expand Down
12 changes: 6 additions & 6 deletions src/round.rs
Expand Up @@ -760,16 +760,16 @@ mod tests {

#[test]
fn issue1010() {
let dt = NaiveDateTime::from_timestamp_opt(-4227854320, 1678774288).unwrap();
let span = Duration::microseconds(-7019067213869040);
let dt = NaiveDateTime::from_timestamp_opt(-4_227_854_320, 678_774_288).unwrap();
let span = Duration::microseconds(-7_019_067_213_869_040);
assert_eq!(dt.duration_trunc(span), Err(RoundingError::DurationExceedsLimit));

let dt = NaiveDateTime::from_timestamp_opt(320041586, 1920103021).unwrap();
let span = Duration::nanoseconds(-8923838508697114584);
let dt = NaiveDateTime::from_timestamp_opt(320_041_586, 920_103_021).unwrap();
let span = Duration::nanoseconds(-8_923_838_508_697_114_584);
assert_eq!(dt.duration_round(span), Err(RoundingError::DurationExceedsLimit));

let dt = NaiveDateTime::from_timestamp_opt(-2621440, 0).unwrap();
let span = Duration::nanoseconds(-9223372036854771421);
let dt = NaiveDateTime::from_timestamp_opt(-2_621_440, 0).unwrap();
let span = Duration::nanoseconds(-9_223_372_036_854_771_421);
assert_eq!(dt.duration_round(span), Err(RoundingError::DurationExceedsLimit));
}
}

0 comments on commit 60283ab

Please sign in to comment.