Trait chrono::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_local_timezone(Utc).unwrap();
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_local_timezone(Utc).unwrap();
assert_eq!(dt.trunc_subsecs(2).nanosecond(), 150_000_000);
assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);

Implementors§

source§

impl<T> SubsecRound for Twhere T: Timelike + Add<Duration, Output = T> + Sub<Duration, Output = T>,