Skip to content

Commit

Permalink
feat(timezones): Add UTC to list of timezones (#163)
Browse files Browse the repository at this point in the history
fixes #21
  • Loading branch information
Allcharles authored Apr 9, 2021
1 parent 7dec499 commit 7c6bfd7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface TimeZone extends RawTimeZone {
currentTimeFormat: string;
}

interface TimeZoneOptions {
includeUtc?: boolean;
}

export const rawTimeZones: RawTimeZone[];
export const timeZonesNames: TimeZoneName[];
export function getTimeZones(): TimeZone[];
export function getTimeZones(opts?: TimeZoneOptions): TimeZone[];
75 changes: 47 additions & 28 deletions lib/getTimeZones.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@ import rawTimeZones from "../raw-time-zones.json";

import formatTimeZone from "./formatTimeZone.js";

export default function getTimeZones() {
export default function getTimeZones(opts) {
const includeUtc = !!opts && opts.includeUtc;
return rawTimeZones
.reduce(function (acc, timeZone) {
const currentDate = DateTime.fromObject({
locale: "en-US",
zone: timeZone.name,
});

// 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 (currentDate.isValid === false) {
.reduce(
function (acc, timeZone) {
const currentDate = DateTime.fromObject({
locale: "en-US",
zone: timeZone.name,
});

// 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 (currentDate.isValid === false) {
return acc;
}

const timeZoneWithCurrentTimeData = {
...timeZone,
currentTimeOffsetInMinutes: currentDate.offset,
};

acc.push({
...timeZoneWithCurrentTimeData,
currentTimeFormat: formatTimeZone(timeZoneWithCurrentTimeData, {
useCurrentOffset: true,
}),
});

return acc;
}

const timeZoneWithCurrentTimeData = {
...timeZone,
currentTimeOffsetInMinutes: currentDate.offset,
};

acc.push({
...timeZoneWithCurrentTimeData,
currentTimeFormat: formatTimeZone(timeZoneWithCurrentTimeData, {
useCurrentOffset: true,
}),
});

return acc;
}, [])
},
includeUtc ? [utcTimezone] : [],
)
.sort((a, b) => {
return (
compareNumbers(a, b) ||
Expand All @@ -53,3 +57,18 @@ function compareStrings(x, y) {
}
return 0;
}

const utcTimezone = {
name: "UTC",
alternativeName: "Coordinated Universal Time (UTC)",
abbreviation: "UTC",
group: ["UTC"],
countryName: "",
continentCode: "",
continentName: "",
mainCities: [""],
rawOffsetInMinutes: 0,
rawFormat: "+00:00 Coordinated Universal Time (UTC)",
currentTimeOffsetInMinutes: 0,
currentTimeFormat: "+00:00 Coordinated Universal Time (UTC)",
};

0 comments on commit 7c6bfd7

Please sign in to comment.