diff --git a/src/duration/mod.rs b/src/duration/mod.rs index 36dee40..c6c0d88 100644 --- a/src/duration/mod.rs +++ b/src/duration/mod.rs @@ -538,9 +538,11 @@ impl Duration { /// assert_eq!(two_hours_three_min.floor(1.hours() + 5.minutes()), 1.hours() + 5.minutes()); /// ``` pub fn floor(&self, duration: Self) -> Self { - Self::from_total_nanoseconds( - self.total_nanoseconds() - self.total_nanoseconds() % duration.total_nanoseconds(), - ) + Self::from_total_nanoseconds(if duration.total_nanoseconds() == 0 { + 0 + } else { + self.total_nanoseconds() - self.total_nanoseconds() % duration.total_nanoseconds() + }) } /// Ceils this duration to the closest provided duration diff --git a/tests/duration.rs b/tests/duration.rs index 67786f4..5a8b31a 100644 --- a/tests/duration.rs +++ b/tests/duration.rs @@ -577,3 +577,15 @@ fn test_min_max() { assert_eq!(d1, d1.max(d0)); assert_eq!(d1, d0.max(d1)); } + +#[test] +fn regression_test_gh_244() { + let zero = Duration::ZERO; + // Test that the ceil of a zero duration is still zero. + assert_eq!(zero.ceil(zero), zero); + let non_zero = Duration::from_parts(1, 23456); + // Test that the ceil of a non-zero duration by zero is still zero. + assert_eq!(non_zero.ceil(zero), zero); + // Test that the ceil of a zero duration by a non-zero is non-zero duration. + assert_eq!(zero.ceil(non_zero), non_zero); +}