-
Notifications
You must be signed in to change notification settings - Fork 543
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
Conversation
codecov error seems unrelated |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1474 +/- ##
=======================================
Coverage 91.78% 91.78%
=======================================
Files 37 37
Lines 18167 18176 +9
=======================================
+ Hits 16674 16683 +9
Misses 1493 1493 ☔ View full report in Codecov by Sentry. |
@Dav1dde Thank you for fixing this! My opinion of this module remains low 😞. In theory rounding or truncating to multiples of 0 (nano)seconds doesn't make sense. Wouldn't it be better to return an error instead? |
In my usecase returning the original value is what I would need. Thinking about it a bit more, to get that same behaviour it should also be possible to truncate by the smallest represented unit in the timestamp (1 ns), which does make more sense. But I think being consistent with |
If @djc agrees I would like to see both rounding and truncating return an error on zero, and leave it up to the caller to use sane arguments. I.e. make not only negative values return |
@Dav1dde what are you using these methods for? What are you trying to achieve? |
Aggregating data by timestamp with a given granularity. Zero was just the first thing I reached for. Though 1ns would make technically more sense, it also requires you to know the minimum precision and in an interface that only accepts seconds While an error makes sense it is also a lot more annoying to deal with. You have to match exactly on the variant, then hope zero has it's own variant, or just check for zero before passing it |
@Dav1dde Would you be willing to update this PR to return consistently return an error when rounding to multiples of zero? In your situation if a function accepts |
Sure, if that is the desired behaviour. While overall the error makes more sense, I don't think that is a good idea. Because it is different behaviour to what |
Yes, both should error. |
Should I update both? |
Please 😄 |
@pitdicker updated! Also added tests for negative values. Edit: Any changelog I should update? |
duration_trunc
ea31b26
to
969f1f9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, looks good!
We add release notes to GitHub releases, nothing you need to update.
@@ -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. |
There was a problem hiding this comment.
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.
@djc Do you have time to look at this PR? |
Will have a look tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (but let's squash during merge?).
The division by zero (modulo) panics,
duration_round
already checks for 0 and returns the original value.