diff --git a/src/format/parsed.rs b/src/format/parsed.rs index cf6bf99e47..6f7253de4c 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -822,7 +822,7 @@ impl Parsed { 59 => {} // `datetime` is known to be off by one second. 0 => { - datetime -= TimeDelta::seconds(1); + datetime -= TimeDelta::try_seconds(1).unwrap(); } // otherwise it is impossible. _ => return Err(IMPOSSIBLE), diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 181576d76c..13e9a1d00b 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -34,14 +34,6 @@ pub(crate) mod serde; #[cfg(test)] mod tests; -/// The tight upper bound guarantees that a time delta with `|TimeDelta| >= 2^MAX_SECS_BITS` -/// will always overflow the addition with any date and time type. -/// -/// So why is this needed? `TimeDelta::seconds(rhs)` may overflow, and we don't have -/// an alternative returning `Option` or `Result`. Thus we need some early bound to avoid -/// touching that call when we are already sure that it WILL overflow... -const MAX_SECS_BITS: usize = 44; - /// The minimum possible `NaiveDateTime`. #[deprecated(since = "0.4.20", note = "Use NaiveDateTime::MIN instead")] pub const MIN_DATETIME: NaiveDateTime = NaiveDateTime::MIN; @@ -527,14 +519,9 @@ impl NaiveDateTime { /// ``` #[must_use] pub const fn checked_add_signed(self, rhs: TimeDelta) -> Option { - let (time, rhs) = self.time.overflowing_add_signed(rhs); - - // early checking to avoid overflow in TimeDelta::seconds - if rhs <= (-1 << MAX_SECS_BITS) || rhs >= (1 << MAX_SECS_BITS) { - return None; - } - - let date = try_opt!(self.date.checked_add_signed(TimeDelta::seconds(rhs))); + let (time, remainder) = self.time.overflowing_add_signed(rhs); + let remainder = try_opt!(TimeDelta::try_seconds(remainder)); + let date = try_opt!(self.date.checked_add_signed(remainder)); Some(NaiveDateTime { date, time }) } @@ -703,14 +690,9 @@ impl NaiveDateTime { /// ``` #[must_use] pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Option { - let (time, rhs) = self.time.overflowing_sub_signed(rhs); - - // early checking to avoid overflow in TimeDelta::seconds - if rhs <= (-1 << MAX_SECS_BITS) || rhs >= (1 << MAX_SECS_BITS) { - return None; - } - - let date = try_opt!(self.date.checked_sub_signed(TimeDelta::seconds(rhs))); + let (time, remainder) = self.time.overflowing_sub_signed(rhs); + let remainder = try_opt!(TimeDelta::try_seconds(remainder)); + let date = try_opt!(self.date.checked_sub_signed(remainder)); Some(NaiveDateTime { date, time }) }