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

Use overflowing_naive_local in DateTime::checked_add* #1333

Merged
merged 4 commits into from
Feb 29, 2024

Conversation

pitdicker
Copy link
Collaborator

@pitdicker pitdicker commented Sep 28, 2023

The final part of #1048.

Using overflowing_naive_local in DateTime::checked_(add|sub)_days and DateTime::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 and checked_(add|sub)_months have a fast path that returns Some(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 for add_days, checked_*_months and with_year that have a different bound checks. It just doesn't seem worth it to me.

@codecov
Copy link

codecov bot commented Sep 28, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.14%. Comparing base (02c68d6) to head (f23dcba).

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.
📢 Have feedback on the report? Share it here.

@pitdicker
Copy link
Collaborator Author

@djc May I ask for this PR as a next one to review?
If @Zomtir wants to work on NaiveDate::checked_add* and NaiveDateTime::checked_add* (and with_year...) it would be nice to have these fixes in.

@pitdicker pitdicker merged commit ca51f69 into chronotope:main Feb 29, 2024
35 checks passed
@pitdicker pitdicker deleted the datetime_min_max_panics branch February 29, 2024 12:16
@pitdicker
Copy link
Collaborator Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants