Skip to content

Commit

Permalink
fix: update customParseFormat plugin to parse 2-digit offset (#1209)
Browse files Browse the repository at this point in the history
fix #1205
  • Loading branch information
iamkun authored Nov 12, 2020
1 parent 9544ed2 commit b56936a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ 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
const matchOffset = /[+-]\d\d:?(\d\d)?/ // +00:00 -00:00 +0000 or -0000 +00
const matchWord = /\d*[^\s\d-:/()]+/ // Word

let locale

function offsetFromString(string) {
if (!string) return 0
const parts = string.match(/([+-]|\d\d)/g)
const minutes = +(parts[1] * 60) + +parts[2]
const minutes = +(parts[1] * 60) + (+parts[2] || 0)
return minutes === 0 ? 0 : parts[0] === '+' ? -minutes : minutes // eslint-disable-line no-nested-ternary
}

Expand Down
16 changes: 16 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ it('timezone with no hour', () => {
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})

describe('Timezone Offset', () => {
it('timezone with 2-digit offset', () => {
const input = '2020-12-01T20:00:00+09'
const format = 'YYYY-MM-DD[T]HH:mm:ssZZ'
const result = dayjs(input, format)
expect(result.valueOf()).toBe(moment(input, format).valueOf())
expect(result.valueOf()).toBe(1606820400000)
})
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'
const result = dayjs(input, format)
expect(result.valueOf()).toBe(moment(input, format).valueOf())
})
})

it('parse hh:mm', () => {
const input = '12:00'
const format = 'hh:mm'
Expand Down

0 comments on commit b56936a

Please sign in to comment.