tc39 / proposal-temporal

Provides standard objects and functions for working with dates and times.
https://tc39.es/proposal-temporal/docs/
Other
3.35k stars 153 forks source link

Interop with "plain" JS Dates? #299

Closed alanhussey closed 4 years ago

alanhussey commented 4 years ago

Hi, I've been lurking for a bit, so I don't think this has been discussed yet (and my search attempts haven't turned anything up).

What (if any) is the story around interoperability with existing Date? I understand that in a greenfield application it doesn't make a lot of sense to mix usage of Temporal and "legacy" Dates, but I can see a lot of scenarios where being able to translate back and forth would be valuable.

A couple examples:

Both of these seem solvable in userspace, but I anticipate that there are some tricky edge cases that I, a normal software developer, might not handle correctly. Are there any plans to provide convenient methods to translate Date↔Temporal?

littledan commented 4 years ago

I think the recommended idiom to convert from Date to Temporal.DateTime is basically Temporal.DateTime.from({miliseconds: +date}). You can convert in the other direction with new Date(temporalDateTime.getEpochMiliseconds()).

I don't think we should provide transparent interoperability since the logical domains are just different--Temporal is strict on purpose, to avoid logic errors. I wouldn't be opposed to adding conversion functions, but I'm also not sure they're necessary.

EDIT: One of the above conversion functions was incorrect; see https://github.com/tc39/proposal-temporal/issues/299#issuecomment-565714020

alanhussey commented 4 years ago

I don't think we should provide transparent interoperability […] I wouldn't be opposed to adding conversion functions, but I'm also not sure they're necessary.

Agreed, on both counts. I think your examples are relatively clear and terse (although it wasn't my first thought to use milliseconds-since-the-epoch as the interchange value). If Temporal were a library, I'd suggest that those examples be documented somewhere.

pipobscure commented 4 years ago

What @littledan recommended is actually exactly wrong and would thrown an error.

Temporal.DateTime.from({milliseconds}) will throw for milliseconds > 999

That’s what Temporal.Absolute is for!

Temporal.Absolute.fromEpochMilliseconds(dateObj) will translate a Date object to the Temporal world.

new Date(absolute.getEpochMilliseconds()) goes the other way.

hax commented 4 years ago

To be honest, Temporal.Absolute.fromEpochMilliseconds() is a little bit lengthy and not easy to remember, could we have a better name?

pipobscure commented 4 years ago

@hax please feel free to make a suggestion. Criteria are:

  1. It has to be clear as to what it does
  2. It has to be consistent with similar methods (fromEpochSeconds, fromEpochMilliseconds, ...)
ryzokuken commented 4 years ago

@hax do you have any alternatives in mind? The method itself is just named fromEpochMilliseconds which seems rather fair, given that it constructs an Absolute object from epoch ms.

hax commented 4 years ago

I don't have concrete name alternatives for fromEpochMilliseconds now, but maybe we can make Temporal.Absolute.from(legacyDate: Date) work for this issue?

There is also another small issue about name (maybe need a separate issue), the word Epoch should be UnixEpoch for accuracy, because there are many different epochs.

jasonwilliams commented 4 years ago

To be honest, Temporal.Absolute.fromEpochMilliseconds() is a little bit lengthy and not easy to remember, could we have a better name?

I disagree, the method name is very clear on what it does and what it expects, it doesn’t seem too long to me. If I saw “from” I would need to look up (mdn?) what that accepts which outweighs the cost of a longer name.

@alanhussey please see my interop examples for working with JS Dates https://github.com/tc39/proposal-temporal/pull/400

ptomato commented 4 years ago

We have cookbook examples showing how to do this now, so I think this can be closed.