Skip to content

Commit

Permalink
Add support for Z token (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferk6a authored Feb 10, 2020
1 parent 4d8a468 commit d514f2f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/fecha.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const token = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
const token = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|Z|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
const twoDigitsOptional = "[1-9]\\d?";
const twoDigits = "\\d\\d";
const threeDigits = "\\d{3}";
Expand Down Expand Up @@ -185,6 +185,15 @@ const formatFlags: Record<
(offset > 0 ? "-" : "+") +
pad(Math.floor(Math.abs(offset) / 60) * 100 + (Math.abs(offset) % 60), 4)
);
},
Z(dateObj: Date): string {
const offset = dateObj.getTimezoneOffset();
return (
(offset > 0 ? "-" : "+") +
pad(Math.floor(Math.abs(offset) / 60), 2) +
":" +
pad(Math.abs(offset) % 60, 2)
);
}
};

Expand All @@ -210,6 +219,20 @@ const amPm: ParseInfo = [
return null;
}
];
const timezoneOffset: ParseInfo = [
"timezoneOffset",
"[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z?",
(v: string): number | null => {
const parts = (v + "").match(/([+-]|\d\d)/gi);

if (parts) {
const minutes = +parts[1] * 60 + parseInt(parts[2], 10);
return parts[0] === "+" ? minutes : -minutes;
}

return 0;
}
];
const parseFlags: Record<string, ParseInfo> = {
D: ["day", twoDigitsOptional],
DD: ["day", twoDigits],
Expand Down Expand Up @@ -245,20 +268,8 @@ const parseFlags: Record<string, ParseInfo> = {
MMMM: ["month", word, monthUpdate("monthNames")],
a: amPm,
A: amPm,
ZZ: [
"timezoneOffset",
"[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z?",
(v: string): number | null => {
const parts = (v + "").match(/([+-]|\d\d)/gi);

if (parts) {
const minutes = +parts[1] * 60 + parseInt(parts[2], 10);
return parts[0] === "+" ? minutes : -minutes;
}

return 0;
}
]
ZZ: timezoneOffset,
Z: timezoneOffset
};

// Some common format strings
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ testParse(
"HH:mm:ss [GMT]ZZ [(EST)]",
new Date(Date.UTC(year, 0, 1, 14, 20, 31))
);
testParse(
"timezone offset with colon",
"09:20:31 GMT-05:00 (EST)",
"HH:mm:ss [GMT]ZZ [(EST)]",
new Date(Date.UTC(year, 0, 1, 14, 20, 31))
);
testParse(
"timezone offset with Z",
"09:20:31 GMT-05:00 (EST)",
"HH:mm:ss [GMT]Z [(EST)]",
new Date(Date.UTC(year, 0, 1, 14, 20, 31))
);
testParse(
"timezone offset positive",
"09:20:31 GMT+0611",
Expand All @@ -233,6 +245,12 @@ testParse(
"HH:mm:ssZZ",
new Date(Date.UTC(year, 0, 1, 9, 20, 31))
);
testParse(
"UTC timezone offset without explicit offset with Z",
"09:20:31Z",
"HH:mm:ssZ",
new Date(Date.UTC(year, 0, 1, 9, 20, 31))
);
testParse(
"UTC timezone offset without GMT",
"09:20:31 -0000 (UTC)",
Expand Down Expand Up @@ -441,6 +459,9 @@ testFormat("rd", new Date(2001, 2, 23), "Do", "23rd");
test("timezone offset", function() {
assert(fecha.format(new Date(2001, 2, 11), "ZZ").match(/^[\+\-]\d{4}$/));
});
test("timezone offset with colon", function() {
assert(fecha.format(new Date(2001, 2, 11), "Z").match(/^[\+\-]\d{2}:\d{2}$/));
});

// Random groupings
testFormat(
Expand Down

0 comments on commit d514f2f

Please sign in to comment.