Skip to content

Commit

Permalink
Make remaining methods on NaiveDateTime const
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jan 28, 2024
1 parent 7900e1d commit 730cbed
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,11 @@ impl NaiveDateTime {
#[deprecated(since = "0.4.31", note = "use `timestamp_nanos_opt()` instead")]
#[inline]
#[must_use]
pub fn timestamp_nanos(&self) -> i64 {
self.timestamp_nanos_opt()
.expect("value can not be represented in a timestamp with nanosecond precision.")
pub const fn timestamp_nanos(&self) -> i64 {
expect!(
self.timestamp_nanos_opt(),
"value can not be represented in a timestamp with nanosecond precision."

Check warning on line 510 in src/naive/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/datetime/mod.rs#L507-L510

Added lines #L507 - L510 were not covered by tests
)
}

/// Returns the number of non-leap *nanoseconds* since midnight on January 1, 1970.
Expand Down Expand Up @@ -542,9 +544,9 @@ impl NaiveDateTime {
/// ```
#[inline]
#[must_use]
pub fn timestamp_nanos_opt(&self) -> Option<i64> {
pub const fn timestamp_nanos_opt(&self) -> Option<i64> {
let mut timestamp = self.timestamp();
let mut timestamp_subsec_nanos = i64::from(self.timestamp_subsec_nanos());
let mut timestamp_subsec_nanos = self.timestamp_subsec_nanos() as i64;

// subsec nanos are always non-negative, however the timestamp itself (both in seconds and in nanos) can be
// negative. Now i64::MIN is NOT dividable by 1_000_000_000, so
Expand All @@ -562,7 +564,7 @@ impl NaiveDateTime {
timestamp += 1;
}

timestamp.checked_mul(1_000_000_000).and_then(|ns| ns.checked_add(timestamp_subsec_nanos))
try_opt!(timestamp.checked_mul(1_000_000_000)).checked_add(timestamp_subsec_nanos)
}

/// Returns the number of milliseconds since the last whole non-leap second.
Expand Down Expand Up @@ -699,7 +701,7 @@ impl NaiveDateTime {
/// Some(from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap()));
/// ```
#[must_use]
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<NaiveDateTime> {
pub const fn checked_add_signed(self, rhs: OldDuration) -> Option<NaiveDateTime> {
let (time, rhs) = self.time.overflowing_add_signed(rhs);

// early checking to avoid overflow in OldDuration::seconds
Expand Down Expand Up @@ -852,15 +854,15 @@ impl NaiveDateTime {
/// Some(from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap()));
/// ```
#[must_use]
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<NaiveDateTime> {
pub const fn checked_sub_signed(self, rhs: OldDuration) -> Option<NaiveDateTime> {
let (time, rhs) = self.time.overflowing_sub_signed(rhs);

// early checking to avoid overflow in OldDuration::seconds
if rhs <= (-1 << MAX_SECS_BITS) || rhs >= (1 << MAX_SECS_BITS) {
return None;
}

let date = self.date.checked_sub_signed(OldDuration::seconds(rhs))?;
let date = try_opt!(self.date.checked_sub_signed(OldDuration::seconds(rhs)));
Some(NaiveDateTime { date, time })
}

Expand Down Expand Up @@ -950,7 +952,12 @@ impl NaiveDateTime {
/// ```
#[must_use]
pub fn signed_duration_since(self, rhs: NaiveDateTime) -> OldDuration {
self.date.signed_duration_since(rhs.date) + self.time.signed_duration_since(rhs.time)
expect!(
self.date
.signed_duration_since(rhs.date)
.checked_add(&self.time.signed_duration_since(rhs.time)),
"always in range"

Check warning on line 959 in src/naive/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/datetime/mod.rs#L959

Added line #L959 was not covered by tests
)
}

/// Formats the combined date and time with the specified formatting items.
Expand Down

0 comments on commit 730cbed

Please sign in to comment.