Skip to content

Commit 391187f

Browse files
authored
Return error when rounding with zero duration
1 parent ffc75e5 commit 391187f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/round.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const fn span_for_digits(digits: u16) -> u32 {
102102
/// Both rounding and truncating are done via [`TimeDelta::num_nanoseconds`] and
103103
/// [`DateTime::timestamp_nanos_opt`]. This means that they will fail if either the
104104
/// `TimeDelta` or the `DateTime` are too big to represented as nanoseconds. They
105-
/// will also fail if the `TimeDelta` is bigger than the timestamp.
105+
/// will also fail if the `TimeDelta` is bigger than the timestamp, negative or zero.
106106
pub trait DurationRound: Sized {
107107
/// Error that can occur in rounding or truncating
108108
#[cfg(feature = "std")]
@@ -188,14 +188,11 @@ where
188188
T: Timelike + Add<TimeDelta, Output = T> + Sub<TimeDelta, Output = T>,
189189
{
190190
if let Some(span) = duration.num_nanoseconds() {
191-
if span < 0 {
191+
if span <= 0 {
192192
return Err(RoundingError::DurationExceedsLimit);
193193
}
194194
let stamp =
195195
naive.and_utc().timestamp_nanos_opt().ok_or(RoundingError::TimestampExceedsLimit)?;
196-
if span == 0 {
197-
return Ok(original);
198-
}
199196
let delta_down = stamp % span;
200197
if delta_down == 0 {
201198
Ok(original)
@@ -225,7 +222,7 @@ where
225222
T: Timelike + Add<TimeDelta, Output = T> + Sub<TimeDelta, Output = T>,
226223
{
227224
if let Some(span) = duration.num_nanoseconds() {
228-
if span < 0 {
225+
if span <= 0 {
229226
return Err(RoundingError::DurationExceedsLimit);
230227
}
231228
let stamp =
@@ -453,9 +450,10 @@ mod tests {
453450
.unwrap();
454451

455452
assert_eq!(
456-
dt.duration_round(TimeDelta::zero()).unwrap().to_string(),
457-
"2016-12-31 23:59:59.175500 UTC"
453+
dt.duration_round(TimeDelta::new(-1, 0).unwrap()),
454+
Err(RoundingError::DurationExceedsLimit)
458455
);
456+
assert_eq!(dt.duration_round(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));
459457

460458
assert_eq!(
461459
dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
@@ -544,9 +542,10 @@ mod tests {
544542
.naive_utc();
545543

546544
assert_eq!(
547-
dt.duration_round(TimeDelta::zero()).unwrap().to_string(),
548-
"2016-12-31 23:59:59.175500"
545+
dt.duration_round(TimeDelta::new(-1, 0).unwrap()),
546+
Err(RoundingError::DurationExceedsLimit)
549547
);
548+
assert_eq!(dt.duration_round(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));
550549

551550
assert_eq!(
552551
dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
@@ -620,6 +619,12 @@ mod tests {
620619
)
621620
.unwrap();
622621

622+
assert_eq!(
623+
dt.duration_trunc(TimeDelta::new(-1, 0).unwrap()),
624+
Err(RoundingError::DurationExceedsLimit)
625+
);
626+
assert_eq!(dt.duration_trunc(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));
627+
623628
assert_eq!(
624629
dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
625630
"2016-12-31 23:59:59.170 UTC"
@@ -705,6 +710,12 @@ mod tests {
705710
.unwrap()
706711
.naive_utc();
707712

713+
assert_eq!(
714+
dt.duration_trunc(TimeDelta::new(-1, 0).unwrap()),
715+
Err(RoundingError::DurationExceedsLimit)
716+
);
717+
assert_eq!(dt.duration_trunc(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));
718+
708719
assert_eq!(
709720
dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
710721
"2016-12-31 23:59:59.170"

0 commit comments

Comments
 (0)