Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Zulu support to customParseFormat #1359

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
})