-
Notifications
You must be signed in to change notification settings - Fork 158
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
Conflict between docs and polyfill for cloning using TimeZone.from and Calendar.from #810
Comments
@gibson042 - what about custom timezone and calendar implementations? Should |
The current behaviour is on fairly shaky ground because of custom time zone and calendar implementations. I think we don't want to make people override @Ms2ger has been advocating for making TimeZone.from and Calendar.from not return new objects for that reason. |
I'm not familiar with the current data models for those types, but the primary purpose of |
I thought |
Yeah, that makes sense. |
I ran into this when I was trying to clone Temporal objects. That's probably the only use case. |
Meeting 2020-08-13: consensus is that One thing that I don't think we discussed in the meeting: should the implementation of methods like |
I think cloning should be unnecessary in those cases, in order to be consistent with custom calendars. |
@Ms2ger I started on this before I noticed you had already started. Luckily nothing I did overlapped with what you had already done, because I started with the documentation. I pushed my work to your branch, feel free to squash it into yours. |
Don't forget to update the TS types to remove object types. Thanks! |
TypeScript helped me find a problem with this approach. Some methods on export interface TimeZoneProtocol {
id?: string;
getOffsetNanosecondsFor(instant: Temporal.Instant): number;
getOffsetStringFor?(instant: Temporal.Instant): string;
getZonedDateTimeFor?(instant: Temporal.Instant, calendar?: CalendarProtocol | string): Temporal.ZonedDateTime;
getDateTimeFor(instant: Temporal.Instant, calendar?: CalendarProtocol | string): Temporal.DateTime;
getInstantFor?(dateTime: Temporal.DateTime, options?: ToInstantOptions): Temporal.Instant;
getNextTransition?(startingPoint: Temporal.Instant): Temporal.Instant | null;
getPreviousTransition?(startingPoint: Temporal.Instant): Temporal.Instant | null;
getPossibleInstantsFor(dateTime: Temporal.DateTime): Temporal.Instant[];
toString(): string;
toJSON?(): string;
} Assuming that this declaration is correct, then the only required methods are Before ZonedDateTime this was not a problem because custom time zone objects are not persisted anywhere. So the only place you could get a TimeZoneProtocol was userland code. But with ZonedDateTime, there's now a I can think of two ways to solve this:
I assume that (1) seems like the safer approach, given that custom timezones is an advanced use case. Making it harder seems like a reasonable compromise to better support non-advanced use cases of calling TimeZone methods and properties. |
Despite today's long discussion, I wonder if we're not actually disagreeing on the outcome? For example, I wrote up some docs content for the custom time zone docs. Would the content below be OK for the custom time zone docs?
If folks are OK with the content above, then I'd recommend we change the TS type of |
To me this text is too long given the tiny, advanced-user edge case that it documents, but I think it's a good start and I'd be happy to edit it down a bit. I think you are right that everyone is agreed on this part. I've opened #1040 for documenting this.
This is what you and @pipobscure disagree on. Personally I would prefer that we don't change the TypeScript type, so if you write TypeScript code you can make your own choice whether you want to treat it as a potentially minimally-implemented TimeZoneProtocol (you leave the type as is and TypeScript will prevent you from calling one of the potentially missing methods); or you explicitly choose to ignore that possibility by saying Having opened those two issues, I believe we can close this one? |
Yep let's close this issue. Thanks! Also, the sample implementation of a custom time zone in my original post had some bugs. I fixed it above. Would you be OK to edit #1040 as well to keep the code correct in both places? |
Will do, thanks. |
The TimeZone docs say:
But this isn't actually true in the current polyfill implementation. If the argument is an object, then it's returned as-is without creating a new object.
proposal-temporal/polyfill/lib/timezone.mjs
Lines 244 to 251 in c929bd1
Which one is correct?
The Calendar class has the same docs vs. code mismatch:
proposal-temporal/polyfill/lib/calendar.mjs
Lines 107 to 112 in c929bd1
BTW, I found this because I was wondering about the TS signature of
TimeZone.from
. Currently it's this:But shouldn't it accept a
Temporal.TimeZoneProtocol | string
instead, to support cloning custom time zone objects? Or is this use case deliberately excluded because we don't want to clone objects that Temporal didn't create? If so, then should the polyfill enforce that exclusion by throwing if an object is passed that's not aTemporal.TimeZone
? I assume whatever we do here we'd also want to do in Calendar. FWIW, the Calendar class accepts a CalendarProtocol in its TS typings forfrom
.The text was updated successfully, but these errors were encountered: