Use overflowing_naive_local in DateTime::checked_add*#1333
Merged
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1333 +/- ##
==========================================
+ Coverage 92.11% 92.14% +0.03%
==========================================
Files 40 40
Lines 18026 18089 +63
==========================================
+ Hits 16604 16668 +64
+ Misses 1422 1421 -1 ☔ View full report in Codecov by Sentry. |
d895c51 to
e479cb8
Compare
This was referenced Feb 2, 2024
e479cb8 to
f23dcba
Compare
Collaborator
Author
djc
approved these changes
Feb 29, 2024
Collaborator
Author
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The final part of #1048.
Using
overflowing_naive_localinDateTime::checked_(add|sub)_daysandDateTime::checked_(add|sub)_monthsprevents panics.I thought it would be good if users can rely on another property: that a clear no-op works as a no-op. I.e.
DateTime::checked_add_days(Days::new(0))should always return the original value.To do that,
checked_(add|sub)_daysandchecked_(add|sub)_monthshave a fast path that returnsSome(self)if it is a no-op.They already had a fast path anyway, we just start relying on it.
with_yeargains a fast path to have the same property.What we can't promise is that you can add or subtract and create a value that is just out of range for the local time but still in range for the UTC value. That would require alternative methods on
NaiveDateforadd_days,checked_*_monthsandwith_yearthat have a different bound checks. It just doesn't seem worth it to me.