Fix issue #658 duration_round by zero panics (#659)

This commit is contained in:
AR Baart 2022-03-21 21:44:49 +01:00 committed by GitHub
parent ea6412b3da
commit 9a5f76c9bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -20,6 +20,7 @@ Versions with only mechanical changes will be omitted from the following list.
* Add `DateTime::from_local()` to construct from given local date and time (#572)
* Correct build for wasm32-unknown-emscripten target (#568)
* Change `Local::now()` and `Utc::now()` documentation from "current date" to "current date and time" (#647)
* Fix `duration_round` panic on rounding by `Duration::zero()` (#658)
## 0.4.19

View File

@ -185,6 +185,9 @@ where
if span > stamp.abs() {
return Err(RoundingError::DurationExceedsTimestamp);
}
if span == 0 {
return Ok(original);
}
let delta_down = stamp % span;
if delta_down == 0 {
Ok(original)
@ -394,6 +397,11 @@ mod tests {
fn test_duration_round() {
let dt = Utc.ymd(2016, 12, 31).and_hms_nano(23, 59, 59, 175_500_000);
assert_eq!(
dt.duration_round(Duration::zero()).unwrap().to_string(),
"2016-12-31 23:59:59.175500 UTC"
);
assert_eq!(
dt.duration_round(Duration::milliseconds(10)).unwrap().to_string(),
"2016-12-31 23:59:59.180 UTC"
@ -456,6 +464,11 @@ mod tests {
fn test_duration_round_naive() {
let dt = Utc.ymd(2016, 12, 31).and_hms_nano(23, 59, 59, 175_500_000).naive_utc();
assert_eq!(
dt.duration_round(Duration::zero()).unwrap().to_string(),
"2016-12-31 23:59:59.175500"
);
assert_eq!(
dt.duration_round(Duration::milliseconds(10)).unwrap().to_string(),
"2016-12-31 23:59:59.180"