From 56763e88477a24785d528cb64d4341b45675c2c1 Mon Sep 17 00:00:00 2001 From: "A.R. Baart" Date: Mon, 21 Mar 2022 16:35:21 +0100 Subject: [PATCH] Fix issue #658 duration_round by zero panics --- src/round.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/round.rs b/src/round.rs index b357d916c0..4b1d1dae7f 100644 --- a/src/round.rs +++ b/src/round.rs @@ -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"