Skip to content

Commit

Permalink
fix: Added Zulu support to customParseFormat (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
beckyconning authored Jan 27, 2021
1 parent 855b7b3 commit 1138a3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ const match3 = /\d{3}/ // 000 - 999
const match4 = /\d{4}/ // 0000 - 9999
const match1to2 = /\d\d?/ // 0 - 99
const matchSigned = /[+-]?\d+/ // -inf - inf
const matchOffset = /[+-]\d\d:?(\d\d)?/ // +00:00 -00:00 +0000 or -0000 +00
const matchOffset = /[+-]\d\d:?(\d\d)?|Z/ // +00:00 -00:00 +0000 or -0000 +00 or Z
const matchWord = /\d*[^\s\d-_:/()]+/ // Word

let locale = {}

function offsetFromString(string) {
if (!string) return 0
if (string === 'Z') return 0
const parts = string.match(/([+-]|\d\d)/g)
const minutes = +(parts[1] * 60) + (+parts[2] || 0)
return minutes === 0 ? 0 : parts[0] === '+' ? -minutes : minutes // eslint-disable-line no-nested-ternary
Expand Down
8 changes: 8 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ describe('Timezone Offset', () => {
expect(result.valueOf()).toBe(moment(input, format).valueOf())
expect(result.valueOf()).toBe(1606820400000)
})
it('zulu', () => {
const input = '2021-01-26T15:38:43.000Z'
const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ'
const result = dayjs(input, format)
expect(result.valueOf()).toBe(moment(input, format).valueOf())
expect(result.valueOf()).toBe(1611675523000)
})
it('no timezone format token should parse in local time', () => {
const input = '2020-12-01T20:00:00+01:00'
const format = 'YYYY-MM-DD[T]HH:mm:ss'
Expand Down Expand Up @@ -347,3 +354,4 @@ it('parse a string for MMM month format with underscore delimiter', () => {
const format2 = 'DD_MMM_YYYY_hhmmss'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})

0 comments on commit 1138a3f

Please sign in to comment.