Skip to content

Commit ea31b26

Browse files
committed
return error when rounding with zero duration
1 parent fc94e04 commit ea31b26

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/round.rs

Lines changed: 18 additions & 15 deletions
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,14 +222,11 @@ 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 =
232229
naive.and_utc().timestamp_nanos_opt().ok_or(RoundingError::TimestampExceedsLimit)?;
233-
if span == 0 {
234-
return Ok(original);
235-
}
236230
let delta_down = stamp % span;
237231
match delta_down.cmp(&0) {
238232
Ordering::Equal => Ok(original),
@@ -456,9 +450,10 @@ mod tests {
456450
.unwrap();
457451

458452
assert_eq!(
459-
dt.duration_round(TimeDelta::zero()).unwrap().to_string(),
460-
"2016-12-31 23:59:59.175500 UTC"
453+
dt.duration_round(TimeDelta::new(-1, 0).unwrap()),
454+
Err(RoundingError::DurationExceedsLimit),
461455
);
456+
assert_eq!(dt.duration_round(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit),);
462457

463458
assert_eq!(
464459
dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
@@ -547,9 +542,10 @@ mod tests {
547542
.naive_utc();
548543

549544
assert_eq!(
550-
dt.duration_round(TimeDelta::zero()).unwrap().to_string(),
551-
"2016-12-31 23:59:59.175500"
545+
dt.duration_round(TimeDelta::new(-1, 0).unwrap()),
546+
Err(RoundingError::DurationExceedsLimit),
552547
);
548+
assert_eq!(dt.duration_round(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit),);
553549

554550
assert_eq!(
555551
dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
@@ -624,9 +620,10 @@ mod tests {
624620
.unwrap();
625621

626622
assert_eq!(
627-
dt.duration_trunc(TimeDelta::zero()).unwrap().to_string(),
628-
"2016-12-31 23:59:59.175500 UTC"
623+
dt.duration_trunc(TimeDelta::new(-1, 0).unwrap()),
624+
Err(RoundingError::DurationExceedsLimit),
629625
);
626+
assert_eq!(dt.duration_trunc(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit),);
630627

631628
assert_eq!(
632629
dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
@@ -713,6 +710,12 @@ mod tests {
713710
.unwrap()
714711
.naive_utc();
715712

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+
716719
assert_eq!(
717720
dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
718721
"2016-12-31 23:59:59.170"

0 commit comments

Comments
 (0)