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

Develop a shim for the Temporal proposal, exposing a Moment-like interface #104

Closed
littledan opened this issue Dec 22, 2018 · 26 comments
Closed

Comments

@littledan
Copy link
Member

This proposal subsumes most of the functionality of Moment.js, but there's a ton of code out there using moment, and it will take a long time to upgrade to the Temporal API.

To make the transition easier, while reducing download size, it'd be great to have an implementation of most of the moment API in terms of Temporal, if this is possible. I think it's not to early now to start on this work, to validate the design of this proposal, and to have something ready as soon as Temporal ships.

@jhpratt
Copy link

jhpratt commented Dec 22, 2018

Would it be feasible to fork moment.js and incrementally replace methods? I've never taken a look at the source, but that would certainly guarantee compatibility.

@developit
Copy link

@jhpratt I think so. It would likely achieve faster widespread adoption compared to recommending end-developers modify their code.

@littledan littledan changed the title Develop a shim for the Temporal propsal, exposing a Moment-like interface Develop a shim for the Temporal proposal, exposing a Moment-like interface Dec 22, 2018
@maggiepint
Copy link
Member

This isn't impossible - biggest thing is that Moment is mutable and this API is immutable - so you like end up having to make something immutable mutable which I think would result in some pretty awkward code.

@littledan
Copy link
Member Author

I guess you would have a mutable interface which internally swaps out different Temporal instances when it needs to mutste itself?

@kaizhu256
Copy link
Contributor

i still think an interface using static-functions to input/output canonical ISOStrings is easier to spec and implement, and more inline with actual javascript use-cases.

most data (including dates/temporals) are primarily meant to be baton-passed in javascript to external, non-js-systems with [occasional] js-business-logic applied while in-transit. using string-based static-functions would save us the hassle of being coerced into error-prone revival/reserialization of dates at every transit-point in a workflow-process, just to do date-arithmetic.

@ljharb
Copy link
Member

ljharb commented Jan 5, 2019

That wouldn’t meet airbnb’s use cases, which require passing around a non-string validateable instance that contains all the disparate parts required to format it properly in multiple formats, where the formatting site is very disconnected from the creation site. Your suggestion would not be in line with our actual JavaScript UX workflow use cases.

@kaizhu256
Copy link
Contributor

wouldn't it be easier to store the ISOString and disparate-parts as easy-to-serialize (and json-schema-validatable) JSON-object rather than class-instance?

{
   "isostring": "2019-01-05T20:06:58.786Z",
   "timezoneOffset": "-03:00",
   "presentationFormat": "{{dayOfWeek}} {{month}} {{dd}} {{yyyy}} {{HH}}:{{MM}}:{{MM}} GMT-{{timezoneOffset}}",
   "locale": "Brazil"
}

@ljharb
Copy link
Member

ljharb commented Jan 5, 2019

No, not at all. A string, as well as a plain object, is not trivial to validate and ensure the user’s passing the correct thing (as opposed to something that kind of looks like the correct thing). It’s the some reason it’s common to use Array.isArray instead of checking the length and the numeric indexes.

@kaizhu256
Copy link
Contributor

its a tradeoff, which in my experience favors json-objects over class-instances in javascript-workflows.

a primary benefit of static-functions and json-objects is avoiding the operational-painpoint of maintaining up-to-date class-dependencies at every transit-point in the workflow-process.

@ljharb
Copy link
Member

ljharb commented Jan 5, 2019

You have to do that regardless - whether it’s explicit or not, you have a coupled dependency on a contract/interface/format/serialization/whatever.

@kaizhu256
Copy link
Contributor

you don't need any of those things with json-objects. just use baked-in JSON.parse/JSON.stringify, and design the workflow so that internal validation-checking isn't necessary/common.

contract/interface/format/etc are only needed at external-endpoints in the workflow-process (database, rest-api for external-clients, dom-inputs, etc). its easier to write uncoupled, custom/throwaway static-functions at each external-endpoint to do those jobs, than some coupled, monolithic beast to handle them all.

@ljharb
Copy link
Member

ljharb commented Jan 5, 2019

Yes, you do still need those things, because "JSON" only dictates the serialization format - not the structure of the resulting data, which is a dependency, whether it's implicit or explicit. In my experience, it's easier to write those things, but the ease of writing doesn't matter - because it's harder to read, understand, debug, and maintain those things.

