-
Notifications
You must be signed in to change notification settings - Fork 155
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
Negative durations #811
Negative durations #811
Conversation
Codecov Report
@@ Coverage Diff @@
## main #811 +/- ##
==========================================
+ Coverage 92.74% 92.89% +0.15%
==========================================
Files 17 17
Lines 4881 5055 +174
Branches 754 773 +19
==========================================
+ Hits 4527 4696 +169
- Misses 347 352 +5
Partials 7 7
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Nice! I left a few comments, mostly on the docs. Nothing major. I didn't dig deeply into the polyfill implementation or the specs. It's nice to see how much simpler a few of the cookbook samples got with negative durations. Thanks for doing this-- huge improvement! |
c7fd2d9
to
5df5d82
Compare
Thanks for the reviews! There were definitely some good catches in there. |
5df5d82
to
51a801c
Compare
51a801c
to
05a2b14
Compare
This now requires #826. |
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.
Partially reviewed, and wow, it's huge.
84bed92
to
9d9326a
Compare
Thanks for the review, definitely caught some stuff I didn't see! I believe it's ready for review again. |
da69c47
to
cebc5f6
Compare
2944709
to
ebd4aa0
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.
It looks good from spot-checking, but I didn't do a thorough review.
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.
What a long pull request! Final few suggestions. Thanks a ton, @ptomato!
<emu-clause id="sec-temporal-durationsign" aoid="DurationSign"> | ||
<h1>DurationSign ( _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_ )</h1> | ||
<emu-alg> | ||
1. For each value _v_ of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, do |
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.
1. For each value _v_ of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, do | |
1. For each value _v_ of « _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_ », do |
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'm not sure what the correct spec language is, but there seems to be a precedent for not using the angle brackets: https://tc39.es/ecma402/#sec-todatetimeoptions
<h1>RejectDurationSign ( _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_ )</h1> | ||
<emu-alg> | ||
1. Let _sign_ be ! DurationSign(_years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_). | ||
1. For each value _v_ of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, do |
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.
Same.
<emu-clause id="sec-temporal-validatetemporalduration" aoid="ValidateTemporalDuration"> | ||
<h1>ValidateTemporalDuration ( _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_ )</h1> | ||
<emu-alg> | ||
1. For each of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, if any of the values is negative or infinite, return *false*. | ||
1. Let _sign_ be ! DurationSign(_years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_). | ||
1. For each value _v_ of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, do |
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.
Same.
1. If _disambiguation_ is *"reject"*, then | ||
1. If ! ValidateTemporalDuration(_years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_) is *false*, then | ||
1. Throw a *RangeError* exception. | ||
1. Else if _disambiguation_ is *"constrain"*, then | ||
1. For each of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, if any of the values is negative, throw a *RangeError* exception. | ||
1. For each of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, if any of the values is infinite, let it be the largest possible finite value of the Number type. | ||
1. For each of _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, if any of the values is infinite, let it be the sign of that value times the largest possible finite value of the Number 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.
Does the last comment apply here too? Not sure, but maybe.
It's odd that we have AddTime but not SubtractTime. Regardless, we'll need to have it as a separate operation since it will be used from two places for negative duration support.
Changes Temporal.Duration to allow negative values in the fields, as long as all the fields have the same sign. Adds negated() and abs() methods to Temporal.Duration, and a sign property. On all types with arithmetic, subtracting a negative duration is equivalent to adding the absolute value of that duration, and adding a negative duration is equivalent to subtracting the absolute value of that duration. Closes: #782
This undoes the work in commit d84653e although it's not exactly a revert, because the situation before that was that durations were always positive. We also don't reinstate the behaviour of never returning a duration larger than 12 hours. Now, for all types, calling smaller.difference(larger) will result in a negative duration. See: #782
ebd4aa0
to
93c64e2
Compare
This is a huge pull request; I'd suggest reviewing it commit by commit.
The first two commits are small unrelated fixes. The third is a small rearrangement. The fourth actually allows negative durations, but the difference() methods still throw if called on two objects in the wrong order. The fifth commit fixes that and allows all the difference() methods to return negative durations.
Closes: #558
Closes: #782
Closes: #645