-
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
Introduce calendar systems to time #1062
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.
This is looking good, is the plan to merge it and work on PRs for documentation and spec separately, or add those to this PR?
I do think that tests for time.getISOFields()
should be added as part of this PR though.
As well, we could consider adding a calendar to test/usercalendar.mjs
that exercises this functionality.
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.
Sorry, I missed the first time around, that Time.add, Time.subtract, Time.until, and Time.since need to use calendar.timeAdd, calendar.timeSubtract, calendar.timeUntil.
void constructor; | ||
throw new Error('not implemented'); | ||
} | ||
timeSubtract(time, duration, options, constructor) { |
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 do we need both timeAdd
and timeSubtract
? Why not just timeAdd
? Same question for dates, FWIW. Also, given that both time and date both use calendars now, why have separate methods for them? Why not just have a dateTimeAdd
that serves both purposes? I assume there are good reasons for the split, just wondering what they are.
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.
Related: #1066 (for dates). But if we're adding time methods for the first time, why not just have one?
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.
The original calendar draft had only one add() and one subtract() method. I can't remember exactly, but it may not be possible to combine dateAdd() and timeAdd() since the allowed ranges for all the Plain types are different. But it may be possible to keep that range checking in the callers.
NANOSECOND, | ||
ISO_HOUR, | ||
ISO_MINUTE, | ||
ISO_SECOND, |
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.
For seconds and smaller units, do they need to be calendar-aware? Are there any use cases we know about that require calendar-specific measurement of seconds or nanoseconds? Or are we just making them calendar-aware for consistency?
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 we should probably rename them to ISO in any case, but yes it does seem that there should not be use cases where 1000 milliseconds ≠ 1 second.
I didn't review this deeply because I'm not familiar enough with the implementation, but I left a few notes. One other related thing I noticed: the phrase "calendar units" shows up in a bunch of places in Temporal polyfill code and a few places in the spec. In the past these were synonyms for "may not be fixed-length units" but now that all units are calendar units, we probably need a different term to replace "calendar units" with. How about "fixed-length units" and "variable-length units" ? |
6e499a9
to
806984a
Compare
597603b
to
185aa2c
Compare
Codecov Report
@@ Coverage Diff @@
## main #1062 +/- ##
===========================================
- Coverage 93.59% 41.40% -52.20%
===========================================
Files 19 18 -1
Lines 7702 3794 -3908
Branches 1224 775 -449
===========================================
- Hits 7209 1571 -5638
- Misses 486 2003 +1517
- Partials 7 220 +213
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
e75798c
to
f53e812
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'm afraid that Duration rounding and balancing will be a big missing part of this, but let's make sure that what is in this PR can be merged soon.
polyfill/lib/datetime.mjs
Outdated
nanosecond, | ||
overflow | ||
)); | ||
const time = calendar.timeFromFields(fields, options, GetIntrinsic('%Temporal.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.
const time = calendar.timeFromFields(fields, options, GetIntrinsic('%Temporal.Time%')); | |
const time = calendar.timeFromFields(fields, { overflow }, GetIntrinsic('%Temporal.Time%')); |
I think we are normally creating a new options object to pass to the calendar methods instead of passing through the user-supplied one.
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.
@ptomato check the dateFromFields
call right above this. It passes in options
directly. I spotted a lot of such issues early-on and even made a few refactor commits that I later dropped to keep this PR as small and on-topic as possible. Should I fix all sites in this PR, or would a single follow-up that takes care of all such cases suffice?
GetSlot(other, MICROSECOND), | ||
GetSlot(other, NANOSECOND) | ||
let { days: deltaDays, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = calendar.timeUntil( | ||
this.toTime(), |
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.
These should be abstract operations so that the call to toTime
isn't observable (though feel free to defer this to your next PR)
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.
Will do!
ConsolidateCalendars: (one, two) => { | ||
const sOne = ES.CalendarToString(one); | ||
const sTwo = ES.CalendarToString(two); | ||
if (sOne === sTwo || sOne === 'iso8601') { |
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 thought we had removed the special handling of the ISO calendar as a "null" calendar everywhere else — double check with @sffc who probably remembers better than I 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'll reach out to @sffc about this, thanks.
GetSlot(other, MICROSECOND), | ||
GetSlot(other, NANOSECOND) | ||
); | ||
let { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = calendar.timeUntil(this, other); | ||
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.RoundDuration( |
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 is going to be a lot of work, but I believe RoundDuration will need to round according to the calendar's length of hours and minutes? It's fine to not block this PR on it.
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.
Well, wasn't the plan to keep Durations ISO-only? But I think I get your point more or less. Since Time.p.add(duration)
interprets the duration in a calendar-specific manner, Time.p.until
should produce a Duration based on the calendar. Let's make an issue to discuss this more in detail and land this? We can either generalize this as calendar.durationRound
or just add the logic to the until
/since
methods on the calendar.
IIUC, all outstanding comments have been addressed and this should be ready to merge. However, a couple of things need to be worked out in follow-ups. In the meantime, I'd also start working on the spec text and docs for this change. Thanks everyone! |
240482b
to
fc5fdf2
Compare
fc5fdf2
to
8e4de7b
Compare
Travis is having issues, so I tested locally before merging. |
Fixes: #522
/cc @ptomato