Skip to content

Commit

Permalink
Return error when rounding with zero duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde authored Apr 9, 2024
1 parent ffc75e5 commit 391187f
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const fn span_for_digits(digits: u16) -> u32 {
/// Both rounding and truncating are done via [`TimeDelta::num_nanoseconds`] and
/// [`DateTime::timestamp_nanos_opt`]. This means that they will fail if either the
/// `TimeDelta` or the `DateTime` are too big to represented as nanoseconds. They
/// will also fail if the `TimeDelta` is bigger than the timestamp.
/// will also fail if the `TimeDelta` is bigger than the timestamp, negative or zero.
pub trait DurationRound: Sized {
/// Error that can occur in rounding or truncating
#[cfg(feature = "std")]
Expand Down Expand Up @@ -188,14 +188,11 @@ where
T: Timelike + Add<TimeDelta, Output = T> + Sub<TimeDelta, Output = T>,
{
if let Some(span) = duration.num_nanoseconds() {
if span < 0 {
if span <= 0 {
return Err(RoundingError::DurationExceedsLimit);
}
let stamp =
naive.and_utc().timestamp_nanos_opt().ok_or(RoundingError::TimestampExceedsLimit)?;
if span == 0 {
return Ok(original);
}
let delta_down = stamp % span;
if delta_down == 0 {
Ok(original)
Expand Down Expand Up @@ -225,7 +222,7 @@ where
T: Timelike + Add<TimeDelta, Output = T> + Sub<TimeDelta, Output = T>,
{
if let Some(span) = duration.num_nanoseconds() {
if span < 0 {
if span <= 0 {
return Err(RoundingError::DurationExceedsLimit);
}
let stamp =
Expand Down Expand Up @@ -453,9 +450,10 @@ mod tests {
.unwrap();

assert_eq!(
dt.duration_round(TimeDelta::zero()).unwrap().to_string(),
"2016-12-31 23:59:59.175500 UTC"
dt.duration_round(TimeDelta::new(-1, 0).unwrap()),
Err(RoundingError::DurationExceedsLimit)
);
assert_eq!(dt.duration_round(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));

assert_eq!(
dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
Expand Down Expand Up @@ -544,9 +542,10 @@ mod tests {
.naive_utc();

assert_eq!(
dt.duration_round(TimeDelta::zero()).unwrap().to_string(),
"2016-12-31 23:59:59.175500"
dt.duration_round(TimeDelta::new(-1, 0).unwrap()),
Err(RoundingError::DurationExceedsLimit)
);
assert_eq!(dt.duration_round(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));

assert_eq!(
dt.duration_round(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
Expand Down Expand Up @@ -620,6 +619,12 @@ mod tests {
)
.unwrap();

assert_eq!(
dt.duration_trunc(TimeDelta::new(-1, 0).unwrap()),
Err(RoundingError::DurationExceedsLimit)
);
assert_eq!(dt.duration_trunc(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));

assert_eq!(
dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
"2016-12-31 23:59:59.170 UTC"
Expand Down Expand Up @@ -705,6 +710,12 @@ mod tests {
.unwrap()
.naive_utc();

assert_eq!(
dt.duration_trunc(TimeDelta::new(-1, 0).unwrap()),
Err(RoundingError::DurationExceedsLimit)
);
assert_eq!(dt.duration_trunc(TimeDelta::zero()), Err(RoundingError::DurationExceedsLimit));

assert_eq!(
dt.duration_trunc(TimeDelta::try_milliseconds(10).unwrap()).unwrap().to_string(),
"2016-12-31 23:59:59.170"
Expand Down

0 comments on commit 391187f

Please sign in to comment.