Skip to content

Commit 955ac08

Browse files
committed
Make parser be aware of "Z" character as timezone offset
1 parent 7d6cd6e commit 955ac08

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/parse-format/parse.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const match1to2 = /\d\d?/ // 0 - 99
88
const matchUpperAMPM = /[AP]M/
99
const matchLowerAMPM = /[ap]m/
1010
const matchSigned = /[+-]?\d+/ // -inf - inf
11-
const matchOffset = /[+-]\d\d:?\d\d/ // +00:00 -00:00 +0000 or -0000
11+
const matchOffset = /([+-]\d\d:?\d\d|Z)/ // +00:00 -00:00 +0000, -0000 or Z
1212
const matchAbbreviation = /[A-Z]{3,4}/ // CET
1313

1414
const parseTokenExpressions = {}
@@ -92,6 +92,10 @@ function addParseToken (tokens, property) {
9292
}
9393

9494
function offsetFromString (string) {
95+
if (string === 'Z') {
96+
return 0
97+
}
98+
9599
const parts = string.match(/([+-]|\d\d)/g)
96100
const minutes = +(parts[1] * 60) + +parts[2]
97101
return minutes === 0 ? 0 : parts[0] === '+' ? -minutes : minutes

test/parseZonedTime.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,18 @@ it('recognizes noon', () => {
7272

7373
it('format parser caching code works', () => {
7474
parseZonedTime('2018', 'YYYY')
75-
const time = parseZonedTime('2018', 'YYYY')
7675
expect(typeof time === 'object').toBeTruthy()
7776
const { year } = time
7877
expect(year).toEqual(2018)
7978
})
8079

80+
it('recognizes ISO UTC timezone', () => {
81+
const time = parseZonedTime('Z', 'Z')
82+
const { zone } = time
83+
84+
expect(zone.offset).toEqual(0)
85+
})
86+
8187
it('leaves non-token parts of the format intact', () => {
8288
const time = parseZonedTime(' S:/-.() SS h ', ' [S]:/-.()[ SS h ]')
8389
expect(typeof time === 'object').toBeTruthy()

0 commit comments

Comments
 (0)