UNPKG

2.25 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = getTimeZones;
7
8var _rawTimeZones = _interopRequireDefault(require("../raw-time-zones.json"));
9
10var _formatTimeZone = _interopRequireDefault(require("./formatTimeZone.js"));
11
12var _timeZone = require("./utils/timeZone.js");
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function getTimeZones(opts) {
17 const includeUtc = !!opts && opts.includeUtc;
18 return _rawTimeZones.default.reduce(function (acc, timeZone) {
19 const timeZoneName = timeZone.name;
20 const currentOffset = (0, _timeZone.getZoneOffset)(timeZoneName); // We build on the latest Node.js version, Node.js embed IANA databases
21 // it might happen that the environment that will execute getTimeZones() will not know about some
22 // timezones. So we ignore the timezone at runtim
23 // See https://github.com/vvo/tzdb/issues/43
24
25 if (currentOffset === false) {
26 return acc;
27 }
28
29 const timeZoneWithCurrentTimeData = { ...timeZone,
30 currentTimeOffsetInMinutes: currentOffset
31 };
32 acc.push({ ...timeZoneWithCurrentTimeData,
33 currentTimeFormat: (0, _formatTimeZone.default)(timeZoneWithCurrentTimeData, {
34 useCurrentOffset: true
35 })
36 });
37 return acc;
38 }, includeUtc ? [utcTimezone] : []).sort((a, b) => {
39 return compareNumbers(a, b) || compareStrings(a.alternativeName, b.alternativeName) || compareStrings(a.mainCities[0], b.mainCities[0]);
40 });
41}
42
43function compareNumbers(x, y) {
44 return x.currentTimeOffsetInMinutes - y.currentTimeOffsetInMinutes;
45}
46
47function compareStrings(x, y) {
48 if (typeof x === "string" && typeof y === "string") {
49 return x.localeCompare(y);
50 }
51
52 return 0;
53}
54
55const utcTimezone = {
56 name: "Etc/UTC",
57 alternativeName: "Coordinated Universal Time (UTC)",
58 abbreviation: "UTC",
59 group: ["Etc/UTC", "Etc/UCT", "UCT", "UTC", "Universal", "Zulu"],
60 countryName: "",
61 continentCode: "",
62 continentName: "",
63 mainCities: [""],
64 rawOffsetInMinutes: 0,
65 rawFormat: "+00:00 Coordinated Universal Time (UTC)",
66 currentTimeOffsetInMinutes: 0,
67 currentTimeFormat: "+00:00 Coordinated Universal Time (UTC)"
68};
69//# sourceMappingURL=getTimeZones.js.map
\No newline at end of file