pub trait DurationRound: Sized {
type Err: Error;
// Required methods
fn duration_round(self, duration: TimeDelta) -> Result<Self, Self::Err>;
fn duration_trunc(self, duration: TimeDelta) -> Result<Self, Self::Err>;
}
Expand description
Extension trait for rounding or truncating a DateTime by a TimeDelta.
§Limitations
Both rounding and truncating are done via TimeDelta::num_nanoseconds
and
DateTime::timestamp_nanos_opt
. This means that they will fail if either the
TimeDelta
or the DateTime
are too big to represented as nanoseconds. They
will also fail if the TimeDelta
is bigger than the timestamp, negative or zero.
Required Associated Types§
Required Methods§
Sourcefn duration_round(self, duration: TimeDelta) -> Result<Self, Self::Err>
fn duration_round(self, duration: TimeDelta) -> Result<Self, Self::Err>
Return a copy rounded by TimeDelta.
§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.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
"2018-01-11 12:00:00.150 UTC"
);
assert_eq!(
dt.duration_round(TimeDelta::try_days(1).unwrap()).unwrap().to_string(),
"2018-01-12 00:00:00 UTC"
);
Sourcefn duration_trunc(self, duration: TimeDelta) -> Result<Self, Self::Err>
fn duration_trunc(self, duration: TimeDelta) -> Result<Self, Self::Err>
Return a copy truncated by TimeDelta.
§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.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
"2018-01-11 12:00:00.150 UTC"
);
assert_eq!(
dt.duration_trunc(TimeDelta::try_days(1).unwrap()).unwrap().to_string(),
"2018-01-11 00:00:00 UTC"
);
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.