chrono::round

Trait SubsecRound

Source
pub trait SubsecRound {
    // Required methods
    fn round_subsecs(self, digits: u16) -> Self;
    fn trunc_subsecs(self, digits: u16) -> Self;
}
Expand description

Extension trait for subsecond rounding or truncation to a maximum number of digits. Rounding can be used to decrease the error variance when serializing/persisting to lower precision. Truncation is the default behavior in Chrono display formatting. Either can be used to guarantee equality (e.g. for testing) when round-tripping through a lower precision format.

Required Methods§

Source

fn round_subsecs(self, digits: u16) -> Self

Return a copy rounded to the specified number of subsecond digits. With 9 or more digits, self is returned unmodified. Halfway values are rounded up (away from zero).

§Example
let dt = NaiveDate::from_ymd_opt(2018, 1, 11)
    .unwrap()
    .and_hms_milli_opt(12, 0, 0, 154)
    .unwrap()
    .and_utc();
assert_eq!(dt.round_subsecs(2).nanosecond(), 150_000_000);
assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000);
Source

fn trunc_subsecs(self, digits: u16) -> Self

Return a copy truncated to the specified number of subsecond digits. With 9 or more digits, self is returned unmodified.

§Example
let dt = NaiveDate::from_ymd_opt(2018, 1, 11)
    .unwrap()
    .and_hms_milli_opt(12, 0, 0, 154)
    .unwrap()
    .and_utc();
assert_eq!(dt.trunc_subsecs(2).nanosecond(), 150_000_000);
assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> SubsecRound for T
where T: Timelike + Add<TimeDelta, Output = T> + Sub<TimeDelta, Output = T>,