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

Make remaining methods const #1337

Merged
merged 11 commits into from
Feb 1, 2024
Prev Previous commit
Next Next commit
Make NaiveTime::signed_duration_since const
  • Loading branch information
pitdicker committed Feb 1, 2024
commit be91e22d4e289966f0c7528d038f8bafb50fc050
29 changes: 12 additions & 17 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ impl NaiveTime {
/// Duration::seconds(61));
/// ```
#[must_use]
pub fn signed_duration_since(self, rhs: NaiveTime) -> OldDuration {
pub const fn signed_duration_since(self, rhs: NaiveTime) -> OldDuration {
// | | :leap| | | | | | | :leap| |
// | | : | | | | | | | : | |
// ----+----+-----*---+----+----+----+----+----+----+-------*-+----+----
Expand All @@ -735,25 +735,20 @@ impl NaiveTime {
// `rhs.frac`|========================================>|
// | | | `self - rhs` | |

use core::cmp::Ordering;

let secs = i64::from(self.secs) - i64::from(rhs.secs);
let frac = i64::from(self.frac) - i64::from(rhs.frac);
let mut secs = self.secs as i64 - rhs.secs as i64;
let frac = self.frac as i64 - rhs.frac as i64;

// `secs` may contain a leap second yet to be counted
let adjust = match self.secs.cmp(&rhs.secs) {
Ordering::Greater => i64::from(rhs.frac >= 1_000_000_000),
Ordering::Equal => 0,
Ordering::Less => {
if self.frac >= 1_000_000_000 {
-1
} else {
0
}
}
};
if self.secs > rhs.secs && rhs.frac >= 1_000_000_000 {
secs += 1;
} else if self.secs < rhs.secs && self.frac >= 1_000_000_000 {
secs -= 1;
}

let secs_from_frac = frac.div_euclid(1_000_000_000);
let frac = frac.rem_euclid(1_000_000_000) as u32;

OldDuration::seconds(secs + adjust) + OldDuration::nanoseconds(frac)
expect!(OldDuration::new(secs + secs_from_frac, frac), "must be in range")
}

/// Adds given `FixedOffset` to the current time, and returns the number of days that should be
Expand Down