Skip to content

Commit

Permalink
Tests: replace TimeDelta::seconds with try_seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 6, 2024
1 parent f93508f commit 2bf3302
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 163 deletions.
12 changes: 6 additions & 6 deletions src/datetime/tests.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand Down
16 changes: 6 additions & 10 deletions src/lib.rs
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/naive/date/mod.rs
Expand Up @@ -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));
Expand Down Expand Up @@ -2056,8 +2059,11 @@ impl Sub<Days> 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));
Expand Down
4 changes: 2 additions & 2 deletions src/naive/date/tests.rs
Expand Up @@ -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)));
Expand Down
86 changes: 52 additions & 34 deletions src/naive/datetime/mod.rs
Expand Up @@ -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())
/// );
///
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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())
/// );
///
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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
Expand All @@ -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)
/// );
/// ```
///
Expand All @@ -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]
Expand Down Expand Up @@ -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!(
Expand All @@ -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());
/// ```
Expand Down Expand Up @@ -1768,11 +1786,11 @@ impl Add<Months> 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!(
Expand All @@ -1792,10 +1810,10 @@ impl Add<Months> 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());
/// ```
Expand Down Expand Up @@ -1942,14 +1960,14 @@ impl Sub<Months> 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)
/// );
/// ```
///
Expand All @@ -1962,11 +1980,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::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<NaiveDateTime> for NaiveDateTime {
Expand Down
27 changes: 14 additions & 13 deletions src/naive/datetime/tests.rs
Expand Up @@ -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`.
Expand All @@ -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);
}

Expand All @@ -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()
);
}

Expand Down

0 comments on commit 2bf3302

Please sign in to comment.