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

Time zone and calendar protocol best practices #1043

Merged
merged 3 commits into from
Oct 26, 2020

Conversation

ptomato
Copy link
Collaborator

@ptomato ptomato commented Oct 22, 2020

Closes: #1040

@codecov
Copy link

codecov bot commented Oct 22, 2020

Codecov Report

Merging #1043 into main will decrease coverage by 0.31%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1043      +/-   ##
==========================================
- Coverage   94.30%   93.99%   -0.32%     
==========================================
  Files          18       18              
  Lines        6657     6662       +5     
  Branches      998     1048      +50     
==========================================
- Hits         6278     6262      -16     
- Misses        372      391      +19     
- Partials        7        9       +2     
Flag Coverage Δ
#test262 48.00% <ø> (-0.05%) ⬇️
#tests 90.43% <ø> (ø)

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

Impacted Files Coverage Δ
polyfill/lib/instant.mjs 91.34% <0.00%> (-5.88%) ⬇️
polyfill/lib/intl.mjs 98.74% <0.00%> (-1.26%) ⬇️

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 efdb545...e99474d. Read the comment docs.

docs/timezone.md Outdated Show resolved Hide resolved
docs/timezone.md Outdated Show resolved Hide resolved
docs/timezone.md Outdated Show resolved Hide resolved
docs/calendar.md Outdated
It must not have a `calendar` property, so that it can be distinguished in `Temporal.Calendar.from()` from other Temporal objects that have a calendar.
It is possible to pass such an object into any Temporal API that would normally take a built-in `Temporal.Calendar`.

Any implementation of the protocol with the required methods will return the correct output from any Temporal property or method.
However, most other code will assume that custom calendars act like built-in `Temporal.Calendar` objects.
If you want to interoperate with libraries or other code that you didn't write, then you should implement the `id` property and the `fields()` method as well.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't get why id and fields() are optional. What happens if Temporal gets a calendar without those fields and some method that needs them is called (like toString for .id or getFields() for fields()?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that id defers to toString, not the other way around.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If fields isn't present on the object, then %Temporal.Calendar.prototype.fields% is called, which just clones the input array. It's optional to override fields if you subclass, so it should be optional to provide it on a plain-object calendar. This is consistent with what we do for the TimeZone methods that are optional to override when you subclass TimeZone.

The default implementation of TimeZone.getDateTimeFor() uses
TimeZone.getOffsetNanosecondsFor(), so it's not required to implement this
method in the protocol.
We have TimeZone.id optional on plain objects, in favour of requiring
TimeZone.toString(). The calendar protocol should do the same thing.
Emphasize that inheriting from the built-in objects is the recommended way
to create custom time zones and calendars, and give details about what the
default implementations of each of the optional methods do.

Closes: #1040
@ptomato ptomato force-pushed the 1040-custom-timezone-best-practice branch from 8563374 to e99474d Compare October 23, 2020 20:28
@Ms2ger Ms2ger merged commit 770a1a2 into main Oct 26, 2020
@Ms2ger Ms2ger deleted the 1040-custom-timezone-best-practice branch October 26, 2020 10:58
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.

Emphasize best practices for creating custom time zones and calendars
4 participants