Skip to content

Commit 7c6bfd7

Browse files
authored
feat(timezones): Add UTC to list of timezones (#163)
fixes #21
1 parent 7dec499 commit 7c6bfd7

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ interface TimeZone extends RawTimeZone {
1919
currentTimeFormat: string;
2020
}
2121

22+
interface TimeZoneOptions {
23+
includeUtc?: boolean;
24+
}
25+
2226
export const rawTimeZones: RawTimeZone[];
2327
export const timeZonesNames: TimeZoneName[];
24-
export function getTimeZones(): TimeZone[];
28+
export function getTimeZones(opts?: TimeZoneOptions): TimeZone[];

lib/getTimeZones.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,40 @@ import rawTimeZones from "../raw-time-zones.json";
44

55
import formatTimeZone from "./formatTimeZone.js";
66

7-
export default function getTimeZones() {
7+
export default function getTimeZones(opts) {
8+
const includeUtc = !!opts && opts.includeUtc;
89
return rawTimeZones
9-
.reduce(function (acc, timeZone) {
10-
const currentDate = DateTime.fromObject({
11-
locale: "en-US",
12-
zone: timeZone.name,
13-
});
14-
15-
// We build on the latest Node.js version, Node.js embed IANA databases
16-
// it might happen that the environment that will execute getTimeZones() will not know about some
17-
// timezones. So we ignore the timezone at runtim
18-
// See https://github.com/vvo/tzdb/issues/43
19-
if (currentDate.isValid === false) {
10+
.reduce(
11+
function (acc, timeZone) {
12+
const currentDate = DateTime.fromObject({
13+
locale: "en-US",
14+
zone: timeZone.name,
15+
});
16+
17+
// We build on the latest Node.js version, Node.js embed IANA databases
18+
// it might happen that the environment that will execute getTimeZones() will not know about some
19+
// timezones. So we ignore the timezone at runtim
20+
// See https://github.com/vvo/tzdb/issues/43
21+
if (currentDate.isValid === false) {
22+
return acc;
23+
}
24+
25+
const timeZoneWithCurrentTimeData = {
26+
...timeZone,
27+
currentTimeOffsetInMinutes: currentDate.offset,
28+
};
29+
30+
acc.push({
31+
...timeZoneWithCurrentTimeData,
32+
currentTimeFormat: formatTimeZone(timeZoneWithCurrentTimeData, {
33+
useCurrentOffset: true,
34+
}),
35+
});
36+
2037
return acc;
21-
}
22-
23-
const timeZoneWithCurrentTimeData = {
24-
...timeZone,
25-
currentTimeOffsetInMinutes: currentDate.offset,
26-
};
27-
28-
acc.push({
29-
...timeZoneWithCurrentTimeData,
30-
currentTimeFormat: formatTimeZone(timeZoneWithCurrentTimeData, {
31-
useCurrentOffset: true,
32-
}),
33-
});
34-
35-
return acc;
36-
}, [])
38+
},
39+
includeUtc ? [utcTimezone] : [],
40+
)
3741
.sort((a, b) => {
3842
return (
3943
compareNumbers(a, b) ||
@@ -53,3 +57,18 @@ function compareStrings(x, y) {
5357
}
5458
return 0;
5559
}
60+
61+
const utcTimezone = {
62+
name: "UTC",
63+
alternativeName: "Coordinated Universal Time (UTC)",
64+
abbreviation: "UTC",
65+
group: ["UTC"],
66+
countryName: "",
67+
continentCode: "",
68+
continentName: "",
69+
mainCities: [""],
70+
rawOffsetInMinutes: 0,
71+
rawFormat: "+00:00 Coordinated Universal Time (UTC)",
72+
currentTimeOffsetInMinutes: 0,
73+
currentTimeFormat: "+00:00 Coordinated Universal Time (UTC)",
74+
};

0 commit comments

Comments
 (0)