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

Negative durations #811

Merged
merged 4 commits into from
Sep 1, 2020
Merged

Negative durations #811

merged 4 commits into from
Sep 1, 2020

Conversation

ptomato
Copy link
Collaborator

@ptomato ptomato commented Aug 11, 2020

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

@codecov
Copy link

codecov bot commented Aug 11, 2020

Codecov Report

Merging #811 into main will increase coverage by 0.15%.
The diff coverage is 99.47%.

Impacted file tree graph

@@            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              
Flag Coverage Δ
#test262 56.48% <52.57%> (-1.03%) ⬇️
#tests 88.01% <98.16%> (+0.40%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
polyfill/lib/calendar.mjs 67.82% <89.47%> (+1.15%) ⬆️
polyfill/lib/absolute.mjs 96.39% <100.00%> (ø)
polyfill/lib/date.mjs 90.87% <100.00%> (-0.57%) ⬇️
polyfill/lib/datetime.mjs 94.04% <100.00%> (+0.10%) ⬆️
polyfill/lib/duration.mjs 97.70% <100.00%> (+0.68%) ⬆️
polyfill/lib/ecmascript.mjs 94.74% <100.00%> (-0.21%) ⬇️
polyfill/lib/regex.mjs 100.00% <100.00%> (ø)
polyfill/lib/time.mjs 97.17% <100.00%> (+0.55%) ⬆️
polyfill/lib/yearmonth.mjs 94.95% <100.00%> (-0.30%) ⬇️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 102b0be...93c64e2. Read the comment docs.

docs/balancing.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
polyfill/lib/regex.mjs Outdated Show resolved Hide resolved
docs/time.md Outdated Show resolved Hide resolved
@justingrant
Copy link
Collaborator

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!

docs/balancing.md Outdated Show resolved Hide resolved
@ptomato
Copy link
Collaborator Author

ptomato commented Aug 12, 2020

Thanks for the reviews! There were definitely some good catches in there.

@ptomato
Copy link
Collaborator Author

ptomato commented Aug 15, 2020

This now requires #826.

Copy link
Member

@ryzokuken ryzokuken left a 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.

spec/time.html Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
polyfill/lib/date.mjs Outdated Show resolved Hide resolved
polyfill/lib/date.mjs Outdated Show resolved Hide resolved
polyfill/lib/time.mjs Outdated Show resolved Hide resolved
spec/duration.html Show resolved Hide resolved
spec/duration.html Show resolved Hide resolved
spec/duration.html Outdated Show resolved Hide resolved
@ptomato
Copy link
Collaborator Author

ptomato commented Aug 24, 2020

Thanks for the review, definitely caught some stuff I didn't see! I believe it's ready for review again.

@ptomato ptomato force-pushed the 782-negative-duration branch 2 times, most recently from da69c47 to cebc5f6 Compare August 25, 2020 18:44
@ptomato ptomato added this to the Stable API milestone Aug 27, 2020
@ptomato ptomato force-pushed the 782-negative-duration branch 2 times, most recently from 2944709 to ebd4aa0 Compare August 28, 2020 20:00
Copy link
Collaborator

@sffc sffc left a 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.

Copy link
Member

@ryzokuken ryzokuken left a 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!

docs/duration.md Outdated Show resolved Hide resolved
docs/duration.md Outdated Show resolved Hide resolved
polyfill/lib/datetime.mjs Show resolved Hide resolved
spec/time.html Outdated Show resolved Hide resolved
<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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Collaborator Author

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
Copy link
Member

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
Copy link
Member

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.
Copy link
Member

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
@ptomato ptomato merged commit 7f8ad58 into main Sep 1, 2020
@ptomato ptomato deleted the 782-negative-duration branch September 1, 2020 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants