Skip to content

Commit

Permalink
Merge pull request #166 from mikegreiling/fix-broken-timezone-abbrevi…
Browse files Browse the repository at this point in the history
…ations

Fix broken timezone abbreviations and GMT fallback
  • Loading branch information
chase-manning authored Sep 21, 2021
2 parents ba70f31 + e242626 commit 4d26455
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/dateformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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");
};
47 changes: 47 additions & 0 deletions test/test_mask-z_.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 4d26455

Please sign in to comment.