Skip to content

Commit cea6aec

Browse files
authored
Add functions to DateTime to add milliseconds and Duration (#506)
1 parent 937998d commit cea6aec

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/datetime.rs

+20
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,26 @@ impl crate::DateTime {
363363
self.0
364364
}
365365

366+
/// Adds `millis` milliseconds to the [`DateTime`] saturating at [`DateTime::MIN`] and
367+
/// [`DateTime::MAX`].
368+
pub const fn saturating_add_millis(self, millis: i64) -> Self {
369+
Self::from_millis(self.0.saturating_add(millis))
370+
}
371+
372+
/// Adds `duration` to the [`DateTime`] saturating at [`DateTime::MAX`].
373+
///
374+
/// As [`DateTime`] only have millisecond-precision this will only use the whole milliseconds
375+
/// of `duration`.
376+
pub const fn saturating_add_duration(self, duration: Duration) -> Self {
377+
// i64::try_from isn't const
378+
let millis = duration.as_millis();
379+
if millis > i64::MAX as u128 {
380+
Self::from_millis(i64::MAX)
381+
} else {
382+
self.saturating_add_millis(millis as i64)
383+
}
384+
}
385+
366386
#[deprecated(since = "2.3.0", note = "Use try_to_rfc3339_string instead.")]
367387
/// Convert this [`DateTime`] to an RFC 3339 formatted string. Panics if it could not be
368388
/// represented in that format.

0 commit comments

Comments
 (0)