Skip to content

Commit

Permalink
Replace TimeDelta::seconds with try_seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 6, 2024
1 parent 073c3c3 commit 4251bd1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/format/parsed.rs
Expand Up @@ -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),
Expand Down
30 changes: 6 additions & 24 deletions src/naive/datetime/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -527,14 +519,9 @@ impl NaiveDateTime {
/// ```
#[must_use]
pub const fn checked_add_signed(self, rhs: TimeDelta) -> Option<NaiveDateTime> {
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 })
}

Expand Down Expand Up @@ -703,14 +690,9 @@ impl NaiveDateTime {
/// ```
#[must_use]
pub const fn checked_sub_signed(self, rhs: TimeDelta) -> Option<NaiveDateTime> {
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 })
}

Expand Down

0 comments on commit 4251bd1

Please sign in to comment.