Improve documentation for subsec_nanos() and add a test for the method.

Co-authored-by: Micha White <botahamec@outlook.com>
This commit is contained in:
Georges Goetz 2025-02-26 19:22:44 +00:00 committed by Dirkjan Ochtman
parent 2b7a28ebe9
commit f0f807d1b5

View File

@ -304,8 +304,10 @@ 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
/// `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 }
@ -768,6 +770,15 @@ 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_num_milliseconds() {
assert_eq!(TimeDelta::zero().num_milliseconds(), 0);