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