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

fix: escape last period to match only milliseconds (#1239) #1295

Merged
merged 3 commits into from
Jan 3, 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
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
export const INVALID_DATE_STRING = 'Invalid Date'

// regex
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d+)?$/
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?\.?(\d+)?$/
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
30 changes: 30 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,34 @@ describe('REGEX_PARSE', () => {
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2019-03-25T06:41:00.999999999-2019-03-25-06-41-00-999999999')
})
it('20210102T012345', () => {
const date = '20210102T012345'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('20210102T012345-2021-01-02-01-23-45-')
})
it('2021-01-02T01:23', () => {
const date = '2021-01-02T01:23'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2021-01-02T01:23-2021-01-02-01-23--')
})
it('2021-01-02T01:23:45', () => {
const date = '2021-01-02T01:23:45'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2021-01-02T01:23:45-2021-01-02-01-23-45-')
})
it('2021-01-02T01:23:45-0500 (no regex match)', () => {
const date = '2021-01-02T01:23:45-0500'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
it('2021-01-02T01:23:45Z (no regex match)', () => {
const date = '2021-01-02T01:23:45Z'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
})