-
Notifications
You must be signed in to change notification settings - Fork 592
Use core::time::Duration with TimeDelta as output type only #824
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
Conversation
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.
Great work! Some thoughts/suggestions from an initial round of skimming.
7729acd
to
4470857
Compare
will move f6dceba into the proper commits once I've got the CI passing |
8961e14
to
b7266a1
Compare
a2b9285
to
d7fa4de
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.
I think the constituent pieces here are looking good. In terms of the commit history, I think instead of leaving the removal of all code related to the OldTimeDelta
until the end, each commit that adds implementations for Duration
should also remove the similar implementations for OldTimeDelta
. That way it's much easier to review if we're adding back equivalent operations.
You also added DaysDelta
and MonthsDelta
. It seems like these are additive (that is, they don't exactly replace equivalent functionaly that would be removed with OldTimeDelta
)? If they are not additive, it would be good to demonstrate that using the same approach (removing code in the same commit as these are added).
src/time_delta.rs
Outdated
|
||
#[derive(Clone, Copy, PartialOrd, Ord, Debug)] | ||
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] | ||
/// A difference (delta) in time build using [`core::time::Duration`] and a direction |
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.
Nit: first line of the docstring should fit on one line.
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.
should be resolved in the latest force push
src/naive/time/mod.rs
Outdated
(time, -rhs) // safe to negate, rhs is within +/- (2^63 / 1000) | ||
} | ||
|
||
/// Adds given `TimeDelta` to the current time, |
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.
Nit: shorter first docstring line.
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.
should be resolved in the latest force push
src/naive/time/mod.rs
Outdated
/// assert_eq!(from_hms(3, 4, 5).checked_overflowing_add(Duration::from_secs(60 * 60 * 23)), | ||
/// Some((from_hms(2, 4, 5), Days::new(1)))); | ||
/// ``` | ||
pub fn checked_overflowing_add(&self, rhs: Duration) -> Option<(NaiveTime, 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.
Where's the precedent for this name? What does overflowing mean here? Conceptually it seems like checked_
and overflowing_
would usually be mutually exclusive.
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've renamed to overflowing_add_opt
but perhaps there is a better option.
The original intention was to use checked
because of the Option
and overflowing
represents the fact that it is ok for the Duration
to be longer than the time to the end of the day.
// this has the effect that if the smaller time is in a leap second, we can simply remove `SECOND_AS_NANOS` | ||
// from the frac, and then calculate as normal | ||
|
||
match self.secs.cmp(&rhs.secs) { |
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.
Why is this related to the TimeDelta
work?
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 used to rely on the fact that the duration could potentially be negative or positive, now that it can't and so the situation is a bit more complex and the signed integers are harder to work with.
An option that works which is closer to the original implementation is:
let mut secs = i64::from(self.secs) - i64::from(rhs.secs);
let mut frac = i64::from(self.frac) - i64::from(rhs.frac);
// `secs` may contain a leap second yet to be counted
let adjust = match self.secs.cmp(&rhs.secs) {
Ordering::Greater => {
if rhs.frac >= 1_000_000_000 {
1
} else {
0
}
}
Ordering::Equal => 0,
Ordering::Less => {
if self.frac >= 1_000_000_000 {
-1
} else {
0
}
}
};
secs += adjust;
while frac < 0 {
frac += 1_000_000_000;
secs -= 1;
}
match secs.cmp(&0) {
Ordering::Equal => {
TimeDelta::Forwards(Duration::new(0, u32::try_from(frac).unwrap()))
}
Ordering::Greater => {
TimeDelta::Forwards(Duration::new(u64::try_from(secs).unwrap(), u32::try_from(frac).unwrap()))
}
Ordering::Less => {
TimeDelta::Backwards(Duration::new(u64::try_from(secs.abs()).unwrap(), u32::try_from(frac.abs()).unwrap()))
}
}
let me know which you think is better? One benefit of the other way is that it doesn't have any unwrap
s
22734af
to
aeb8aae
Compare
aeb8aae
to
670e133
Compare
* add shims between OldTimeDelta and TimeDelta where needed * Remove OldTimeDelta parts from NaiveTime code
* remove problematic code from Date<Tz> - this will likely be fully removed before this is merged anyway * add shims between OldTimeDelta and TimeDelta where needed * remove OldTimeDelta code in NaiveDate
* these are done in one commit as they are very intertwined, eg via round.rs * removing OldTimeDelta where it is no longer used
670e133
to
74b087f
Compare
Don't have time to review this in detail today. One question on my mind: is there some way we can start exposing parts of this change on 0.4.x already? Presumably we could just add the |
This should be quite possible. Almost all the methods have new names as well. The PR is actually more problematic in As an additional thought, what do you think about deprecating the Do you think this would be better structured as a PR to |
I think it would be great to first do a PR to 0.4.x which adds new API and deprecates old stuff (I forget if/how well deprecations on Good point about |
Personally I don't think an enum wrapping an I'm closing this PR for now because it hasn't seen any activity in the last year while it is going to need a lot of work to become merge-able. |
Setting this as draft while I figure out a good way to split up the commit into manageable units