moment / moment-timezone

Timezone support for moment.js
momentjs.com/timezone
MIT License
3.82k stars 835 forks source link

Is there an alternative to moment-timezone with bundled IANA TZ DB? #1103

Closed driver-by closed 1 month ago

driver-by commented 4 months ago

All alternatives rely on Intl browser API, but browsers don't update TZ DB reliably. If it's important to have correct times in different timezones in your application, the only way is moment + moment-timezone now. And these projects being in maintenance mode makes me sad 😢 Anything I miss?

I see that js-joda provides some IANA DB (taken from moment-timezone?), but the package is not updated often. I could contribute with tz updates myself there, but it also depends on maintainers' possibility to apply PRs.

Just one example of the Chromium being really behind with these updates: January 23, 2024 - news about Kazakhstan being moved to single timezone starting March 1, 2024 February 01, 2024 - IANA DB updated to 2024a including this timezone change February 03, 2024 - moment-timezone updated to 2024a version February 29, 2024 - the issue in Chromium about lack of Kazakhstan TZ change March 8, 2024 - marked as fixed in Chromium tracker And then it takes some time to get updates on the client side.

Despite it's being a pretty quick fix, the update was pretty late. Moreover, it looks like the process is not automated, and is done manually after an issue is being raised.

gilmoreorless commented 3 months ago

I'm not sure about other date/time libraries that still bundle their own tzdb data. However, if you want full control over the dataset, you could try a hybrid approach: Use one of the many libraries that rely on Intl, but add a custom polyfill such as https://formatjs.io/docs/polyfills/intl-datetimeformat#adding-iana-timezone-database (Note: I haven't tried this approach myself, so I can't verify its effectiveness.)

Schnark commented 2 months ago

I didn't try to use it, but there is also https://github.com/spiritit/timezonecomplete + https://github.com/rogierschouten/tzdata-generate, and given that the release with 2024a was published on Feb 1, this library seems to be very well managed.

driver-by commented 1 month ago

@gilmoreorless , @Schnark thank you for your advices. I choose the polyfill and looks wonderful. Basically, I can use any library I want and add the TZ bundle on top

For somebody who can use this approach, just install npm i @formatjs/intl-datetimeformat and in your code:

import '@formatjs/intl-datetimeformat/polyfill-force';
import '@formatjs/intl-datetimeformat/locale-data/en';
import '@formatjs/intl-datetimeformat/add-all-tz';

With polyfill-force you force overriding browser's TZ data, even if they have that.