Use overflowing_naive_local
in DateTime::checked_add*
#1333
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_local
inDateTime::checked_(add|sub)_days
andDateTime::checked_(add|sub)_months
prevents 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)_days
andchecked_(add|sub)_months
have 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_year
gains 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
NaiveDate
foradd_days
,checked_*_months
andwith_year
that have a different bound checks. It just doesn't seem worth it to me.