Skip to content
Merged
Changes from all commits
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
53 changes: 51 additions & 2 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,33 @@ impl TimeDelta {
if self.secs < 0 && self.nanos > 0 { self.secs + 1 } else { self.secs }
}

/// Returns the number of nanoseconds such that
/// `subsec_nanos() + num_seconds() * NANOS_PER_SEC` is the total number of
/// Returns the number of nanoseconds in the fractional part of the duration.
///
/// This is the number of nanoseconds such that

@djc djc Feb 27, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, by literal I meant to replace NANOS_PER_SEC with 1_000_000_000 here (and same for the new methods). Other than that this seems fine!

(Maybe also update the PR description to describe the current approach.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the 3 comments and the PR description.

/// `subsec_nanos() + num_seconds() * 1_000_000_000` is the total number of
/// nanoseconds in the `TimeDelta`.
pub const fn subsec_nanos(&self) -> i32 {
if self.secs < 0 && self.nanos > 0 { self.nanos - NANOS_PER_SEC } else { self.nanos }
}

/// Returns the number of microseconds in the fractional part of the duration.
///
/// This is the number of microseconds such that
/// `subsec_micros() + num_seconds() * 1_000_000` is the truncated number of
/// microseconds in the duration.
pub const fn subsec_micros(&self) -> i32 {
self.subsec_nanos() / NANOS_PER_MICRO
}

/// Returns the number of milliseconds in the fractional part of the duration.
///
/// This is the number of milliseconds such that
/// `subsec_millis() + num_seconds() * 1_000` is the truncated number of
/// milliseconds in the duration.
pub const fn subsec_millis(&self) -> i32 {
self.subsec_nanos() / NANOS_PER_MILLI
}

/// Returns the total number of whole milliseconds in the `TimeDelta`.
pub const fn num_milliseconds(&self) -> i64 {
// A proper TimeDelta will not overflow, because MIN and MAX are defined such
Expand Down Expand Up @@ -768,6 +788,35 @@ mod tests {
let _ = TimeDelta::seconds(-i64::MAX / 1_000 - 1);
}

#[test]
fn test_duration_subsec_nanos() {
assert_eq!(TimeDelta::zero().subsec_nanos(), 0);
assert_eq!(TimeDelta::nanoseconds(1).subsec_nanos(), 1);
assert_eq!(TimeDelta::nanoseconds(-1).subsec_nanos(), -1);
assert_eq!(TimeDelta::seconds(1).subsec_nanos(), 0);
assert_eq!(TimeDelta::nanoseconds(1_000_000_001).subsec_nanos(), 1);
}

#[test]
fn test_duration_subsec_micros() {
assert_eq!(TimeDelta::zero().subsec_micros(), 0);
assert_eq!(TimeDelta::microseconds(1).subsec_micros(), 1);
assert_eq!(TimeDelta::microseconds(-1).subsec_micros(), -1);
assert_eq!(TimeDelta::seconds(1).subsec_micros(), 0);
assert_eq!(TimeDelta::microseconds(1_000_001).subsec_micros(), 1);
assert_eq!(TimeDelta::nanoseconds(1_000_001_999).subsec_micros(), 1);
}

#[test]
fn test_duration_subsec_millis() {
assert_eq!(TimeDelta::zero().subsec_millis(), 0);
assert_eq!(TimeDelta::milliseconds(1).subsec_millis(), 1);
assert_eq!(TimeDelta::milliseconds(-1).subsec_millis(), -1);
assert_eq!(TimeDelta::seconds(1).subsec_millis(), 0);
assert_eq!(TimeDelta::milliseconds(1_001).subsec_millis(), 1);
assert_eq!(TimeDelta::microseconds(1_001_999).subsec_millis(), 1);
}

#[test]
fn test_duration_num_milliseconds() {
assert_eq!(TimeDelta::zero().num_milliseconds(), 0);
Expand Down