From e242626dc94d1dd9c5b19caa3bd6fd88c4b3ca8e Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Mon, 20 Sep 2021 14:55:03 -0600 Subject: [PATCH] Fix broken timezone abbreviations and GMT fallback --- src/dateformat.js | 24 +++++++++++++++++----- test/test_mask-z_.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 test/test_mask-z_.js diff --git a/src/dateformat.js b/src/dateformat.js index 1f7e9e3..8b742f8 100644 --- a/src/dateformat.js +++ b/src/dateformat.js @@ -14,7 +14,7 @@ // Regexes and supporting functions are cached through closure const token = /d{1,4}|D{3,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|W{1,2}|[LlopSZN]|"[^"]*"|'[^']*'/g; -const timezone = /\b((?:[PMCEA][SDP][TC])(?:[-+]\d{4})?|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time)\b/g; +const timezone = /\b(?:[A-Z]{1,3}[A-Z][TC])(?:[-+]\d{4})?|((?:Australian )?(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time)\b/g; const timezoneClip = /[^-+\dA-Z]/g; /** @@ -128,10 +128,7 @@ export default function dateFormat (date, mask, utc, gmt) { ? "GMT" : utc ? "UTC" - : (String(date).match(timezone) || [""]) - .pop() - .replace(timezoneClip, "") - .replace(/GMT\+0000/g, "UTC"), + : formatTimezone(date), o: () => (o() > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o()) / 60) * 100 + (Math.abs(o()) % 60), 4), @@ -310,3 +307,20 @@ const getDayOfWeek = (date) => { } return dow; }; + +/** + * Get proper timezone abbreviation or timezone offset. + * + * This will fall back to `GMT+xxxx` if it does not recognize the + * timezone within the `timezone` RegEx above. Currently only common + * American and Australian timezone abbreviations are supported. + * + * @param {String | Date} date + * @return {String} + */ +export const formatTimezone = (date) => { + return (String(date).match(timezone) || [""]) + .pop() + .replace(timezoneClip, "") + .replace(/GMT\+0000/g, "UTC"); +}; diff --git a/test/test_mask-z_.js b/test/test_mask-z_.js new file mode 100644 index 0000000..ea63700 --- /dev/null +++ b/test/test_mask-z_.js @@ -0,0 +1,47 @@ +import { strictEqual } from "node:assert"; +import { formatTimezone } from "../lib/dateformat.js"; + +describe("Mask: 'Z'", function () { + it("should format 'Tue Sep 08 2020 13:26:11 GMT-0500 (Central Daylight Time)' as 'CDT'", function (done) { + var d = formatTimezone("Tue Sep 08 2020 13:26:11 GMT-0500 (Central Daylight Time)"); + strictEqual(d, "CDT"); + done(); + }); + + it("should format 'Tue Sep 08 2020 12:26:11 GMT-0600 (Mountain Daylight Time)' as 'MDT'", function (done) { + var d = formatTimezone("Tue Sep 08 2020 12:26:11 GMT-0600 (Mountain Daylight Time)"); + strictEqual(d, "MDT"); + done(); + }); + + it("should format 'Wed Sep 09 2020 04:28:21 GMT+1000 (Australian Eastern Standard Time)' as 'AEST'", function (done) { + var d = formatTimezone("Wed Sep 09 2020 04:28:21 GMT+1000 (Australian Eastern Standard Time)"); + strictEqual(d, "AEST"); + done(); + }); + + it("should format 'Wed Sep 09 2020 03:56:05 GMT+0930 (Australian Central Standard Time)' as 'ACST'", function (done) { + var d = formatTimezone("Wed Sep 09 2020 03:56:05 GMT+0930 (Australian Central Standard Time)"); + strictEqual(d, "ACST"); + done(); + }); + + it("should format 'Tue Feb 02 2021 09:51:33 GMT+1030 (Australian Central Daylight Time)' as 'ACDT'", function (done) { + var d = formatTimezone("Tue Feb 02 2021 09:51:33 GMT+1030 (Australian Central Daylight Time)"); + strictEqual(d, "ACDT"); + done(); + }); + + /* Since CEST is not currently supported abbreviation we fall back to GMT+xxxx */ + it("should format 'Tue Feb 02 2021 00:21:33 GMT+0100 (Central European Standard Time)' as 'GMT+0100' (fallback)", function (done) { + var d = formatTimezone("Tue Feb 02 2021 00:21:33 GMT+0100 (Central European Standard Time)"); + strictEqual(d, "GMT+0100"); + done(); + }); + + it("should format 'Tue Sep 08 2020 20:26:22 GMT+0200 (Central European Summer Time)' as 'GMT+0200' (fallback)", function (done) { + var d = formatTimezone("Tue Sep 08 2020 20:26:22 GMT+0200 (Central European Summer Time)"); + strictEqual(d, "GMT+0200"); + done(); + }); +});