Regardless, you certainly don't have to write your code following my experience, but it's not reasonable or friendly to attempt to erase the validity of these patterns for others. All use cases are valid.

@pipobscure
Copy link
Collaborator

Once the proposal is stable (which I think it almost is), and the polyfill is complete and up to date (which will happen in real-time), I'll give this a go. Maybe first just keep moment objects as they are and make it use the temporal stuff for it's own storage/handling. That way the moment object remain mutable, but use immutable underlying objects.

@ryzokuken ryzokuken added the non-prod-polyfill THIS POLYFILL IS NOT FOR PRODUCTION USE! label Jul 12, 2019
@ryzokuken ryzokuken added this to the Stage 3 milestone Jul 12, 2019
@jorroll
Copy link

jorroll commented Oct 1, 2019

When it comes time to create this, it might make sense to simply create a shim that mimics the moment-timezone API. I think the moment-timezone API applies non-breaking additions to the moment API, so the moment-timezone API would be useful for both user groups.

@ryzokuken
Copy link
Member

Closing this since the polyfill has been successfully merged into the repository. Please feel free to reopen.

@ryzokuken
Copy link
Member

Now that I think about it, this issue was about a moment-like shim and not the polyfill itself. Reopening. @pipobscure is there any ongoing work on this?

@ryzokuken ryzokuken reopened this Oct 11, 2019
@pipobscure
Copy link
Collaborator

Yes I’ve go a colleague of mine to take it up.

@jasonwilliams
Copy link
Member

@pipobscure do we know whats happening with this?

@pipobscure
Copy link
Collaborator

I was hoping that this would happen, but have not seen movement yet. I’ll follow up, but it’s a significant piece of work.

@littledan
Copy link
Member Author

This might be better to work on once the main polyfill and docs are all done.

@ptomato
Copy link
Collaborator

ptomato commented May 13, 2020

From #557, consider also date-fns and luxon

@littledan littledan removed this from the Stage 3 milestone May 14, 2020
@littledan
Copy link
Member Author

I'm removing the "Stage 3" milestone and "Finalize polyfill" project, as I think this whole issue is a nice-to-have. From a practical perspective, an "updating from Moment" or "updating from Date and date-fns" practical guide may be more useful, but also isn't a Stage 3 or 4 requirement.

@ryzokuken ryzokuken added nice to have and removed non-prod-polyfill THIS POLYFILL IS NOT FOR PRODUCTION USE! labels May 20, 2020
@ptomato ptomato modified the milestone: Stage 4 May 20, 2020
@ptomato ptomato mentioned this issue Jun 26, 2020
7 tasks
@ptomato
Copy link
Collaborator

ptomato commented Jul 17, 2020

I have been taking a look intermittently at porting date-fns to use Temporal internally. Since date-fns operates on legacy Date there are two ways to do it: keep the API backwards compatible or not. Keeping the API backwards compatible means that date-fns can accept Temporal types but only return legacy Date. It's useful for shaking out bugs in the Temporal polyfill by running the ported code against the date-fns test suite, but it's got the disadvantage that you have to convert everything back into legacy Date in every function, meaning that you lose many of the benefits of Temporal because, say if you do addBusinessDays(someTemporalDate, 8) then you still have to pick an arbitrary time of day to convert the Temporal.Date result back to legacy Date. So it wouldn't really be useful for porting programs into the Temporal future.

@ptomato
Copy link
Collaborator

ptomato commented Jul 17, 2020

This is all I plan to do for date-fns at the moment, but I think it illustrates how such a port would proceed: https://github.com/ptomato/temporal-fns/commits/port-to-temporal
I may return to a moment port later on.

@mikemccaughan
Copy link

Considering Moment's website has the prominent note

Considering using Moment in your project?
There may be better modern alternatives.
For more details and recommendations,
please see Project Status in the docs.

And the Project Status page goes on to say,

We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.

They say they will not be adding any new features... I would think porting the library to use Temporal rather than Date to be a new feature.

Then again, this shim seems to be for spec-testing purposes, not as a PR to moment, so feel free to ignore me.

Perhaps the time would be better spent on its modular cousin, Luxon, although signs seem to indicate the primary maintainer of that library may give it up once Temporal is in browsers...

@ptomato
Copy link
Collaborator

ptomato commented Jan 2, 2023

Moved to the developer-education task list: js-temporal/temporal-developer-education#1

@ptomato ptomato closed this as completed Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests