From f2d1bc3d9a8299dcc38dd056d9e1ccf68cd1bea3 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Thu, 14 Mar 2024 07:41:34 +0100 Subject: [PATCH 1/6] Format doctests --- src/naive/datetime/mod.rs | 10 ++-------- src/naive/time/mod.rs | 5 +---- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 170e33dd40..82477e6759 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -963,10 +963,7 @@ impl NaiveDateTime { /// use chrono::{Error, NaiveDate}; /// /// let dt = NaiveDate::from_ymd(2015, 9, 8)?.and_hms_milli(12, 34, 56, 789)?; - /// assert_eq!( - /// dt.with_minute(45), - /// NaiveDate::from_ymd(2015, 9, 8)?.and_hms_milli(12, 45, 56, 789) - /// ); + /// assert_eq!(dt.with_minute(45), NaiveDate::from_ymd(2015, 9, 8)?.and_hms_milli(12, 45, 56, 789)); /// assert_eq!(dt.with_minute(60), Err(Error::InvalidArgument)); /// # Ok::<(), chrono::Error>(()) /// ``` @@ -992,10 +989,7 @@ impl NaiveDateTime { /// use chrono::{Error, NaiveDate}; /// /// let dt = NaiveDate::from_ymd(2015, 9, 8)?.and_hms_milli(12, 34, 56, 789)?; - /// assert_eq!( - /// dt.with_second(17), - /// NaiveDate::from_ymd(2015, 9, 8)?.and_hms_milli(12, 34, 17, 789) - /// ); + /// assert_eq!(dt.with_second(17), NaiveDate::from_ymd(2015, 9, 8)?.and_hms_milli(12, 34, 17, 789)); /// assert_eq!(dt.with_second(60), Err(Error::InvalidArgument)); /// # Ok::<(), chrono::Error>(()) /// ``` diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 6ddfd5660f..b75ab3bad6 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -901,10 +901,7 @@ impl NaiveTime { /// use chrono::{Error, NaiveTime}; /// /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678)?; - /// assert_eq!( - /// dt.with_nanosecond(333_333_333), - /// NaiveTime::from_hms_nano(23, 56, 4, 333_333_333) - /// ); + /// assert_eq!(dt.with_nanosecond(333_333_333), NaiveTime::from_hms_nano(23, 56, 4, 333_333_333)); /// assert_eq!(dt.with_nanosecond(2_000_000_000), Err(Error::InvalidArgument)); /// # Ok::<(), chrono::Error>(()) /// ``` From d6d667d46b990b866728c2bf0e1843ca1b051b28 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 13 Mar 2024 19:04:58 +0100 Subject: [PATCH 2/6] Make `TimeDelta::weeks` infallible --- src/round.rs | 8 ++++---- src/time_delta.rs | 26 ++++++++++++++++---------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/round.rs b/src/round.rs index 06fbcca849..f53ac634db 100644 --- a/src/round.rs +++ b/src/round.rs @@ -512,7 +512,7 @@ mod tests { "2020-10-28 00:00:00 +01:00" ); assert_eq!( - dt.duration_round(TimeDelta::weeks(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::weeks(1)).unwrap().to_string(), "2020-10-29 00:00:00 +01:00" ); @@ -523,7 +523,7 @@ mod tests { "2020-10-28 00:00:00 -01:00" ); assert_eq!( - dt.duration_round(TimeDelta::weeks(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::weeks(1)).unwrap().to_string(), "2020-10-29 00:00:00 -01:00" ); } @@ -660,7 +660,7 @@ mod tests { "2020-10-27 00:00:00 +01:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::weeks(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::weeks(1)).unwrap().to_string(), "2020-10-22 00:00:00 +01:00" ); @@ -671,7 +671,7 @@ mod tests { "2020-10-27 00:00:00 -01:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::weeks(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::weeks(1)).unwrap().to_string(), "2020-10-22 00:00:00 -01:00" ); } diff --git a/src/time_delta.rs b/src/time_delta.rs index e17aaaeb8b..1e747abc36 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -16,7 +16,7 @@ use core::{fmt, i64}; #[cfg(feature = "std")] use std::error::Error; -use crate::try_opt; +use crate::{expect, try_opt}; #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))] use rkyv::{Archive, Deserialize, Serialize}; @@ -94,15 +94,10 @@ impl TimeDelta { /// Makes a new `TimeDelta` with the given number of weeks. /// - /// Equivalent to `TimeDelta::seconds(weeks * 7 * 24 * 60 * 60)` with - /// overflow checks. - /// - /// # Errors - /// - /// Returns `None` when the `TimeDelta` would be out of bounds. + /// Equivalent to `TimeDelta::new(weeks as i64 * 7 * 24 * 60 * 60).unwrap()`. #[inline] - pub const fn weeks(weeks: i64) -> Option { - TimeDelta::seconds(try_opt!(weeks.checked_mul(SECS_PER_WEEK))) + pub const fn weeks(weeks: i32) -> TimeDelta { + expect!(TimeDelta::new(weeks as i64 * SECS_PER_WEEK, 0), "always in range") } /// Makes a new `TimeDelta` with the given number of days. @@ -542,6 +537,17 @@ mod tests { use crate::expect; use core::time::Duration; + #[test] + fn test_timedelta_max_value() { + const MAX: TimeDelta = TimeDelta::max_value(); + dbg!(MAX.num_weeks()); + assert!(MAX.num_weeks() > i32::MAX as i64); + dbg!(MAX.num_days()); + assert!(MAX.num_days() > i32::MAX as i64); + dbg!(MAX.num_hours()); + assert!(MAX.num_hours() > i32::MAX as i64); + } + #[test] fn test_duration() { let days = |d| TimeDelta::days(d).unwrap(); @@ -1108,7 +1114,7 @@ mod tests { #[test] fn test_duration_const() { - const ONE_WEEK: TimeDelta = expect!(TimeDelta::weeks(1), ""); + const ONE_WEEK: TimeDelta = TimeDelta::weeks(1); const ONE_DAY: TimeDelta = expect!(TimeDelta::days(1), ""); const ONE_HOUR: TimeDelta = expect!(TimeDelta::hours(1), ""); const ONE_MINUTE: TimeDelta = expect!(TimeDelta::minutes(1), ""); From 785759a9f17589627c6b3fa5ed2a0918c0d26fbd Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 13 Mar 2024 19:09:28 +0100 Subject: [PATCH 3/6] Make `TimeDelta::days` infallible --- bench/benches/chrono.rs | 2 +- src/datetime/tests.rs | 8 ++-- src/naive/date/mod.rs | 91 +++++++++++++++---------------------- src/naive/date/tests.rs | 52 +++++++++------------ src/naive/datetime/mod.rs | 22 ++++----- src/naive/datetime/tests.rs | 4 +- src/naive/time/mod.rs | 8 ++-- src/naive/time/tests.rs | 10 ++-- src/round.rs | 27 +++++------ src/time_delta.rs | 72 ++++++++--------------------- 10 files changed, 114 insertions(+), 182 deletions(-) diff --git a/bench/benches/chrono.rs b/bench/benches/chrono.rs index 9bc0db7669..b7b7eb066c 100644 --- a/bench/benches/chrono.rs +++ b/bench/benches/chrono.rs @@ -199,7 +199,7 @@ fn bench_format_manual(c: &mut Criterion) { fn bench_naivedate_add_signed(c: &mut Criterion) { let date = NaiveDate::from_ymd(2023, 7, 29).unwrap(); - let extra = TimeDelta::days(25).unwrap(); + let extra = TimeDelta::days(25); c.bench_function("bench_naivedate_add_signed", |b| { b.iter(|| black_box(date).checked_add_signed(extra).unwrap()) }); diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 1d198c6a4d..1a82cfd722 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -1415,8 +1415,8 @@ fn test_datetime_add_assign_local() { // ensure we cross a DST transition for i in 1..=365 { - datetime_add += TimeDelta::days(1).unwrap(); - assert_eq!(datetime_add, datetime + TimeDelta::days(i).unwrap()) + datetime_add += TimeDelta::days(1); + assert_eq!(datetime_add, datetime + TimeDelta::days(i)) } } @@ -1630,8 +1630,8 @@ fn test_datetime_sub_assign_local() { // ensure we cross a DST transition for i in 1..=365 { - datetime_sub -= TimeDelta::days(1).unwrap(); - assert_eq!(datetime_sub, datetime - TimeDelta::days(i).unwrap()) + datetime_sub -= TimeDelta::days(1); + assert_eq!(datetime_sub, datetime - TimeDelta::days(i)) } } diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index d0d30de063..6aeb6a7d80 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -35,7 +35,7 @@ use crate::format::{ }; use crate::month::Months; use crate::naive::{Days, IsoWeek, NaiveDateTime, NaiveTime, NaiveWeek}; -use crate::{expect, ok, try_err, try_ok_or, try_opt}; +use crate::{ok, try_err, try_ok_or, try_opt}; use crate::{Datelike, Error, TimeDelta, Weekday}; use super::internals::{Mdf, YearFlags}; @@ -872,16 +872,16 @@ impl NaiveDate { /// /// let d = NaiveDate::from_ymd(2015, 9, 5).unwrap(); /// assert_eq!( - /// d.checked_add_signed(TimeDelta::days(40).unwrap()), + /// d.checked_add_signed(TimeDelta::days(40)), /// Some(NaiveDate::from_ymd(2015, 10, 15).unwrap()) /// ); /// assert_eq!( - /// d.checked_add_signed(TimeDelta::days(-40).unwrap()), + /// d.checked_add_signed(TimeDelta::days(-40)), /// Some(NaiveDate::from_ymd(2015, 7, 27).unwrap()) /// ); - /// assert_eq!(d.checked_add_signed(TimeDelta::days(1_000_000_000).unwrap()), None); - /// assert_eq!(d.checked_add_signed(TimeDelta::days(-1_000_000_000).unwrap()), None); - /// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::days(1).unwrap()), None); + /// assert_eq!(d.checked_add_signed(TimeDelta::days(1_000_000_000)), None); + /// assert_eq!(d.checked_add_signed(TimeDelta::days(-1_000_000_000)), None); + /// assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::days(1)), None); /// ``` #[must_use] pub const fn checked_add_signed(self, rhs: TimeDelta) -> Option { @@ -905,16 +905,16 @@ impl NaiveDate { /// /// let d = NaiveDate::from_ymd(2015, 9, 5).unwrap(); /// assert_eq!( - /// d.checked_sub_signed(TimeDelta::days(40).unwrap()), + /// d.checked_sub_signed(TimeDelta::days(40)), /// Some(NaiveDate::from_ymd(2015, 7, 27).unwrap()) /// ); /// assert_eq!( - /// d.checked_sub_signed(TimeDelta::days(-40).unwrap()), + /// d.checked_sub_signed(TimeDelta::days(-40)), /// Some(NaiveDate::from_ymd(2015, 10, 15).unwrap()) /// ); - /// assert_eq!(d.checked_sub_signed(TimeDelta::days(1_000_000_000).unwrap()), None); - /// assert_eq!(d.checked_sub_signed(TimeDelta::days(-1_000_000_000).unwrap()), None); - /// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::days(1).unwrap()), None); + /// assert_eq!(d.checked_sub_signed(TimeDelta::days(1_000_000_000)), None); + /// assert_eq!(d.checked_sub_signed(TimeDelta::days(-1_000_000_000)), None); + /// assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::days(1)), None); /// ``` #[must_use] pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Option { @@ -940,18 +940,12 @@ impl NaiveDate { /// let since = NaiveDate::signed_duration_since; /// /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), TimeDelta::zero()); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)), TimeDelta::days(1).unwrap()); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)), TimeDelta::days(-1).unwrap()); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)), TimeDelta::days(100).unwrap()); - /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)), TimeDelta::days(365).unwrap()); - /// assert_eq!( - /// since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), - /// TimeDelta::days(365 * 4 + 1).unwrap() - /// ); - /// assert_eq!( - /// since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), - /// TimeDelta::days(365 * 400 + 97).unwrap() - /// ); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)), TimeDelta::days(1)); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)), TimeDelta::days(-1)); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)), TimeDelta::days(100)); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)), TimeDelta::days(365)); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)), TimeDelta::days(365 * 4 + 1)); + /// assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)), TimeDelta::days(365 * 400 + 97)); /// ``` #[must_use] pub const fn signed_duration_since(self, rhs: NaiveDate) -> TimeDelta { @@ -959,12 +953,10 @@ impl NaiveDate { let year2 = rhs.year(); let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400); let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400); - let cycle1 = yo_to_cycle(year1_mod_400 as u32, self.ordinal()) as i64; - let cycle2 = yo_to_cycle(year2_mod_400 as u32, rhs.ordinal()) as i64; - let days = (year1_div_400 as i64 - year2_div_400 as i64) * 146_097 + (cycle1 - cycle2); - // The range of `TimeDelta` is ca. 585 million years, the range of `NaiveDate` ca. 525.000 - // years. - expect!(TimeDelta::days(days), "always in range") + let cycle1 = yo_to_cycle(year1_mod_400 as u32, self.ordinal()) as i32; + let cycle2 = yo_to_cycle(year2_mod_400 as u32, rhs.ordinal()) as i32; + let days = (year1_div_400 - year2_div_400) * 146_097 + (cycle1 - cycle2); + TimeDelta::days(days) } /// Returns the number of whole years from the given `base` until `self`. @@ -1709,14 +1701,11 @@ impl Datelike for NaiveDate { /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::zero(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(86399).unwrap(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(-86399).unwrap(), from_ymd(2014, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(1).unwrap(), from_ymd(2014, 1, 2)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1).unwrap(), from_ymd(2013, 12, 31)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364).unwrap(), from_ymd(2014, 12, 31)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 4 + 1).unwrap(), from_ymd(2018, 1, 1)); -/// assert_eq!( -/// from_ymd(2014, 1, 1) + TimeDelta::days(365 * 400 + 97).unwrap(), -/// from_ymd(2414, 1, 1) -/// ); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(1), from_ymd(2014, 1, 2)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1), from_ymd(2013, 12, 31)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364), from_ymd(2014, 12, 31)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 4 + 1), from_ymd(2018, 1, 1)); +/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365 * 400 + 97), from_ymd(2414, 1, 1)); /// ``` /// /// [`NaiveDate::checked_add_signed`]: crate::NaiveDate::checked_add_signed @@ -1855,14 +1844,11 @@ impl Sub for NaiveDate { /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::zero(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(86399).unwrap(), from_ymd(2014, 1, 1)); /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(-86399).unwrap(), from_ymd(2014, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(1).unwrap(), from_ymd(2013, 12, 31)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1).unwrap(), from_ymd(2014, 1, 2)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364).unwrap(), from_ymd(2013, 1, 2)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 4 + 1).unwrap(), from_ymd(2010, 1, 1)); -/// assert_eq!( -/// from_ymd(2014, 1, 1) - TimeDelta::days(365 * 400 + 97).unwrap(), -/// from_ymd(1614, 1, 1) -/// ); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(1), from_ymd(2013, 12, 31)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1), from_ymd(2014, 1, 2)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364), from_ymd(2013, 1, 2)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 4 + 1), from_ymd(2010, 1, 1)); +/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365 * 400 + 97), from_ymd(1614, 1, 1)); /// ``` /// /// [`NaiveDate::checked_sub_signed`]: crate::NaiveDate::checked_sub_signed @@ -1909,15 +1895,12 @@ impl SubAssign for NaiveDate { /// let from_ymd = |y, m, d| NaiveDate::from_ymd(y, m, d).unwrap(); /// /// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 1), TimeDelta::zero()); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 12, 31), TimeDelta::days(1).unwrap()); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 2), TimeDelta::days(-1).unwrap()); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 9, 23), TimeDelta::days(100).unwrap()); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 1, 1), TimeDelta::days(365).unwrap()); -/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2010, 1, 1), TimeDelta::days(365 * 4 + 1).unwrap()); -/// assert_eq!( -/// from_ymd(2014, 1, 1) - from_ymd(1614, 1, 1), -/// TimeDelta::days(365 * 400 + 97).unwrap() -/// ); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 12, 31), TimeDelta::days(1)); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2014, 1, 2), TimeDelta::days(-1)); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 9, 23), TimeDelta::days(100)); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2013, 1, 1), TimeDelta::days(365)); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(2010, 1, 1), TimeDelta::days(365 * 4 + 1)); +/// assert_eq!(from_ymd(2014, 1, 1) - from_ymd(1614, 1, 1), TimeDelta::days(365 * 400 + 97)); /// ``` impl Sub for NaiveDate { type Output = TimeDelta; diff --git a/src/naive/date/tests.rs b/src/naive/date/tests.rs index 9a2c0cb89c..d666736e2a 100644 --- a/src/naive/date/tests.rs +++ b/src/naive/date/tests.rs @@ -455,24 +455,20 @@ fn test_date_checked_add_signed() { check(ymd(2014, 1, 1), TimeDelta::seconds(86399).unwrap(), ymd(2014, 1, 1)); // always round towards zero check(ymd(2014, 1, 1), TimeDelta::seconds(-86399).unwrap(), ymd(2014, 1, 1)); - check(ymd(2014, 1, 1), TimeDelta::days(1).unwrap(), ymd(2014, 1, 2)); - check(ymd(2014, 1, 1), TimeDelta::days(-1).unwrap(), ymd(2013, 12, 31)); - check(ymd(2014, 1, 1), TimeDelta::days(364).unwrap(), ymd(2014, 12, 31)); - check(ymd(2014, 1, 1), TimeDelta::days(365 * 4 + 1).unwrap(), ymd(2018, 1, 1)); - check(ymd(2014, 1, 1), TimeDelta::days(365 * 400 + 97).unwrap(), ymd(2414, 1, 1)); + check(ymd(2014, 1, 1), TimeDelta::days(1), ymd(2014, 1, 2)); + check(ymd(2014, 1, 1), TimeDelta::days(-1), ymd(2013, 12, 31)); + check(ymd(2014, 1, 1), TimeDelta::days(364), ymd(2014, 12, 31)); + check(ymd(2014, 1, 1), TimeDelta::days(365 * 4 + 1), ymd(2018, 1, 1)); + check(ymd(2014, 1, 1), TimeDelta::days(365 * 400 + 97), ymd(2414, 1, 1)); - check(ymd(-7, 1, 1), TimeDelta::days(365 * 12 + 3).unwrap(), ymd(5, 1, 1)); + check(ymd(-7, 1, 1), TimeDelta::days(365 * 12 + 3), ymd(5, 1, 1)); // overflow check - check( - ymd(0, 1, 1), - TimeDelta::days(MAX_DAYS_FROM_YEAR_0 as i64).unwrap(), - ymd(MAX_YEAR, 12, 31), - ); - check(ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 as i64 + 1).unwrap(), None); + check(ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0), ymd(MAX_YEAR, 12, 31)); + check(ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0 + 1), None); check(ymd(0, 1, 1), TimeDelta::max_value(), None); - check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap(), ymd(MIN_YEAR, 1, 1)); - check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 as i64 - 1).unwrap(), None); + check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0), ymd(MIN_YEAR, 1, 1)); + check(ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 - 1), None); check(ymd(0, 1, 1), TimeDelta::min_value(), None); } @@ -485,18 +481,14 @@ fn test_date_signed_duration_since() { let ymd = |y, m, d| NaiveDate::from_ymd(y, m, d).ok(); check(ymd(2014, 1, 1), ymd(2014, 1, 1), TimeDelta::zero()); - check(ymd(2014, 1, 2), ymd(2014, 1, 1), TimeDelta::days(1).unwrap()); - check(ymd(2014, 12, 31), ymd(2014, 1, 1), TimeDelta::days(364).unwrap()); - check(ymd(2015, 1, 3), ymd(2014, 1, 1), TimeDelta::days(365 + 2).unwrap()); - check(ymd(2018, 1, 1), ymd(2014, 1, 1), TimeDelta::days(365 * 4 + 1).unwrap()); - check(ymd(2414, 1, 1), ymd(2014, 1, 1), TimeDelta::days(365 * 400 + 97).unwrap()); - - check( - ymd(MAX_YEAR, 12, 31), - ymd(0, 1, 1), - TimeDelta::days(MAX_DAYS_FROM_YEAR_0 as i64).unwrap(), - ); - check(ymd(MIN_YEAR, 1, 1), ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0 as i64).unwrap()); + check(ymd(2014, 1, 2), ymd(2014, 1, 1), TimeDelta::days(1)); + check(ymd(2014, 12, 31), ymd(2014, 1, 1), TimeDelta::days(364)); + check(ymd(2015, 1, 3), ymd(2014, 1, 1), TimeDelta::days(365 + 2)); + check(ymd(2018, 1, 1), ymd(2014, 1, 1), TimeDelta::days(365 * 4 + 1)); + check(ymd(2414, 1, 1), ymd(2014, 1, 1), TimeDelta::days(365 * 400 + 97)); + + check(ymd(MAX_YEAR, 12, 31), ymd(0, 1, 1), TimeDelta::days(MAX_DAYS_FROM_YEAR_0)); + check(ymd(MIN_YEAR, 1, 1), ymd(0, 1, 1), TimeDelta::days(MIN_DAYS_FROM_YEAR_0)); } #[test] @@ -542,9 +534,9 @@ fn test_date_sub_days() { fn test_date_addassignment() { let ymd = |y, m, d| NaiveDate::from_ymd(y, m, d).unwrap(); let mut date = ymd(2016, 10, 1); - date += TimeDelta::days(10).unwrap(); + date += TimeDelta::days(10); assert_eq!(date, ymd(2016, 10, 11)); - date += TimeDelta::days(30).unwrap(); + date += TimeDelta::days(30); assert_eq!(date, ymd(2016, 11, 10)); } @@ -552,9 +544,9 @@ fn test_date_addassignment() { fn test_date_subassignment() { let ymd = |y, m, d| NaiveDate::from_ymd(y, m, d).unwrap(); let mut date = ymd(2016, 10, 11); - date -= TimeDelta::days(10).unwrap(); + date -= TimeDelta::days(10); assert_eq!(date, ymd(2016, 10, 1)); - date -= TimeDelta::days(2).unwrap(); + date -= TimeDelta::days(2); assert_eq!(date, ymd(2016, 9, 29)); } diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 82477e6759..30910541d4 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -280,7 +280,7 @@ impl NaiveDateTime { /// ``` /// # use chrono::{TimeDelta, NaiveDate}; /// # let hms = |h, m, s| NaiveDate::from_ymd(2016, 7, 8).unwrap().and_hms(h, m, s).unwrap(); - /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::days(1_000_000_000).unwrap()), None); + /// assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::days(1_000_000_000)), None); /// ``` /// /// Leap seconds are handled, @@ -303,7 +303,7 @@ impl NaiveDateTime { /// Some(hmsm(3, 6, 9, 300))); /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(-10).unwrap()), /// Some(hmsm(3, 5, 50, 300))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::days(1).unwrap()), + /// assert_eq!(leap.checked_add_signed(TimeDelta::days(1)), /// Some(from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300).unwrap())); /// ``` #[must_use] @@ -461,7 +461,7 @@ impl NaiveDateTime { /// ``` /// # use chrono::{TimeDelta, NaiveDate}; /// # let hms = |h, m, s| NaiveDate::from_ymd(2016, 7, 8).unwrap().and_hms(h, m, s).unwrap(); - /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::days(1_000_000_000).unwrap()), None); + /// assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::days(1_000_000_000)), None); /// ``` /// /// Leap seconds are handled, @@ -480,7 +480,7 @@ impl NaiveDateTime { /// Some(hmsm(3, 5, 59, 800))); /// assert_eq!(leap.checked_sub_signed(TimeDelta::seconds(60).unwrap()), /// Some(hmsm(3, 5, 0, 300))); - /// assert_eq!(leap.checked_sub_signed(TimeDelta::days(1).unwrap()), + /// assert_eq!(leap.checked_sub_signed(TimeDelta::days(1)), /// Some(from_ymd(2016, 7, 7).and_hms_milli(3, 6, 0, 300).unwrap())); /// ``` #[must_use] @@ -1318,10 +1318,7 @@ impl Timelike for NaiveDateTime { /// hms(3, 5, 7) + TimeDelta::seconds(86_400).unwrap(), /// from_ymd(2016, 7, 9).and_hms(3, 5, 7).unwrap() /// ); -/// assert_eq!( -/// hms(3, 5, 7) + TimeDelta::days(365).unwrap(), -/// from_ymd(2017, 7, 8).and_hms(3, 5, 7).unwrap() -/// ); +/// assert_eq!(hms(3, 5, 7) + TimeDelta::days(365), from_ymd(2017, 7, 8).and_hms(3, 5, 7).unwrap()); /// /// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli).unwrap(); /// assert_eq!(hmsm(3, 5, 7, 980) + TimeDelta::milliseconds(450).unwrap(), hmsm(3, 5, 8, 430)); @@ -1341,7 +1338,7 @@ impl Timelike for NaiveDateTime { /// assert_eq!(leap + TimeDelta::milliseconds(800).unwrap(), hmsm(3, 6, 0, 100)); /// assert_eq!(leap + TimeDelta::seconds(10).unwrap(), hmsm(3, 6, 9, 300)); /// assert_eq!(leap + TimeDelta::seconds(-10).unwrap(), hmsm(3, 5, 50, 300)); -/// assert_eq!(leap + TimeDelta::days(1).unwrap(), +/// assert_eq!(leap + TimeDelta::days(1), /// from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300).unwrap()); /// ``` /// @@ -1503,10 +1500,7 @@ impl Add for NaiveDateTime { /// hms(3, 5, 7) - TimeDelta::seconds(86_400).unwrap(), /// from_ymd(2016, 7, 7).and_hms(3, 5, 7).unwrap() /// ); -/// assert_eq!( -/// hms(3, 5, 7) - TimeDelta::days(365).unwrap(), -/// from_ymd(2015, 7, 9).and_hms(3, 5, 7).unwrap() -/// ); +/// assert_eq!(hms(3, 5, 7) - TimeDelta::days(365), from_ymd(2015, 7, 9).and_hms(3, 5, 7).unwrap()); /// /// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli).unwrap(); /// assert_eq!(hmsm(3, 5, 7, 450) - TimeDelta::milliseconds(670).unwrap(), hmsm(3, 5, 6, 780)); @@ -1524,7 +1518,7 @@ impl Add for NaiveDateTime { /// assert_eq!(leap - TimeDelta::milliseconds(200).unwrap(), hmsm(3, 5, 59, 1_100)); /// assert_eq!(leap - TimeDelta::milliseconds(500).unwrap(), hmsm(3, 5, 59, 800)); /// assert_eq!(leap - TimeDelta::seconds(60).unwrap(), hmsm(3, 5, 0, 300)); -/// assert_eq!(leap - TimeDelta::days(1).unwrap(), +/// assert_eq!(leap - TimeDelta::days(1), /// from_ymd(2016, 7, 7).and_hms_milli(3, 6, 0, 300).unwrap()); /// ``` /// diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index d7a066d596..c090683820 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -74,7 +74,7 @@ fn test_datetime_addassignment() { let mut date = ymdhms(2016, 10, 1, 10, 10, 10); date += TimeDelta::minutes(10_000_000).unwrap(); assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10)); - date += TimeDelta::days(10).unwrap(); + date += TimeDelta::days(10); assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10)); } @@ -84,7 +84,7 @@ fn test_datetime_subassignment() { let mut date = ymdhms(2016, 10, 1, 10, 10, 10); date -= TimeDelta::minutes(10_000_000).unwrap(); assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10)); - date -= TimeDelta::days(10).unwrap(); + date -= TimeDelta::days(10); assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10)); } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index b75ab3bad6..45faf010bc 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -1110,7 +1110,7 @@ impl Timelike for NaiveTime { /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(22*60*60).unwrap(), from_hmsm(1, 5, 7, 0)); /// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-8*60*60).unwrap(), from_hmsm(19, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::days(800).unwrap(), from_hmsm(3, 5, 7, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::days(800), from_hmsm(3, 5, 7, 0)); /// ``` /// /// Leap seconds are handled, but the addition assumes that it is the only leap second happened. @@ -1125,7 +1125,7 @@ impl Timelike for NaiveTime { /// assert_eq!(leap + TimeDelta::milliseconds(800).unwrap(), from_hmsm(3, 6, 0, 100)); /// assert_eq!(leap + TimeDelta::seconds(10).unwrap(), from_hmsm(3, 6, 9, 300)); /// assert_eq!(leap + TimeDelta::seconds(-10).unwrap(), from_hmsm(3, 5, 50, 300)); -/// assert_eq!(leap + TimeDelta::days(1).unwrap(), from_hmsm(3, 5, 59, 300)); +/// assert_eq!(leap + TimeDelta::days(1), from_hmsm(3, 5, 59, 300)); /// ``` /// /// [leap second handling]: crate::NaiveTime#leap-second-handling @@ -1231,7 +1231,7 @@ impl Add for NaiveTime { /// # use chrono::{TimeDelta, NaiveTime}; /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli(h, m, s, milli).unwrap() }; /// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(8*60*60).unwrap(), from_hmsm(19, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::days(800).unwrap(), from_hmsm(3, 5, 7, 0)); +/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::days(800), from_hmsm(3, 5, 7, 0)); /// ``` /// /// Leap seconds are handled, but the subtraction assumes that it is the only leap second happened. @@ -1244,7 +1244,7 @@ impl Add for NaiveTime { /// assert_eq!(leap - TimeDelta::milliseconds(200).unwrap(), from_hmsm(3, 5, 59, 1_100)); /// assert_eq!(leap - TimeDelta::milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 800)); /// assert_eq!(leap - TimeDelta::seconds(60).unwrap(), from_hmsm(3, 5, 0, 300)); -/// assert_eq!(leap - TimeDelta::days(1).unwrap(), from_hmsm(3, 6, 0, 300)); +/// assert_eq!(leap - TimeDelta::days(1), from_hmsm(3, 6, 0, 300)); /// ``` /// /// [leap second handling]: crate::NaiveTime#leap-second-handling diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index fb3c414353..3b83357f9a 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -112,9 +112,9 @@ fn test_time_add() { check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(1800).unwrap(), hmsm(3, 6, 1, 100)); check!(hmsm(3, 5, 59, 900), TimeDelta::seconds(86399).unwrap(), hmsm(3, 5, 58, 900)); // overwrap check!(hmsm(3, 5, 59, 900), TimeDelta::seconds(-86399).unwrap(), hmsm(3, 6, 0, 900)); - check!(hmsm(3, 5, 59, 900), TimeDelta::days(12345).unwrap(), hmsm(3, 5, 59, 900)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(1).unwrap(), hmsm(3, 5, 59, 300)); - check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(-1).unwrap(), hmsm(3, 6, 0, 300)); + check!(hmsm(3, 5, 59, 900), TimeDelta::days(12345), hmsm(3, 5, 59, 900)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(1), hmsm(3, 5, 59, 300)); + check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(-1), hmsm(3, 6, 0, 300)); // regression tests for #37 check!(hmsm(0, 0, 0, 0), TimeDelta::milliseconds(-990).unwrap(), hmsm(23, 59, 59, 10)); @@ -140,11 +140,11 @@ fn test_time_overflowing_add() { // overflowing_add_signed with leap seconds may be counter-intuitive assert_eq!( - hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::days(1).unwrap()), + hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::days(1)), (hmsm(3, 4, 59, 678), 86_400) ); assert_eq!( - hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::days(-1).unwrap()), + hmsm(3, 4, 59, 1_678).overflowing_add_signed(TimeDelta::days(-1)), (hmsm(3, 5, 0, 678), -86_400) ); } diff --git a/src/round.rs b/src/round.rs index f53ac634db..35b62228c6 100644 --- a/src/round.rs +++ b/src/round.rs @@ -130,7 +130,7 @@ pub trait DurationRound: Sized { /// "2018-01-11 12:00:00.150 UTC" /// ); /// assert_eq!( - /// dt.duration_round(TimeDelta::days(1).unwrap()).unwrap().to_string(), + /// dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), /// "2018-01-12 00:00:00 UTC" /// ); /// ``` @@ -152,7 +152,7 @@ pub trait DurationRound: Sized { /// "2018-01-11 12:00:00.150 UTC" /// ); /// assert_eq!( - /// dt.duration_trunc(TimeDelta::days(1).unwrap()).unwrap().to_string(), + /// dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), /// "2018-01-11 00:00:00 UTC" /// ); /// ``` @@ -267,7 +267,7 @@ pub enum RoundingError { /// .unwrap(); /// /// assert_eq!( - /// dt.duration_round(TimeDelta::days(300 * 365).unwrap()), + /// dt.duration_round(TimeDelta::days(300 * 365)), /// Err(RoundingError::DurationExceedsLimit) /// ); /// ``` @@ -279,10 +279,7 @@ pub enum RoundingError { /// # use chrono::{DurationRound, TimeDelta, RoundingError, TimeZone, Utc}; /// let dt = Utc.with_ymd_and_hms(2300, 12, 12, 0, 0, 0).unwrap(); /// - /// assert_eq!( - /// dt.duration_round(TimeDelta::days(1).unwrap()), - /// Err(RoundingError::TimestampExceedsLimit) - /// ); + /// assert_eq!(dt.duration_round(TimeDelta::days(1)), Err(RoundingError::TimestampExceedsLimit)); /// ``` TimestampExceedsLimit, } @@ -501,14 +498,14 @@ mod tests { "2012-12-12 18:00:00 UTC" ); assert_eq!( - dt.duration_round(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), "2012-12-13 00:00:00 UTC" ); // timezone east let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_round(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), "2020-10-28 00:00:00 +01:00" ); assert_eq!( @@ -519,7 +516,7 @@ mod tests { // timezone west let dt = FixedOffset::west(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_round(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), "2020-10-28 00:00:00 -01:00" ); assert_eq!( @@ -586,7 +583,7 @@ mod tests { "2012-12-12 18:00:00" ); assert_eq!( - dt.duration_round(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), "2012-12-13 00:00:00" ); } @@ -649,14 +646,14 @@ mod tests { "2012-12-12 18:00:00 UTC" ); assert_eq!( - dt.duration_trunc(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), "2012-12-12 00:00:00 UTC" ); // timezone east let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), "2020-10-27 00:00:00 +01:00" ); assert_eq!( @@ -667,7 +664,7 @@ mod tests { // timezone west let dt = FixedOffset::west(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), "2020-10-27 00:00:00 -01:00" ); assert_eq!( @@ -728,7 +725,7 @@ mod tests { "2012-12-12 18:00:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::days(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), "2012-12-12 00:00:00" ); } diff --git a/src/time_delta.rs b/src/time_delta.rs index 1e747abc36..a9d90827d6 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -102,15 +102,10 @@ impl TimeDelta { /// Makes a new `TimeDelta` with the given number of days. /// - /// Equivalent to `TimeDelta::seconds(days * 24 * 60 * 60)` with overflow - /// checks. - /// - /// # Errors - /// - /// Returns `None` when the `TimeDelta` would be out of bounds. + /// Equivalent to `TimeDelta::new(days as i64 * 24 * 60 * 60).unwrap()`. #[inline] - pub const fn days(days: i64) -> Option { - TimeDelta::seconds(try_opt!(days.checked_mul(SECS_PER_DAY))) + pub const fn days(days: i32) -> TimeDelta { + expect!(TimeDelta::new(days as i64 * SECS_PER_DAY, 0), "always in range") } /// Makes a new `TimeDelta` with the given number of hours. @@ -550,7 +545,7 @@ mod tests { #[test] fn test_duration() { - let days = |d| TimeDelta::days(d).unwrap(); + let days = TimeDelta::days; let seconds = |s| TimeDelta::seconds(s).unwrap(); assert!(seconds(1) != TimeDelta::zero()); @@ -574,14 +569,14 @@ mod tests { #[test] fn test_duration_num_days() { assert_eq!(TimeDelta::zero().num_days(), 0); - assert_eq!(TimeDelta::days(1).unwrap().num_days(), 1); - assert_eq!(TimeDelta::days(-1).unwrap().num_days(), -1); + assert_eq!(TimeDelta::days(1).num_days(), 1); + assert_eq!(TimeDelta::days(-1).num_days(), -1); assert_eq!(TimeDelta::seconds(86_399).unwrap().num_days(), 0); assert_eq!(TimeDelta::seconds(86_401).unwrap().num_days(), 1); assert_eq!(TimeDelta::seconds(-86_399).unwrap().num_days(), 0); assert_eq!(TimeDelta::seconds(-86_401).unwrap().num_days(), -1); - assert_eq!(TimeDelta::days(i32::MAX as i64).unwrap().num_days(), i32::MAX as i64); - assert_eq!(TimeDelta::days(i32::MIN as i64).unwrap().num_days(), i32::MIN as i64); + assert_eq!(TimeDelta::days(i32::MAX).num_days(), i32::MAX as i64); + assert_eq!(TimeDelta::days(i32::MIN).num_days(), i32::MIN as i64); } #[test] @@ -692,23 +687,8 @@ mod tests { assert_eq!(TimeDelta::nanoseconds(-1001).num_microseconds(), Some(-1)); // overflow checks - const MICROS_PER_DAY: i64 = 86_400_000_000; - assert_eq!( - TimeDelta::days(i64::MAX / MICROS_PER_DAY).unwrap().num_microseconds(), - Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY) - ); - assert_eq!( - TimeDelta::days(-i64::MAX / MICROS_PER_DAY).unwrap().num_microseconds(), - Some(-i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY) - ); - assert_eq!( - TimeDelta::days(i64::MAX / MICROS_PER_DAY + 1).unwrap().num_microseconds(), - None - ); - assert_eq!( - TimeDelta::days(-i64::MAX / MICROS_PER_DAY - 1).unwrap().num_microseconds(), - None - ); + assert_eq!(TimeDelta::max_value().num_microseconds(), None); + assert_eq!(TimeDelta::min_value().num_microseconds(), None); } #[test] fn test_duration_microseconds_max_allowed() { @@ -796,17 +776,8 @@ mod tests { assert_eq!(TimeDelta::nanoseconds(-1).num_nanoseconds(), Some(-1)); // overflow checks - const NANOS_PER_DAY: i64 = 86_400_000_000_000; - assert_eq!( - TimeDelta::days(i64::MAX / NANOS_PER_DAY).unwrap().num_nanoseconds(), - Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY) - ); - assert_eq!( - TimeDelta::days(-i64::MAX / NANOS_PER_DAY).unwrap().num_nanoseconds(), - Some(-i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY) - ); - assert_eq!(TimeDelta::days(i64::MAX / NANOS_PER_DAY + 1).unwrap().num_nanoseconds(), None); - assert_eq!(TimeDelta::days(-i64::MAX / NANOS_PER_DAY - 1).unwrap().num_nanoseconds(), None); + assert_eq!(TimeDelta::max_value().num_nanoseconds(), None); + assert_eq!(TimeDelta::min_value().num_nanoseconds(), None); } #[test] fn test_duration_nanoseconds_max_allowed() { @@ -990,13 +961,8 @@ mod tests { TimeDelta::seconds(10).unwrap() - TimeDelta::nanoseconds(10) ); assert_eq!( - (TimeDelta::nanoseconds(1) - + TimeDelta::seconds(1).unwrap() - + TimeDelta::days(1).unwrap()) - * 3, - TimeDelta::nanoseconds(3) - + TimeDelta::seconds(3).unwrap() - + TimeDelta::days(3).unwrap() + (TimeDelta::nanoseconds(1) + TimeDelta::seconds(1).unwrap() + TimeDelta::days(1)) * 3, + TimeDelta::nanoseconds(3) + TimeDelta::seconds(3).unwrap() + TimeDelta::days(3) ); assert_eq!(TimeDelta::milliseconds(1500).unwrap() * -2, TimeDelta::seconds(-3).unwrap()); assert_eq!(TimeDelta::milliseconds(-1500).unwrap() * 2, TimeDelta::seconds(-3).unwrap()); @@ -1047,14 +1013,14 @@ mod tests { #[test] fn test_duration_fmt() { assert_eq!(TimeDelta::zero().to_string(), "P0D"); - assert_eq!(TimeDelta::days(42).unwrap().to_string(), "PT3628800S"); - assert_eq!(TimeDelta::days(-42).unwrap().to_string(), "-PT3628800S"); + assert_eq!(TimeDelta::days(42).to_string(), "PT3628800S"); + assert_eq!(TimeDelta::days(-42).to_string(), "-PT3628800S"); assert_eq!(TimeDelta::seconds(42).unwrap().to_string(), "PT42S"); assert_eq!(TimeDelta::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::days(7).unwrap() + TimeDelta::milliseconds(6543).unwrap()).to_string(), + (TimeDelta::days(7) + TimeDelta::milliseconds(6543).unwrap()).to_string(), "PT604806.543S" ); assert_eq!(TimeDelta::seconds(-86_401).unwrap().to_string(), "-PT86401S"); @@ -1062,7 +1028,7 @@ mod tests { // the format specifier should have no effect on `TimeDelta` assert_eq!( - format!("{:30}", TimeDelta::days(1).unwrap() + TimeDelta::milliseconds(2345).unwrap()), + format!("{:30}", TimeDelta::days(1) + TimeDelta::milliseconds(2345).unwrap()), "PT86402.345S" ); } @@ -1115,7 +1081,7 @@ mod tests { #[test] fn test_duration_const() { const ONE_WEEK: TimeDelta = TimeDelta::weeks(1); - const ONE_DAY: TimeDelta = expect!(TimeDelta::days(1), ""); + const ONE_DAY: TimeDelta = TimeDelta::days(1); const ONE_HOUR: TimeDelta = expect!(TimeDelta::hours(1), ""); const ONE_MINUTE: TimeDelta = expect!(TimeDelta::minutes(1), ""); const ONE_SECOND: TimeDelta = expect!(TimeDelta::seconds(1), ""); From f3f5805666a69deb1ff185fe85b0b34cc8e5386b Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Thu, 14 Mar 2024 07:35:44 +0100 Subject: [PATCH 4/6] Make `TimeDelta::hours` infallible --- src/datetime/tests.rs | 14 ++++---------- src/naive/time/mod.rs | 12 ++++++------ src/naive/time/tests.rs | 14 +++++++------- src/offset/local/windows.rs | 2 +- src/round.rs | 8 ++++---- src/time_delta.rs | 12 ++++-------- tests/dateutils.rs | 2 +- 7 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 1a82cfd722..1429d408ff 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -51,7 +51,7 @@ impl TimeZone for DstTester { DstTester::TO_WINTER_MONTH_DAY.1, ) .unwrap() - .and_time(DstTester::transition_start_local() - TimeDelta::hours(1).unwrap()); + .and_time(DstTester::transition_start_local() - TimeDelta::hours(1)); let local_to_summer_transition_start = NaiveDate::from_ymd( local.year(), @@ -67,7 +67,7 @@ impl TimeZone for DstTester { DstTester::TO_SUMMER_MONTH_DAY.1, ) .unwrap() - .and_time(DstTester::transition_start_local() + TimeDelta::hours(1).unwrap()); + .and_time(DstTester::transition_start_local() + TimeDelta::hours(1)); if *local < local_to_winter_transition_end || *local >= local_to_summer_transition_end { LocalResult::Single(DstTester::summer_offset()) @@ -1522,10 +1522,7 @@ fn test_min_max_setters() { assert_eq!(beyond_min.with_ordinal0(beyond_min.ordinal0()), Some(beyond_min)); assert_eq!(beyond_min.with_ordinal0(200), None); assert_eq!(beyond_min.with_hour(beyond_min.hour()), Some(beyond_min)); - assert_eq!( - beyond_min.with_hour(23), - beyond_min.checked_add_signed(TimeDelta::hours(1).unwrap()) - ); + assert_eq!(beyond_min.with_hour(23), beyond_min.checked_add_signed(TimeDelta::hours(1))); assert_eq!(beyond_min.with_hour(5), None); assert_eq!(beyond_min.with_minute(0), Some(beyond_min)); assert_eq!(beyond_min.with_second(0), Some(beyond_min)); @@ -1546,10 +1543,7 @@ fn test_min_max_setters() { assert_eq!(beyond_max.with_ordinal0(beyond_max.ordinal0()), Some(beyond_max)); assert_eq!(beyond_max.with_ordinal0(200), None); assert_eq!(beyond_max.with_hour(beyond_max.hour()), Some(beyond_max)); - assert_eq!( - beyond_max.with_hour(0), - beyond_max.checked_sub_signed(TimeDelta::hours(1).unwrap()) - ); + assert_eq!(beyond_max.with_hour(0), beyond_max.checked_sub_signed(TimeDelta::hours(1))); assert_eq!(beyond_max.with_hour(5), None); assert_eq!(beyond_max.with_minute(beyond_max.minute()), Some(beyond_max)); assert_eq!(beyond_max.with_second(beyond_max.second()), Some(beyond_max)); diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 45faf010bc..6714263d3c 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -527,15 +527,15 @@ impl NaiveTime { /// let from_hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap(); /// /// assert_eq!( - /// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11).unwrap()), + /// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(11)), /// (from_hms(14, 4, 5), 0) /// ); /// assert_eq!( - /// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23).unwrap()), + /// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(23)), /// (from_hms(2, 4, 5), 86_400) /// ); /// assert_eq!( - /// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7).unwrap()), + /// from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::hours(-7)), /// (from_hms(20, 4, 5), -86_400) /// ); /// ``` @@ -589,15 +589,15 @@ impl NaiveTime { /// let from_hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap(); /// /// assert_eq!( - /// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2).unwrap()), + /// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(2)), /// (from_hms(1, 4, 5), 0) /// ); /// assert_eq!( - /// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17).unwrap()), + /// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(17)), /// (from_hms(10, 4, 5), 86_400) /// ); /// assert_eq!( - /// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22).unwrap()), + /// from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::hours(-22)), /// (from_hms(1, 4, 5), -86_400) /// ); /// ``` diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index 3b83357f9a..3d2d2125f8 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -126,15 +126,15 @@ fn test_time_overflowing_add() { let hmsm = |h, m, s, ms| NaiveTime::from_hms_milli(h, m, s, ms).unwrap(); assert_eq!( - hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(11).unwrap()), + hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(11)), (hmsm(14, 4, 5, 678), 0) ); assert_eq!( - hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(23).unwrap()), + hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(23)), (hmsm(2, 4, 5, 678), 86_400) ); assert_eq!( - hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(-7).unwrap()), + hmsm(3, 4, 5, 678).overflowing_add_signed(TimeDelta::hours(-7)), (hmsm(20, 4, 5, 678), -86_400) ); @@ -153,9 +153,9 @@ fn test_time_overflowing_add() { fn test_time_addassignment() { let hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap(); let mut time = hms(12, 12, 12); - time += TimeDelta::hours(10).unwrap(); + time += TimeDelta::hours(10); assert_eq!(time, hms(22, 12, 12)); - time += TimeDelta::hours(10).unwrap(); + time += TimeDelta::hours(10); assert_eq!(time, hms(8, 12, 12)); } @@ -163,9 +163,9 @@ fn test_time_addassignment() { fn test_time_subassignment() { let hms = |h, m, s| NaiveTime::from_hms(h, m, s).unwrap(); let mut time = hms(12, 12, 12); - time -= TimeDelta::hours(10).unwrap(); + time -= TimeDelta::hours(10); assert_eq!(time, hms(2, 12, 12)); - time -= TimeDelta::hours(10).unwrap(); + time -= TimeDelta::hours(10); assert_eq!(time, hms(16, 12, 12)); } diff --git a/src/offset/local/windows.rs b/src/offset/local/windows.rs index 2370297cc1..ecbec3fd86 100644 --- a/src/offset/local/windows.rs +++ b/src/offset/local/windows.rs @@ -287,7 +287,7 @@ mod tests { if let Some(our_result) = Local.from_local_datetime(&date).earliest() { assert_eq!(from_local_time(&date), our_result); } - date += TimeDelta::hours(1).unwrap(); + date += TimeDelta::hours(1); } } } diff --git a/src/round.rs b/src/round.rs index 35b62228c6..1fd39eace0 100644 --- a/src/round.rs +++ b/src/round.rs @@ -494,7 +494,7 @@ mod tests { "2012-12-12 18:30:00 UTC" ); assert_eq!( - dt.duration_round(TimeDelta::hours(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::hours(1)).unwrap().to_string(), "2012-12-12 18:00:00 UTC" ); assert_eq!( @@ -579,7 +579,7 @@ mod tests { "2012-12-12 18:30:00" ); assert_eq!( - dt.duration_round(TimeDelta::hours(1).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::hours(1)).unwrap().to_string(), "2012-12-12 18:00:00" ); assert_eq!( @@ -642,7 +642,7 @@ mod tests { "2012-12-12 18:00:00 UTC" ); assert_eq!( - dt.duration_trunc(TimeDelta::hours(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::hours(1)).unwrap().to_string(), "2012-12-12 18:00:00 UTC" ); assert_eq!( @@ -721,7 +721,7 @@ mod tests { "2012-12-12 18:00:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::hours(1).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::hours(1)).unwrap().to_string(), "2012-12-12 18:00:00" ); assert_eq!( diff --git a/src/time_delta.rs b/src/time_delta.rs index a9d90827d6..599beebaa6 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -110,14 +110,10 @@ impl TimeDelta { /// Makes a new `TimeDelta` with the given number of hours. /// - /// Equivalent to `TimeDelta::seconds(hours * 60 * 60)` with overflow checks. - /// - /// # Errors - /// - /// Returns `None` when the `TimeDelta` would be out of bounds. + /// Equivalent to `TimeDelta::new(hours as i64 * 60 * 60, 0).unwrap()`. #[inline] - pub const fn hours(hours: i64) -> Option { - TimeDelta::seconds(try_opt!(hours.checked_mul(SECS_PER_HOUR))) + pub const fn hours(hours: i32) -> TimeDelta { + expect!(TimeDelta::new(hours as i64 * SECS_PER_HOUR, 0), "always in range") } /// Makes a new `TimeDelta` with the given number of minutes. @@ -1082,7 +1078,7 @@ mod tests { fn test_duration_const() { const ONE_WEEK: TimeDelta = TimeDelta::weeks(1); const ONE_DAY: TimeDelta = TimeDelta::days(1); - const ONE_HOUR: TimeDelta = expect!(TimeDelta::hours(1), ""); + const ONE_HOUR: TimeDelta = TimeDelta::hours(1); const ONE_MINUTE: TimeDelta = expect!(TimeDelta::minutes(1), ""); const ONE_SECOND: TimeDelta = expect!(TimeDelta::seconds(1), ""); const ONE_MILLI: TimeDelta = expect!(TimeDelta::milliseconds(1), ""); diff --git a/tests/dateutils.rs b/tests/dateutils.rs index e25aafa2ca..07c2594ba3 100644 --- a/tests/dateutils.rs +++ b/tests/dateutils.rs @@ -94,7 +94,7 @@ fn try_verify_against_date_command() { let end = NaiveDate::from_ymd(*year + 1, 1, 1).unwrap().and_time(NaiveTime::MIN); while date <= end { verify_against_date_command_local(DATE_PATH, date); - date += chrono::TimeDelta::hours(1).unwrap(); + date += chrono::TimeDelta::hours(1); } })); } From 31cfe3994165a67a3aac02f2e1f4105d5dd9c003 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Thu, 14 Mar 2024 07:39:36 +0100 Subject: [PATCH 5/6] Make `TimeDelta::minutes` infallible --- src/datetime/tests.rs | 8 ++++---- src/naive/datetime/tests.rs | 4 ++-- src/round.rs | 40 ++++++++++++++++++------------------- src/time_delta.rs | 14 +++++-------- tests/wasm.rs | 2 +- 5 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 1429d408ff..bce6f5e165 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -1426,20 +1426,20 @@ fn test_datetime_sub_assign() { let datetime = naivedatetime.and_utc(); let mut datetime_sub = datetime; - datetime_sub -= TimeDelta::minutes(90).unwrap(); - assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90).unwrap()); + datetime_sub -= TimeDelta::minutes(90); + assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90)); let timezone = FixedOffset::east(60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_sub = datetime_sub.with_timezone(&timezone); - assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90).unwrap()); + assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90)); let timezone = FixedOffset::west(2 * 60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_sub = datetime_sub.with_timezone(&timezone); - assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90).unwrap()); + assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90)); } #[test] diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index c090683820..f47e6ca2ff 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -72,7 +72,7 @@ fn test_datetime_sub() { fn test_datetime_addassignment() { let ymdhms = |y, m, d, h, n, s| NaiveDate::from_ymd(y, m, d).unwrap().and_hms(h, n, s).unwrap(); let mut date = ymdhms(2016, 10, 1, 10, 10, 10); - date += TimeDelta::minutes(10_000_000).unwrap(); + date += TimeDelta::minutes(10_000_000); assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10)); date += TimeDelta::days(10); assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10)); @@ -82,7 +82,7 @@ fn test_datetime_addassignment() { fn test_datetime_subassignment() { let ymdhms = |y, m, d, h, n, s| NaiveDate::from_ymd(y, m, d).unwrap().and_hms(h, n, s).unwrap(); let mut date = ymdhms(2016, 10, 1, 10, 10, 10); - date -= TimeDelta::minutes(10_000_000).unwrap(); + date -= TimeDelta::minutes(10_000_000); assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10)); date -= TimeDelta::days(10); assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10)); diff --git a/src/round.rs b/src/round.rs index 1fd39eace0..d2ec45c2d3 100644 --- a/src/round.rs +++ b/src/round.rs @@ -471,7 +471,7 @@ mod tests { ) .unwrap(); assert_eq!( - dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:25:00 UTC" ); // round down @@ -481,16 +481,16 @@ mod tests { ) .unwrap(); assert_eq!( - dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:20:00 UTC" ); assert_eq!( - dt.duration_round(TimeDelta::minutes(10).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(), "2012-12-12 18:20:00 UTC" ); assert_eq!( - dt.duration_round(TimeDelta::minutes(30).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(30)).unwrap().to_string(), "2012-12-12 18:30:00 UTC" ); assert_eq!( @@ -555,7 +555,7 @@ mod tests { .unwrap() .naive_utc(); assert_eq!( - dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:25:00" ); // round down @@ -566,16 +566,16 @@ mod tests { .unwrap() .naive_utc(); assert_eq!( - dt.duration_round(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:20:00" ); assert_eq!( - dt.duration_round(TimeDelta::minutes(10).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(), "2012-12-12 18:20:00" ); assert_eq!( - dt.duration_round(TimeDelta::minutes(30).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(30)).unwrap().to_string(), "2012-12-12 18:30:00" ); assert_eq!( @@ -592,7 +592,7 @@ mod tests { fn test_duration_round_pre_epoch() { let dt = Utc.with_ymd_and_hms(1969, 12, 12, 12, 12, 12).unwrap(); assert_eq!( - dt.duration_round(TimeDelta::minutes(10).unwrap()).unwrap().to_string(), + dt.duration_round(TimeDelta::minutes(10)).unwrap().to_string(), "1969-12-12 12:10:00 UTC" ); } @@ -620,7 +620,7 @@ mod tests { ) .unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:20:00 UTC" ); // would round down @@ -630,15 +630,15 @@ mod tests { ) .unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:20:00 UTC" ); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(10).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(), "2012-12-12 18:20:00 UTC" ); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(30).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(30)).unwrap().to_string(), "2012-12-12 18:00:00 UTC" ); assert_eq!( @@ -698,7 +698,7 @@ mod tests { .unwrap() .naive_utc(); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:20:00" ); // would round down @@ -709,15 +709,15 @@ mod tests { .unwrap() .naive_utc(); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(5).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(5)).unwrap().to_string(), "2012-12-12 18:20:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(10).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(), "2012-12-12 18:20:00" ); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(30).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(30)).unwrap().to_string(), "2012-12-12 18:00:00" ); assert_eq!( @@ -734,7 +734,7 @@ mod tests { fn test_duration_trunc_pre_epoch() { let dt = Utc.with_ymd_and_hms(1969, 12, 12, 12, 12, 12).unwrap(); assert_eq!( - dt.duration_trunc(TimeDelta::minutes(10).unwrap()).unwrap().to_string(), + dt.duration_trunc(TimeDelta::minutes(10)).unwrap().to_string(), "1969-12-12 12:10:00 UTC" ); } @@ -756,7 +756,7 @@ mod tests { #[test] fn test_duration_trunc_close_to_epoch() { - let span = TimeDelta::minutes(15).unwrap(); + let span = TimeDelta::minutes(15); let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms(0, 0, 15).unwrap(); assert_eq!(dt.duration_trunc(span).unwrap().to_string(), "1970-01-01 00:00:00"); @@ -767,7 +767,7 @@ mod tests { #[test] fn test_duration_round_close_to_epoch() { - let span = TimeDelta::minutes(15).unwrap(); + let span = TimeDelta::minutes(15); let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms(0, 0, 15).unwrap(); assert_eq!(dt.duration_round(span).unwrap().to_string(), "1970-01-01 00:00:00"); diff --git a/src/time_delta.rs b/src/time_delta.rs index 599beebaa6..f354e0630d 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -118,14 +118,10 @@ impl TimeDelta { /// Makes a new `TimeDelta` with the given number of minutes. /// - /// Equivalent to `TimeDelta::seconds(minutes * 60)` with overflow checks. - /// - /// # Errors - /// - /// Returns `None` when the `TimeDelta` would be out of bounds. + /// Equivalent to `TimeDelta::new(minutes as i64 * 60, 0).unwrap()`. #[inline] - pub const fn minutes(minutes: i64) -> Option { - TimeDelta::seconds(try_opt!(minutes.checked_mul(SECS_PER_MINUTE))) + pub const fn minutes(minutes: i32) -> TimeDelta { + expect!(TimeDelta::new(minutes as i64 * SECS_PER_MINUTE, 0), "always in range") } /// Makes a new `TimeDelta` with the given number of seconds. @@ -557,7 +553,7 @@ mod tests { assert_eq!(-(days(3) + seconds(70)), days(-4) + seconds(86_400 - 70)); let mut d = TimeDelta::default(); - d += TimeDelta::minutes(1).unwrap(); + d += TimeDelta::minutes(1); d -= seconds(30); assert_eq!(d, seconds(30)); } @@ -1079,7 +1075,7 @@ mod tests { const ONE_WEEK: TimeDelta = TimeDelta::weeks(1); const ONE_DAY: TimeDelta = TimeDelta::days(1); const ONE_HOUR: TimeDelta = TimeDelta::hours(1); - const ONE_MINUTE: TimeDelta = expect!(TimeDelta::minutes(1), ""); + const ONE_MINUTE: TimeDelta = TimeDelta::minutes(1); const ONE_SECOND: TimeDelta = expect!(TimeDelta::seconds(1), ""); const ONE_MILLI: TimeDelta = expect!(TimeDelta::milliseconds(1), ""); const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1); diff --git a/tests/wasm.rs b/tests/wasm.rs index 3b6b7951ce..46a74fbae5 100644 --- a/tests/wasm.rs +++ b/tests/wasm.rs @@ -25,7 +25,7 @@ fn now() { let actual = NaiveDateTime::parse_from_str(&now, "%s").unwrap().and_utc(); let diff = utc - actual; assert!( - diff < chrono::TimeDelta::minutes(5).unwrap(), + diff < chrono::TimeDelta::minutes(5), "expected {} - {} == {} < 5m (env var: {})", utc, actual, From 4f79a8a89d67f1f6ff7dd3238dc3946dfa3da577 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Thu, 14 Mar 2024 07:49:44 +0100 Subject: [PATCH 6/6] Make `TimeDelta::seconds` infallible --- src/datetime/tests.rs | 12 ++-- src/format/parsed.rs | 2 +- src/lib.rs | 8 +-- src/naive/date/mod.rs | 8 +-- src/naive/date/tests.rs | 4 +- src/naive/datetime/mod.rs | 78 +++++++++------------- src/naive/datetime/tests.rs | 10 +-- src/naive/time/mod.rs | 81 +++++++++-------------- src/naive/time/tests.rs | 8 +-- src/time_delta.rs | 128 +++++++++++++----------------------- 10 files changed, 135 insertions(+), 204 deletions(-) diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index bce6f5e165..64afafe130 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -569,12 +569,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).unwrap(), + dt + TimeDelta::seconds(3600 + 60 + 1), 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).unwrap() + TimeDelta::seconds(-7 * 3600 - 3 * 60 - 3) ); assert_eq!(*Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap().offset(), Utc); @@ -1389,20 +1389,20 @@ fn test_datetime_add_assign() { let datetime = naivedatetime.and_utc(); let mut datetime_add = datetime; - datetime_add += TimeDelta::seconds(60).unwrap(); - assert_eq!(datetime_add, datetime + TimeDelta::seconds(60).unwrap()); + datetime_add += TimeDelta::seconds(60); + assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); let timezone = FixedOffset::east(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).unwrap()); + assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); let timezone = FixedOffset::west(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).unwrap()); + assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); } #[test] diff --git a/src/format/parsed.rs b/src/format/parsed.rs index a0a193c715..199a504f71 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -795,7 +795,7 @@ impl Parsed { 59 => {} // `datetime` is known to be off by one second. 0 => { - datetime -= TimeDelta::seconds(1).unwrap(); + datetime -= TimeDelta::seconds(1); } // otherwise it is impossible. _ => return Err(IMPOSSIBLE), diff --git a/src/lib.rs b/src/lib.rs index daefd98193..f4a93d6741 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -199,11 +199,11 @@ //! // 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).unwrap()); -//! assert_eq!(dt2.signed_duration_since(dt1), TimeDelta::seconds(2 * 3600 - 2).unwrap()); -//! assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap() + TimeDelta::seconds(1_000_000_000).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).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()); //! ``` //! diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 6aeb6a7d80..a6c5104f3c 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -1699,8 +1699,8 @@ impl Datelike for NaiveDate { /// let from_ymd = |y, m, d| NaiveDate::from_ymd(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).unwrap(), from_ymd(2014, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(-86399).unwrap(), 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::days(1), from_ymd(2014, 1, 2)); /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1), from_ymd(2013, 12, 31)); /// assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364), from_ymd(2014, 12, 31)); @@ -1842,8 +1842,8 @@ impl Sub for NaiveDate { /// let from_ymd = |y, m, d| NaiveDate::from_ymd(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).unwrap(), from_ymd(2014, 1, 1)); -/// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(-86399).unwrap(), 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::days(1), from_ymd(2013, 12, 31)); /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1), from_ymd(2014, 1, 2)); /// assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364), from_ymd(2013, 1, 2)); diff --git a/src/naive/date/tests.rs b/src/naive/date/tests.rs index d666736e2a..7711edec2d 100644 --- a/src/naive/date/tests.rs +++ b/src/naive/date/tests.rs @@ -452,9 +452,9 @@ fn test_date_checked_add_signed() { let ymd = |y, m, d| NaiveDate::from_ymd(y, m, d).ok(); check(ymd(2014, 1, 1), TimeDelta::zero(), ymd(2014, 1, 1)); - check(ymd(2014, 1, 1), TimeDelta::seconds(86399).unwrap(), ymd(2014, 1, 1)); + check(ymd(2014, 1, 1), TimeDelta::seconds(86399), ymd(2014, 1, 1)); // always round towards zero - check(ymd(2014, 1, 1), TimeDelta::seconds(-86399).unwrap(), ymd(2014, 1, 1)); + check(ymd(2014, 1, 1), TimeDelta::seconds(-86399), ymd(2014, 1, 1)); check(ymd(2014, 1, 1), TimeDelta::days(1), ymd(2014, 1, 2)); check(ymd(2014, 1, 1), TimeDelta::days(-1), ymd(2013, 12, 31)); check(ymd(2014, 1, 1), TimeDelta::days(364), ymd(2014, 12, 31)); diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 30910541d4..2f8ecbb61e 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -254,17 +254,11 @@ impl NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms(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).unwrap()), Some(hms(3, 5, 8))); + /// 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(-1).unwrap()), - /// Some(hms(3, 5, 6)) - /// ); - /// assert_eq!( - /// hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(3600 + 60).unwrap()), - /// Some(hms(4, 6, 7)) - /// ); - /// assert_eq!( - /// hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(86_400).unwrap()), + /// hms(3, 5, 7).checked_add_signed(TimeDelta::seconds(86_400)), /// Some(from_ymd(2016, 7, 9).and_hms(3, 5, 7).unwrap()) /// ); /// @@ -299,9 +293,9 @@ impl NaiveDateTime { /// Some(hmsm(3, 5, 59, 1_800))); /// assert_eq!(leap.checked_add_signed(TimeDelta::milliseconds(800).unwrap()), /// Some(hmsm(3, 6, 0, 100))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(10).unwrap()), + /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(10)), /// Some(hmsm(3, 6, 9, 300))); - /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(-10).unwrap()), + /// assert_eq!(leap.checked_add_signed(TimeDelta::seconds(-10)), /// Some(hmsm(3, 5, 50, 300))); /// assert_eq!(leap.checked_add_signed(TimeDelta::days(1)), /// Some(from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300).unwrap())); @@ -309,7 +303,7 @@ impl NaiveDateTime { #[must_use] pub const fn checked_add_signed(self, rhs: TimeDelta) -> Option { let (time, remainder) = self.time.overflowing_add_signed(rhs); - let remainder = try_opt!(TimeDelta::seconds(remainder)); + let remainder = try_opt!(TimeDelta::new(remainder, 0)); let date = try_opt!(self.date.checked_add_signed(remainder)); Some(NaiveDateTime { date, time }) } @@ -435,17 +429,11 @@ impl NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms(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).unwrap()), Some(hms(3, 5, 6))); - /// assert_eq!( - /// hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(-1).unwrap()), - /// Some(hms(3, 5, 8)) - /// ); - /// assert_eq!( - /// hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(3600 + 60).unwrap()), - /// Some(hms(2, 4, 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).unwrap()), + /// hms(3, 5, 7).checked_sub_signed(TimeDelta::seconds(86_400)), /// Some(from_ymd(2016, 7, 7).and_hms(3, 5, 7).unwrap()) /// ); /// @@ -478,7 +466,7 @@ impl NaiveDateTime { /// Some(hmsm(3, 5, 59, 1_100))); /// assert_eq!(leap.checked_sub_signed(TimeDelta::milliseconds(500).unwrap()), /// Some(hmsm(3, 5, 59, 800))); - /// assert_eq!(leap.checked_sub_signed(TimeDelta::seconds(60).unwrap()), + /// assert_eq!(leap.checked_sub_signed(TimeDelta::seconds(60)), /// Some(hmsm(3, 5, 0, 300))); /// assert_eq!(leap.checked_sub_signed(TimeDelta::days(1)), /// Some(from_ymd(2016, 7, 7).and_hms_milli(3, 6, 0, 300).unwrap())); @@ -486,7 +474,7 @@ impl NaiveDateTime { #[must_use] pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Option { let (time, remainder) = self.time.overflowing_sub_signed(rhs); - let remainder = try_opt!(TimeDelta::seconds(remainder)); + let remainder = try_opt!(TimeDelta::new(remainder, 0)); let date = try_opt!(self.date.checked_sub_signed(remainder)); Some(NaiveDateTime { date, time }) } @@ -560,15 +548,14 @@ impl NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// assert_eq!( /// d.and_hms(3, 5, 7).unwrap().signed_duration_since(d.and_hms(2, 4, 6).unwrap()), - /// TimeDelta::seconds(3600 + 60 + 1).unwrap() + /// TimeDelta::seconds(3600 + 60 + 1) /// ); /// /// // July 8 is 190th day in the year 2016 /// let d0 = from_ymd(2016, 1, 1); /// assert_eq!( /// d.and_hms_milli(0, 7, 6, 500).unwrap().signed_duration_since(d0.and_hms(0, 0, 0).unwrap()), - /// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6).unwrap() - /// + TimeDelta::milliseconds(500).unwrap() + /// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500).unwrap() /// ); /// ``` /// @@ -581,11 +568,11 @@ impl NaiveDateTime { /// let leap = from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500).unwrap(); /// assert_eq!( /// leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms(23, 0, 0).unwrap()), - /// TimeDelta::seconds(3600).unwrap() + TimeDelta::milliseconds(500).unwrap() + /// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500).unwrap() /// ); /// assert_eq!( /// from_ymd(2015, 7, 1).and_hms(1, 0, 0).unwrap().signed_duration_since(leap), - /// TimeDelta::seconds(3600).unwrap() - TimeDelta::milliseconds(500).unwrap() + /// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500).unwrap() /// ); /// ``` #[must_use] @@ -1311,11 +1298,11 @@ impl Timelike for NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms(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).unwrap(), hms(3, 5, 8)); -/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(-1).unwrap(), hms(3, 5, 6)); -/// assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(3600 + 60).unwrap(), hms(4, 6, 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::seconds(86_400).unwrap(), +/// hms(3, 5, 7) + TimeDelta::seconds(86_400), /// from_ymd(2016, 7, 9).and_hms(3, 5, 7).unwrap() /// ); /// assert_eq!(hms(3, 5, 7) + TimeDelta::days(365), from_ymd(2017, 7, 8).and_hms(3, 5, 7).unwrap()); @@ -1336,8 +1323,8 @@ impl Timelike for NaiveDateTime { /// assert_eq!(leap + TimeDelta::milliseconds(-500).unwrap(), hmsm(3, 5, 59, 800)); /// assert_eq!(leap + TimeDelta::milliseconds(500).unwrap(), hmsm(3, 5, 59, 1_800)); /// assert_eq!(leap + TimeDelta::milliseconds(800).unwrap(), hmsm(3, 6, 0, 100)); -/// assert_eq!(leap + TimeDelta::seconds(10).unwrap(), hmsm(3, 6, 9, 300)); -/// assert_eq!(leap + TimeDelta::seconds(-10).unwrap(), hmsm(3, 5, 50, 300)); +/// 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::days(1), /// from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300).unwrap()); /// ``` @@ -1493,11 +1480,11 @@ impl Add for NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// let hms = |h, m, s| d.and_hms(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).unwrap(), hms(3, 5, 6)); -/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(-1).unwrap(), hms(3, 5, 8)); -/// assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(3600 + 60).unwrap(), hms(2, 4, 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::seconds(86_400).unwrap(), +/// hms(3, 5, 7) - TimeDelta::seconds(86_400), /// from_ymd(2016, 7, 7).and_hms(3, 5, 7).unwrap() /// ); /// assert_eq!(hms(3, 5, 7) - TimeDelta::days(365), from_ymd(2015, 7, 9).and_hms(3, 5, 7).unwrap()); @@ -1517,7 +1504,7 @@ impl Add for NaiveDateTime { /// assert_eq!(leap - TimeDelta::zero(), hmsm(3, 5, 59, 1_300)); /// assert_eq!(leap - TimeDelta::milliseconds(200).unwrap(), hmsm(3, 5, 59, 1_100)); /// assert_eq!(leap - TimeDelta::milliseconds(500).unwrap(), hmsm(3, 5, 59, 800)); -/// assert_eq!(leap - TimeDelta::seconds(60).unwrap(), hmsm(3, 5, 0, 300)); +/// assert_eq!(leap - TimeDelta::seconds(60), hmsm(3, 5, 0, 300)); /// assert_eq!(leap - TimeDelta::days(1), /// from_ymd(2016, 7, 7).and_hms_milli(3, 6, 0, 300).unwrap()); /// ``` @@ -1661,15 +1648,14 @@ impl Sub for NaiveDateTime { /// let d = from_ymd(2016, 7, 8); /// assert_eq!( /// d.and_hms(3, 5, 7).unwrap() - d.and_hms(2, 4, 6).unwrap(), -/// TimeDelta::seconds(3600 + 60 + 1).unwrap() +/// TimeDelta::seconds(3600 + 60 + 1) /// ); /// /// // July 8 is 190th day in the year 2016 /// let d0 = from_ymd(2016, 1, 1); /// assert_eq!( /// d.and_hms_milli(0, 7, 6, 500).unwrap() - d0.and_hms(0, 0, 0).unwrap(), -/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6).unwrap() -/// + TimeDelta::milliseconds(500).unwrap() +/// TimeDelta::seconds(189 * 86_400 + 7 * 60 + 6) + TimeDelta::milliseconds(500).unwrap() /// ); /// ``` /// @@ -1682,11 +1668,11 @@ impl Sub for NaiveDateTime { /// let leap = from_ymd(2015, 6, 30).and_hms_milli(23, 59, 59, 1_500).unwrap(); /// assert_eq!( /// leap - from_ymd(2015, 6, 30).and_hms(23, 0, 0).unwrap(), -/// TimeDelta::seconds(3600).unwrap() + TimeDelta::milliseconds(500).unwrap() +/// TimeDelta::seconds(3600) + TimeDelta::milliseconds(500).unwrap() /// ); /// assert_eq!( /// from_ymd(2015, 7, 1).and_hms(1, 0, 0).unwrap() - leap, -/// TimeDelta::seconds(3600).unwrap() - TimeDelta::milliseconds(500).unwrap() +/// TimeDelta::seconds(3600) - TimeDelta::milliseconds(500).unwrap() /// ); /// ``` impl Sub for NaiveDateTime { diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index f47e6ca2ff..10401cdaa7 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -15,7 +15,7 @@ fn test_datetime_add() { assert_eq!(lhs.checked_add_signed(rhs), sum); assert_eq!(lhs.checked_sub_signed(-rhs), sum); } - let seconds = |s| TimeDelta::seconds(s).unwrap(); + let seconds = TimeDelta::seconds; 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))); @@ -52,19 +52,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).unwrap() + TimeDelta::seconds(1) ); assert_eq!( since(ymdhms(2014, 5, 6, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)), - TimeDelta::seconds(-1).unwrap() + TimeDelta::seconds(-1) ); assert_eq!( since(ymdhms(2014, 5, 7, 7, 8, 9), ymdhms(2014, 5, 6, 7, 8, 10)), - TimeDelta::seconds(86399).unwrap() + TimeDelta::seconds(86399) ); assert_eq!( since(ymdhms(2001, 9, 9, 1, 46, 39), ymdhms(1970, 1, 1, 0, 0, 0)), - TimeDelta::seconds(999_999_999).unwrap() + TimeDelta::seconds(999_999_999) ); } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 6714263d3c..f91a3aeec3 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -635,25 +635,16 @@ impl NaiveTime { /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)), /// TimeDelta::milliseconds(975).unwrap() /// ); - /// assert_eq!( - /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)), - /// TimeDelta::seconds(7).unwrap() - /// ); - /// assert_eq!( - /// since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)), - /// TimeDelta::seconds(5 * 60).unwrap() - /// ); + /// 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(0, 5, 7, 900)), - /// TimeDelta::seconds(3 * 3600).unwrap() - /// ); - /// assert_eq!( - /// since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)), - /// TimeDelta::seconds(-3600).unwrap() + /// TimeDelta::seconds(3 * 3600) /// ); + /// 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).unwrap() + TimeDelta::milliseconds(100).unwrap() + /// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100).unwrap() /// ); /// ``` /// @@ -665,15 +656,15 @@ impl NaiveTime { /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli(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).unwrap()); + /// TimeDelta::seconds(1)); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)), /// TimeDelta::milliseconds(1500).unwrap()); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)), - /// TimeDelta::seconds(60).unwrap()); + /// TimeDelta::seconds(60)); /// assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)), - /// TimeDelta::seconds(1).unwrap()); + /// TimeDelta::seconds(1)); /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)), - /// TimeDelta::seconds(61).unwrap()); + /// TimeDelta::seconds(61)); /// ``` #[must_use] pub const fn signed_duration_since(self, rhs: NaiveTime) -> TimeDelta { @@ -1082,11 +1073,11 @@ impl Timelike for NaiveTime { /// let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli(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).unwrap(), from_hmsm(3, 5, 8, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-1).unwrap(), from_hmsm(3, 5, 6, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(60 + 4).unwrap(), from_hmsm(3, 6, 11, 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::seconds(7 * 60 * 60 - 6 * 60).unwrap(), +/// from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(7 * 60 * 60 - 6 * 60), /// from_hmsm(9, 59, 7, 0) /// ); /// assert_eq!( @@ -1108,8 +1099,8 @@ impl Timelike for NaiveTime { /// ``` /// # use chrono::{TimeDelta, NaiveTime}; /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli(h, m, s, milli).unwrap() }; -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(22*60*60).unwrap(), from_hmsm(1, 5, 7, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-8*60*60).unwrap(), from_hmsm(19, 5, 7, 0)); +/// 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::days(800), from_hmsm(3, 5, 7, 0)); /// ``` /// @@ -1123,8 +1114,8 @@ impl Timelike for NaiveTime { /// assert_eq!(leap + TimeDelta::milliseconds(-500).unwrap(), from_hmsm(3, 5, 59, 800)); /// assert_eq!(leap + TimeDelta::milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 1_800)); /// assert_eq!(leap + TimeDelta::milliseconds(800).unwrap(), from_hmsm(3, 6, 0, 100)); -/// assert_eq!(leap + TimeDelta::seconds(10).unwrap(), from_hmsm(3, 6, 9, 300)); -/// assert_eq!(leap + TimeDelta::seconds(-10).unwrap(), from_hmsm(3, 5, 50, 300)); +/// 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::days(1), from_hmsm(3, 5, 59, 300)); /// ``` /// @@ -1209,10 +1200,10 @@ impl Add for NaiveTime { /// let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli(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).unwrap(), from_hmsm(3, 5, 6, 0)); -/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(60 + 5).unwrap(), from_hmsm(3, 4, 2, 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::seconds(2 * 60 * 60 + 6 * 60).unwrap(), +/// from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(2 * 60 * 60 + 6 * 60), /// from_hmsm(0, 59, 7, 0) /// ); /// assert_eq!( @@ -1230,7 +1221,7 @@ impl Add for NaiveTime { /// ``` /// # use chrono::{TimeDelta, NaiveTime}; /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli(h, m, s, milli).unwrap() }; -/// assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(8*60*60).unwrap(), from_hmsm(19, 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::days(800), from_hmsm(3, 5, 7, 0)); /// ``` /// @@ -1243,7 +1234,7 @@ impl Add for NaiveTime { /// assert_eq!(leap - TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300)); /// assert_eq!(leap - TimeDelta::milliseconds(200).unwrap(), from_hmsm(3, 5, 59, 1_100)); /// assert_eq!(leap - TimeDelta::milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 800)); -/// assert_eq!(leap - TimeDelta::seconds(60).unwrap(), from_hmsm(3, 5, 0, 300)); +/// assert_eq!(leap - TimeDelta::seconds(60), from_hmsm(3, 5, 0, 300)); /// assert_eq!(leap - TimeDelta::days(1), from_hmsm(3, 6, 0, 300)); /// ``` /// @@ -1339,22 +1330,13 @@ impl Sub for NaiveTime { /// from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 6, 925), /// TimeDelta::milliseconds(975).unwrap() /// ); -/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 0, 900), TimeDelta::seconds(7).unwrap()); -/// assert_eq!( -/// from_hmsm(3, 5, 7, 900) - from_hmsm(3, 0, 7, 900), -/// TimeDelta::seconds(5 * 60).unwrap() -/// ); -/// assert_eq!( -/// from_hmsm(3, 5, 7, 900) - from_hmsm(0, 5, 7, 900), -/// TimeDelta::seconds(3 * 3600).unwrap() -/// ); -/// assert_eq!( -/// from_hmsm(3, 5, 7, 900) - from_hmsm(4, 5, 7, 900), -/// TimeDelta::seconds(-3600).unwrap() -/// ); +/// 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(2, 4, 6, 800), -/// TimeDelta::seconds(3600 + 60 + 1).unwrap() + TimeDelta::milliseconds(100).unwrap() +/// TimeDelta::seconds(3600 + 60 + 1) + TimeDelta::milliseconds(100).unwrap() /// ); /// ``` /// @@ -1364,13 +1346,12 @@ impl Sub for NaiveTime { /// ``` /// # use chrono::{TimeDelta, NaiveTime}; /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli(h, m, s, milli).unwrap() }; -/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 59, 0), TimeDelta::seconds(1).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_500) - from_hmsm(3, 0, 59, 0), /// TimeDelta::milliseconds(1500).unwrap()); -/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 0, 0), TimeDelta::seconds(60).unwrap()); -/// assert_eq!(from_hmsm(3, 0, 0, 0) - from_hmsm(2, 59, 59, 1_000), TimeDelta::seconds(1).unwrap()); -/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(2, 59, 59, 1_000), -/// TimeDelta::seconds(61).unwrap()); +/// 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(2, 59, 59, 1_000), TimeDelta::seconds(61)); /// ``` impl Sub for NaiveTime { type Output = TimeDelta; diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index 3d2d2125f8..5219a9e137 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -110,8 +110,8 @@ fn test_time_add() { check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(100).unwrap(), hmsm(3, 5, 59, 1_400)); check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(800).unwrap(), hmsm(3, 6, 0, 100)); check!(hmsm(3, 5, 59, 1_300), TimeDelta::milliseconds(1800).unwrap(), hmsm(3, 6, 1, 100)); - check!(hmsm(3, 5, 59, 900), TimeDelta::seconds(86399).unwrap(), hmsm(3, 5, 58, 900)); // overwrap - check!(hmsm(3, 5, 59, 900), TimeDelta::seconds(-86399).unwrap(), hmsm(3, 6, 0, 900)); + 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::days(12345), hmsm(3, 5, 59, 900)); check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(1), hmsm(3, 5, 59, 300)); check!(hmsm(3, 5, 59, 1_300), TimeDelta::days(-1), hmsm(3, 6, 0, 300)); @@ -183,11 +183,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).unwrap()); - check!(hmsm(3, 5, 7, 200), hmsm(2, 4, 6, 200), TimeDelta::seconds(3600 + 60 + 1).unwrap()); + 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, 300), - TimeDelta::seconds(3600 + 60).unwrap() + TimeDelta::milliseconds(900).unwrap() + TimeDelta::seconds(3600 + 60) + TimeDelta::milliseconds(900).unwrap() ); // 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 f354e0630d..f25e9c71fb 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -126,14 +126,11 @@ impl TimeDelta { /// Makes a new `TimeDelta` with the given number of seconds. /// - /// # Errors - /// - /// Returns `None` when `seconds` is more than `i64::MAX / 1_000` or less than - /// `-i64::MAX / 1_000` (in this context, this is the same as `i64::MIN / 1_000` due to - /// rounding). + /// Use [`TimeDelta::new()`] if you want to create a value `TimeDelta` with a range that is + /// greater than that of an `i32`. #[inline] - pub const fn seconds(seconds: i64) -> Option { - TimeDelta::new(seconds, 0) + pub const fn seconds(seconds: i32) -> TimeDelta { + expect!(TimeDelta::new(seconds as i64, 0), "always in range") } /// Makes a new `TimeDelta` with the given number of milliseconds. @@ -538,7 +535,7 @@ mod tests { #[test] fn test_duration() { let days = TimeDelta::days; - let seconds = |s| TimeDelta::seconds(s).unwrap(); + let seconds = TimeDelta::seconds; assert!(seconds(1) != TimeDelta::zero()); assert_eq!(seconds(1) + seconds(2), seconds(3)); @@ -563,10 +560,10 @@ mod tests { assert_eq!(TimeDelta::zero().num_days(), 0); assert_eq!(TimeDelta::days(1).num_days(), 1); assert_eq!(TimeDelta::days(-1).num_days(), -1); - assert_eq!(TimeDelta::seconds(86_399).unwrap().num_days(), 0); - assert_eq!(TimeDelta::seconds(86_401).unwrap().num_days(), 1); - assert_eq!(TimeDelta::seconds(-86_399).unwrap().num_days(), 0); - assert_eq!(TimeDelta::seconds(-86_401).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::days(i32::MAX).num_days(), i32::MAX as i64); assert_eq!(TimeDelta::days(i32::MIN).num_days(), i32::MIN as i64); } @@ -574,44 +571,14 @@ mod tests { #[test] fn test_duration_num_seconds() { assert_eq!(TimeDelta::zero().num_seconds(), 0); - assert_eq!(TimeDelta::seconds(1).unwrap().num_seconds(), 1); - assert_eq!(TimeDelta::seconds(-1).unwrap().num_seconds(), -1); + assert_eq!(TimeDelta::seconds(1).num_seconds(), 1); + assert_eq!(TimeDelta::seconds(-1).num_seconds(), -1); assert_eq!(TimeDelta::milliseconds(999).unwrap().num_seconds(), 0); assert_eq!(TimeDelta::milliseconds(1001).unwrap().num_seconds(), 1); assert_eq!(TimeDelta::milliseconds(-999).unwrap().num_seconds(), 0); assert_eq!(TimeDelta::milliseconds(-1001).unwrap().num_seconds(), -1); } - #[test] - fn test_duration_seconds_max_allowed() { - let duration = TimeDelta::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, - i64::MAX as i128 / 1_000 * 1_000_000_000 - ); - } - - #[test] - fn test_duration_seconds_max_overflow() { - assert!(TimeDelta::seconds(i64::MAX / 1_000 + 1).is_none()); - } - - #[test] - fn test_duration_seconds_min_allowed() { - let duration = TimeDelta::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, - -i64::MAX as i128 / 1_000 * 1_000_000_000 - ); - } - - #[test] - fn test_duration_seconds_min_underflow() { - assert!(TimeDelta::seconds(-i64::MAX / 1_000 - 1).is_none()); - } - #[test] fn test_duration_num_milliseconds() { assert_eq!(TimeDelta::zero().num_milliseconds(), 0); @@ -945,19 +912,19 @@ 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).unwrap()); - assert_eq!(TimeDelta::nanoseconds(1) * -1_000_000_000, -TimeDelta::seconds(1).unwrap()); - assert_eq!(-TimeDelta::nanoseconds(1) * 1_000_000_000, -TimeDelta::seconds(1).unwrap()); + 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(30) * 333_333_333, - TimeDelta::seconds(10).unwrap() - TimeDelta::nanoseconds(10) + TimeDelta::seconds(10) - TimeDelta::nanoseconds(10) ); assert_eq!( - (TimeDelta::nanoseconds(1) + TimeDelta::seconds(1).unwrap() + TimeDelta::days(1)) * 3, - TimeDelta::nanoseconds(3) + TimeDelta::seconds(3).unwrap() + TimeDelta::days(3) + (TimeDelta::nanoseconds(1) + TimeDelta::seconds(1) + TimeDelta::days(1)) * 3, + TimeDelta::nanoseconds(3) + TimeDelta::seconds(3) + TimeDelta::days(3) ); - assert_eq!(TimeDelta::milliseconds(1500).unwrap() * -2, TimeDelta::seconds(-3).unwrap()); - assert_eq!(TimeDelta::milliseconds(-1500).unwrap() * 2, TimeDelta::seconds(-3).unwrap()); + assert_eq!(TimeDelta::milliseconds(1500).unwrap() * -2, TimeDelta::seconds(-3)); + assert_eq!(TimeDelta::milliseconds(-1500).unwrap() * 2, TimeDelta::seconds(-3)); } #[test] @@ -968,38 +935,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).unwrap() / 3, TimeDelta::nanoseconds(333_333_333)); - assert_eq!(TimeDelta::seconds(4).unwrap() / 3, TimeDelta::nanoseconds(1_333_333_333)); - assert_eq!(TimeDelta::seconds(-1).unwrap() / 2, TimeDelta::milliseconds(-500).unwrap()); - assert_eq!(TimeDelta::seconds(1).unwrap() / -2, TimeDelta::milliseconds(-500).unwrap()); - assert_eq!(TimeDelta::seconds(-1).unwrap() / -2, TimeDelta::milliseconds(500).unwrap()); - assert_eq!(TimeDelta::seconds(-4).unwrap() / 3, TimeDelta::nanoseconds(-1_333_333_333)); - assert_eq!(TimeDelta::seconds(-4).unwrap() / -3, TimeDelta::nanoseconds(1_333_333_333)); + 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).unwrap()); + assert_eq!(TimeDelta::seconds(1) / -2, TimeDelta::milliseconds(-500).unwrap()); + assert_eq!(TimeDelta::seconds(-1) / -2, TimeDelta::milliseconds(500).unwrap()); + assert_eq!(TimeDelta::seconds(-4) / 3, TimeDelta::nanoseconds(-1_333_333_333)); + assert_eq!(TimeDelta::seconds(-4) / -3, TimeDelta::nanoseconds(1_333_333_333)); } #[test] fn test_duration_sum() { - let duration_list_1 = [TimeDelta::zero(), TimeDelta::seconds(1).unwrap()]; + let duration_list_1 = [TimeDelta::zero(), TimeDelta::seconds(1)]; let sum_1: TimeDelta = duration_list_1.iter().sum(); - assert_eq!(sum_1, TimeDelta::seconds(1).unwrap()); + assert_eq!(sum_1, TimeDelta::seconds(1)); let duration_list_2 = [ TimeDelta::zero(), - TimeDelta::seconds(1).unwrap(), - TimeDelta::seconds(6).unwrap(), - TimeDelta::seconds(10).unwrap(), + TimeDelta::seconds(1), + TimeDelta::seconds(6), + TimeDelta::seconds(10), ]; let sum_2: TimeDelta = duration_list_2.iter().sum(); - assert_eq!(sum_2, TimeDelta::seconds(17).unwrap()); + assert_eq!(sum_2, TimeDelta::seconds(17)); let duration_arr = [ TimeDelta::zero(), - TimeDelta::seconds(1).unwrap(), - TimeDelta::seconds(6).unwrap(), - TimeDelta::seconds(10).unwrap(), + TimeDelta::seconds(1), + TimeDelta::seconds(6), + TimeDelta::seconds(10), ]; let sum_3: TimeDelta = duration_arr.into_iter().sum(); - assert_eq!(sum_3, TimeDelta::seconds(17).unwrap()); + assert_eq!(sum_3, TimeDelta::seconds(17)); } #[test] @@ -1007,7 +974,7 @@ mod tests { assert_eq!(TimeDelta::zero().to_string(), "P0D"); assert_eq!(TimeDelta::days(42).to_string(), "PT3628800S"); assert_eq!(TimeDelta::days(-42).to_string(), "-PT3628800S"); - assert_eq!(TimeDelta::seconds(42).unwrap().to_string(), "PT42S"); + assert_eq!(TimeDelta::seconds(42).to_string(), "PT42S"); assert_eq!(TimeDelta::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"); @@ -1015,7 +982,7 @@ mod tests { (TimeDelta::days(7) + TimeDelta::milliseconds(6543).unwrap()).to_string(), "PT604806.543S" ); - assert_eq!(TimeDelta::seconds(-86_401).unwrap().to_string(), "-PT86401S"); + assert_eq!(TimeDelta::seconds(-86_401).to_string(), "-PT86401S"); assert_eq!(TimeDelta::nanoseconds(-1).to_string(), "-PT0.000000001S"); // the format specifier should have no effect on `TimeDelta` @@ -1027,8 +994,8 @@ mod tests { #[test] fn test_to_std() { - assert_eq!(TimeDelta::seconds(1).unwrap().to_std(), Ok(Duration::new(1, 0))); - assert_eq!(TimeDelta::seconds(86_401).unwrap().to_std(), Ok(Duration::new(86_401, 0))); + 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::milliseconds(123).unwrap().to_std(), Ok(Duration::new(0, 123_000_000)) @@ -1039,17 +1006,14 @@ mod tests { ); 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).unwrap().to_std(), Err(OutOfRangeError(()))); + assert_eq!(TimeDelta::seconds(-1).to_std(), Err(OutOfRangeError(()))); assert_eq!(TimeDelta::milliseconds(-1).unwrap().to_std(), Err(OutOfRangeError(()))); } #[test] fn test_from_std() { - assert_eq!(Ok(TimeDelta::seconds(1).unwrap()), TimeDelta::from_std(Duration::new(1, 0))); - assert_eq!( - Ok(TimeDelta::seconds(86_401).unwrap()), - TimeDelta::from_std(Duration::new(86_401, 0)) - ); + 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::milliseconds(123).unwrap()), TimeDelta::from_std(Duration::new(0, 123_000_000)) @@ -1076,7 +1040,7 @@ mod tests { const ONE_DAY: TimeDelta = TimeDelta::days(1); const ONE_HOUR: TimeDelta = TimeDelta::hours(1); const ONE_MINUTE: TimeDelta = TimeDelta::minutes(1); - const ONE_SECOND: TimeDelta = expect!(TimeDelta::seconds(1), ""); + const ONE_SECOND: TimeDelta = TimeDelta::seconds(1); const ONE_MILLI: TimeDelta = expect!(TimeDelta::milliseconds(1), ""); const ONE_MICRO: TimeDelta = TimeDelta::microseconds(1); const ONE_NANO: TimeDelta = TimeDelta::nanoseconds(1); @@ -1099,7 +1063,7 @@ mod tests { assert!(ONE_NANO != TimeDelta::zero()); assert_eq!( combo, - TimeDelta::seconds(86400 * 7 + 86400 + 3600 + 60 + 1).unwrap() + TimeDelta::seconds(86400 * 7 + 86400 + 3600 + 60 + 1) + TimeDelta::nanoseconds(1 + 1_000 + 1_000_000) ); } @@ -1107,7 +1071,7 @@ mod tests { #[test] #[cfg(feature = "rkyv-validation")] fn test_rkyv_validation() { - let duration = TimeDelta::seconds(1).unwrap(); + let duration = TimeDelta::seconds(1); let bytes = rkyv::to_bytes::<_, 16>(&duration).unwrap(); assert_eq!(rkyv::from_bytes::(&bytes).unwrap(), duration); }