mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-02 15:26:12 +00:00
Add subsec_millis and subsec_micros methods for TimeDelta.
Co-authored-by: Micha White <botahamec@outlook.com>
This commit is contained in:
parent
f0f807d1b5
commit
042109f4f8
@ -313,6 +313,24 @@ impl TimeDelta {
|
||||
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
|
||||
@ -779,6 +797,26 @@ mod tests {
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user