Skip to content

Commit d4b636f

Browse files
committed
Add a timezone guide for country/zone mappings
1 parent 5702dc3 commit d4b636f

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

docs/moment-timezone/01-using-timezones/08-getting-country-zones.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ signature: |
66
---
77

88
To get a list of time zones for some country, use `moment.tz.zonesForCountry()`.
9+
This takes an [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) two-letter country code.
910

1011
```js
1112
moment.tz.zonesForCountry('US');
@@ -17,13 +18,13 @@ By default this method returns zone names sorted alphabetically:
1718
["America/Adak", "America/Anchorage", ... "Pacific/Honolulu"]
1819
```
1920

20-
To get also offsets, pass `true` as 2nd parameter:
21+
To also get offsets, pass `true` as the 2nd parameter:
2122

2223
```js
2324
moment.tz.zonesForCountry('CN', true);
2425
```
2526

26-
it returns array of objects with name and offset:
27+
In this case, it returns an array of objects with the zone's name and its offset for the **current date and time**.
2728

2829
```js
2930
[
@@ -32,6 +33,9 @@ it returns array of objects with name and offset:
3233
]
3334
```
3435

35-
It's useful if you need to sort time zones by offset.
36+
It's useful if you need to sort time zones by offset, but be aware that the offsets can change throughout the year.
3637

37-
All country codes can be retrieved using method `moment.tz.countries()`
38+
All known country codes in the data can be retrieved using method `moment.tz.countries()`.
39+
40+
**Note:** Sometimes these methods might return unexpected data.
41+
See the [Countries and Zones guide](/timezone/guides/#/data-calculations/country-zones/) for more details.

guides/moment-timezone/01-data-calculations/02-limited-range.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Limited Data Ranges
33
---
44

5-
By default, Moment Timezone includes all the data from the [IANA Time Zone Database ("tzdb")](https://www.iana.org/time-zones).
5+
By default, Moment Timezone includes all the data from the IANA tzdb (see [Data Source](#/data-calculations/data-source/)).
66
This covers past data as far back as the 1800s for some zones, and future data up to the year 2499.
77
This is usually more data than required by most applications, so there are also some pre-built data bundles available with more limited year ranges.
88
These are listed on the [project homepage](/timezone) and the ["Where to use it" documentation](/timezone/docs/#/use-it)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Countries and Zones
3+
---
4+
5+
Moment Timezone's [country-related APIs](/timezone/docs/#/using-timezones/getting-country-zones/) rely on known associations between zones and countries, which come directly from the IANA tzdb (see [Data Source](#/data-calculations/data-source/)).
6+
7+
The mapping of zones to countries exposes a subtle nuance in the tzdb structure that doesn't match a lot of people's mental model of the data.
8+
This can sometimes catch out users who see something unexpected in the country data and assume it must be a bug.
9+
10+
The key principle is that zone identifiers like `Europe/Berlin` and `Asia/Tokyo` are **labels** for regions of the world where the clocks have agreed since 1970.
11+
These regions are not specifically tied to a single country, and can span multiple countries if needed.
12+
This would be equally true if the labels didn't have city names at all, but instead were named something like `Europe/Zone073`.
13+
The city name is simply an aid to users to make the database more human-readable.
14+
15+
The tzdb defines which countries use each time zone.
16+
But because the zones are named after cities, this can be easily misinterpreted to mean that a city is in multiple countries, which is incorrect.
17+
18+
As an example, let's look at a line from the source [`zone1970.tab`](https://data.iana.org/time-zones/data/zone1970.tab):
19+
20+
```
21+
CZ,SK +5005+01426 Europe/Prague
22+
```
23+
24+
This line is **not** indicating that the city of Prague is in both Czechia and Slovakia.
25+
It is saying that a time zone region—where clocks have agreed since 1970—can be used in both Czechia and Slovakia, and happens to have the _label_ of `Europe/Prague`.
26+
27+
Moment Timezone also includes the backwards-compatibility data listed in the older (and deprecated) [`zone.tab`](https://data.iana.org/time-zones/data/zone.tab).
28+
This includes the line:
29+
30+
```
31+
SK +4809+01707 Europe/Bratislava
32+
```
33+
34+
The `Europe/Bratislava` zone is an alias of `Europe/Prague`, so there's no difference in the processed time zone data.
35+
36+
All this means that the results from Moment Timezone's country APIs can give some seemingly-unintuitive results that are correct according to the data model:
37+
38+
```js
39+
moment.tz.zonesForCountry('CZ'); // ["Europe/Prague"]
40+
moment.tz.zonesForCountry('SK'); // ["Europe/Bratislava", "Europe/Prague"]
41+
42+
moment.tz.zone('Europe/Bratislava').countries(); // ["SK"]
43+
moment.tz.zone('Europe/Prague').countries(); // ["CZ", "SK"]
44+
```

0 commit comments

Comments
 (0)