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

Request: js_of_ocaml example #42

Open
glennsl opened this issue Feb 26, 2022 · 5 comments
Open

Request: js_of_ocaml example #42

glennsl opened this issue Feb 26, 2022 · 5 comments

Comments

@glennsl
Copy link
Contributor

glennsl commented Feb 26, 2022

It seems this has been made with js_of_ocaml, and platform independence more generally, in mind. But it's not at all clear to me how you would set up a js_of_ocaml project to actually use it. It certainly isn't sufficient to just install and use timedesc, as that will try to read files from the file system.

I can see there are various backends that can be used, but none that target js_of_ocaml specifically. There's also the export-js-tzdb-* executables which do target js_of_ocaml, but I don't see how to connect these with anything else. My guess based on this is that it might require using one of these executables to generate a js bundle of the timezone db, include that in your own code somehow, then create a custom backend to utilize it?

In short, would it be possible to provide an example or a more detailed description of how this could be accomplished?

@darrenldl
Copy link
Contributor

Ah indeed I've largely forgotten about the js side of usage and it remains unpolished. One outdated example is at https://github.com/daypack-dev/timere-parse-demo

But indeed there remains a need for a proper guide.

@glennsl
Copy link
Contributor Author

glennsl commented Feb 26, 2022

Thanks! So it seems the problem is not so much with the timezone database, but that there's no tzlocal backend for js. I don't think that should be too hard to implement though, so I'll see about doing that.

Also, a bit as an aside, I don't really need a tzdb. All I need, and I think this applies to most users, is the local timezone offset. I'll only ever deal with local time for interacting with the end-user, and UTC time for interacting with the server. Is it possible to only create a single fixed-offset time zone, get the current time for that, and deal only with that and UTC?

@darrenldl
Copy link
Contributor

darrenldl commented Feb 26, 2022

Not exactly advisable, but you can do so via picking tzlocal.none or tzlocal.utc backend, and supply your offset based time zone to all relevant calls via Timedesc.Time_zone.make_offset_only[_exn].

Main issue is as soon as any extrapolation into future can cause error when you cross DST boundary.

We experimented with dynamic lookup at here https://github.com/daypack-dev/doodlinator/blob/main/bookmarklet/crawler.ml#L24 which retrieves the relevant tzdb-json file after user specification of time zone selection (granted the tzdb json files should be hosted in some CDN for production).
This is likely the better approach with lower space requirement and no compromise on correctness.

@glennsl
Copy link
Contributor Author

glennsl commented Feb 26, 2022

True, that probably is a better approach. I'll look into that if it starts becoming a problem. Thanks for the guidance!

@glennsl
Copy link
Contributor Author

glennsl commented Feb 26, 2022

It's actually possible to query the JS environment's own timezone database by abusing the Intl.DateTimeFormat API a little bit. See for example https://github.com/moment/luxon/blob/master/src/zones/IANAZone.js

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

No branches or pull requests

2 participants