Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate panicking constructors of TimeDelta #1450

Merged
merged 13 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace TimeDelta::seconds with try_seconds
  • Loading branch information
pitdicker committed Mar 6, 2024
commit 0dc7353e164c8d24b4a768e7be79599cae7982fc
2 changes: 1 addition & 1 deletion src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,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
Original file line number Diff line number Diff line change
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