Skip to content

Commit

Permalink
Fix issue chronotope#658 duration_round by zero panics
Browse files Browse the repository at this point in the history
  • Loading branch information
A.R. Baart committed Mar 21, 2022
1 parent ea6412b commit 56763e8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/round.rs
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 56763e8

Please sign in to comment.