Skip to content

Commit

Permalink
Fix issue chronotope#658 duration_round by zero panics (chronotope#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
ARBaart authored and pickfire committed Jul 5, 2022
1 parent 32906e9 commit 5974e91
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,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

Expand Down
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 5974e91

Please sign in to comment.