moment / moment-timezone

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

(docs) please make it easier to find list of timezone strings or add TS support #985

Closed iway1 closed 2 years ago

iway1 commented 2 years ago

Version: any

Issue description:

not even sure where I can find list of available timezone strings, the .tz function should accept a more strongly typed parameter instead of just string to prevent developer error and provide autocomplete.

gilmoreorless commented 2 years ago

To get a list of all available names for the currently-loaded dataset, you can call moment.tz.names(). This list should mostly match the more detailed version at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones (they both come from the same data source).

However, I don't think the type definitions should use anything more specific than string, for a few reasons:

  1. With the default dataset provided by moment-timezone, there are a lot of possible values. In version 0.5.34 the .names() function returns an array of 594 strings.
  2. The default values can change per release, depending on changes to the underlying IANA tzdb sources.
  3. Users can define and load custom datasets for time zones. This could mean either removing names that are found in the default dataset, or defining new custom names. The first case could pass type checks but fail at runtime, while the second case could throw a type error for a valid input.
ichernev commented 2 years ago

@iway1 just write a script that patches your type-script bindings via moment.tz.names locally and be happy. No need to push this usecase to all users, as @gilmoreorless pointed out, there are good reasons against it.

Also, the idea of all these timezones is to select one at runtime, if you are going to hardcode a string in your code, then you should know which one you want. Also, if it's one or a few you might re-compile the tzdata to only include those zones and save on a lof of bandwidth (if this is client-side). I can't possibly see a use case where a developer is like "umm which timezone should I hardcode, let the IDE help me out".