-
Notifications
You must be signed in to change notification settings - Fork 542
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
Deprecate panicking constructors of TimeDelta
#1450
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1450 +/- ##
==========================================
+ Coverage 91.95% 92.03% +0.07%
==========================================
Files 40 40
Lines 18035 18163 +128
==========================================
+ Hits 16584 16716 +132
+ Misses 1451 1447 -4 ☔ View full report in Codecov by Sentry. |
578771b
to
cf8765e
Compare
TimeDelta
for deprecating panicking constructorsTimeDelta
cf8765e
to
006d58d
Compare
src/naive/date/mod.rs
Outdated
@@ -1140,7 +1140,8 @@ impl NaiveDate { | |||
let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400); | |||
let cycle1 = yo_to_cycle(year1_mod_400 as u32, self.ordinal()) as i64; | |||
let cycle2 = yo_to_cycle(year2_mod_400 as u32, rhs.ordinal()) as i64; | |||
TimeDelta::days((year1_div_400 as i64 - year2_div_400 as i64) * 146_097 + (cycle1 - cycle2)) | |||
let days = (year1_div_400 as i64 - year2_div_400 as i64) * 146_097 + (cycle1 - cycle2); | |||
expect!(TimeDelta::try_days(days), "range of NaiveDate is MUCH less than TimeDelta") |
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.
This error message is a bit too opinionated for my taste, can we just say it is out of range?
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.
It is const but otherwise functionally the same as
TimeDelta::try_days(days).unwrap() // range of NaiveDate is MUCH less than TimeDelta
The doc comment a little above says:
/// This does not overflow or underflow at all,
/// as all possible output fits in the range of `TimeDelta`.
The range of TimeDelta
is ca. 585 million years, the range of NaiveDate
ca. 525.000 years.
I can change it to "always in range"
.
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.
That works for me.
src/naive/datetime/mod.rs
Outdated
|
||
let date = try_opt!(self.date.checked_add_signed(TimeDelta::seconds(rhs))); | ||
let (time, rhs_days) = self.time.overflowing_add_signed(rhs); | ||
let rhs_days = try_opt!(TimeDelta::try_seconds(rhs_days)); |
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.
It seems pretty confusing to pass something called rhs_days
to try_seconds()
.
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.
I suppose remainder
is less confusing.
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.
Sounds good.
@@ -307,7 +307,7 @@ fn test_nanosecond_range() { | |||
// Far beyond range | |||
let maximum = "2262-04-11T23:47:16.854775804UTC"; | |||
let parsed: DateTime<Utc> = maximum.parse().unwrap(); | |||
let beyond_max = parsed + TimeDelta::days(365); | |||
let beyond_max = parsed + Days::new(365); |
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.
Hmm, maybe we should resurrect your work on CalendarDuration
?
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.
I'd like to. Part 1 is ready for review 😄. Maybe wait a few more weeks until we are good underway with the Result
stuff?
src/time_delta.rs
Outdated
@@ -102,6 +102,10 @@ impl TimeDelta { | |||
/// Panics when the duration is out of bounds. | |||
#[inline] | |||
#[must_use] | |||
#[deprecated( | |||
since = "0.4.35", | |||
note = "Use `TimeDelta::try_weeks` instead or consider the `Days` type" |
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.
So I don't really want to recommend the Days
type here.
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.
Okay, I'll remove it.
006d58d
to
6a1be47
Compare
src/naive/datetime/mod.rs
Outdated
} | ||
|
||
let date = try_opt!(self.date.checked_add_signed(TimeDelta::seconds(rhs))); | ||
let (time, rhs_days) = self.time.overflowing_add_signed(rhs); |
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.
rhs_days
-> remainder
here as well?
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.
Oops, stupid!
6a1be47
to
563c53d
Compare
This PR touches a lot of lines, but I would only call the first 5 commits and the last one interesting.
TimeDelta::days
andTimeDelta::weeks
with theDays
type, which should be a little faster and conceptually a better match.NaiveDateTime::{checked_add_signed, checked_sub_signed}
we could remove a workaround now that we havetry_seconds
.Parsed::to_naive_date
I factored out the logic to calculate a date from a week starting on Monday or Sunday toresolve_week_date
. And instead of calculating aTimeDelta
with the number of days to add to January 1 we just calculate the ordinal directly and set the ordinal.try_
variant.Fixes #1408.