Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when rounding with a zero duration #1474

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Another weirdness in this module. 'will fail if TimeDelta is bigger than the timestamp.' So rounding to hours will fail for values within the first hour of 1970-01-01 😞. Don't worry about it for this PR though.

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
Loading