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

Duration.compare() #1163

Merged
merged 3 commits into from
Nov 11, 2020
Merged

Duration.compare() #1163

merged 3 commits into from
Nov 11, 2020

Conversation

ptomato
Copy link
Collaborator

@ptomato ptomato commented Nov 9, 2020

Closes: #856

@codecov
Copy link

codecov bot commented Nov 9, 2020

Codecov Report

Merging #1163 (40d3927) into main (5930868) will decrease coverage by 2.80%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1163      +/-   ##
==========================================
- Coverage   93.55%   90.75%   -2.81%     
==========================================
  Files          19       17       -2     
  Lines        7961     8913     +952     
  Branches     1264     1260       -4     
==========================================
+ Hits         7448     8089     +641     
- Misses        506      811     +305     
- Partials        7       13       +6     
Flag Coverage Δ
test262 ?
tests 90.75% <ø> (+1.31%) ⬆️

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

Impacted Files Coverage Δ
polyfill/lib/shim.mjs
polyfill/lib/legacydate.mjs
polyfill/lib/intrinsicclass.mjs
polyfill/lib/slots.mjs
lib/timezone.mjs 89.91% <0.00%> (ø)
lib/now.mjs 100.00% <0.00%> (ø)
lib/plaindate.mjs 87.02% <0.00%> (ø)
lib/intrinsicclass.mjs 56.81% <0.00%> (ø)
lib/intl.mjs 99.74% <0.00%> (ø)
lib/regex.mjs 100.00% <0.00%> (ø)
... and 11 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 a0e0a75...6c877a8. Read the comment docs.

Copy link
Collaborator

@justingrant justingrant left a comment

Choose a reason for hiding this comment

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

Looks good, other than not adjusting for DST which I assume will be in a later PR.

polyfill/lib/duration.mjs Show resolved Hide resolved
polyfill/index.d.ts Outdated Show resolved Hide resolved
@ptomato ptomato requested a review from Ms2ger November 9, 2020 17:49
@ptomato ptomato force-pushed the 856-duration-compare branch 2 times, most recently from ad6ef47 to 4346e12 Compare November 10, 2020 04:36
Copy link
Collaborator

@justingrant justingrant left a comment

Choose a reason for hiding this comment

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

Overall this looks fine, except I didn't understand why there's code to accommodate ZDT and DST changes but no tests that cover the case where a DST change is inside the duration. Is that case expected to work after this PR, and if so then why no tests?

docs/duration.md Show resolved Hide resolved
polyfill/lib/ecmascript.mjs Show resolved Hide resolved
polyfill/lib/ecmascript.mjs Outdated Show resolved Hide resolved
polyfill/lib/ecmascript.mjs Outdated Show resolved Hide resolved
polyfill/test/duration.mjs Show resolved Hide resolved
@ptomato
Copy link
Collaborator Author

ptomato commented Nov 10, 2020

Overall this looks fine, except I didn't understand why there's code to accommodate ZDT and DST changes but no tests that cover the case where a DST change is inside the duration. Is that case expected to work after this PR, and if so then why no tests?

Sorry for the confusion about this — those cases are not yet expected to work after this PR, so that's why I didn't add those tests. Only compare() has a relativeTo that actually works as expected with ZonedDateTime. The other methods (round(), total(), add(), subtract()) accept ZonedDateTime after this PR, but still always treat days as 24 hours. I am working on (hopefully only) one more followup PR that should make the rest of these work with DST shifts.

<emu-alg>
1. If _days_ ≠ 0, then
1. Set _nanoseconds_ to ℝ(_nanoseconds_) − ℝ(_offsetShift_).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Followup: this is not great - nanoseconds ends up being a JS Number or a Mathematical Value depending on the value of days. Let's convert it unconditionally before the days check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will address this in the next PR, thanks.

This operation gives the total number of nanoseconds in an exact
duration as a bigint, not losing precision. This will be reused in
Duration.compare().

This operation takes an "offset shift" parameter, used if there are days
that need to be balanced down to hours. If it's 0 (as is the case
everywhere in this commit) then days are always 24 hours. We will start
using nonzero values for this in Duration.compare().

See: #856
Everywhere a relativeTo option is accepted, allow a ZonedDateTime instance
or a value that is convertible to one. If converting a value,
preferentially convert to ZonedDateTime if there is enough information
(i.e. the time zone name is present), only if not then convert to
PlainDateTime.
Implements a Duration.compare() static method with a relativeTo option
for comparing durations with date units.

See: #856
@Ms2ger Ms2ger merged commit 47506ca into main Nov 11, 2020
@Ms2ger Ms2ger deleted the 856-duration-compare branch November 11, 2020 09:46
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.

Proposal: rounding and balancing for Duration type (replaces #789)
3 participants