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: fix customParseFormat plugin parse meridiem bug #1169

Merged
merged 1 commit into from
Oct 27, 2020
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
27 changes: 20 additions & 7 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const match2 = /\d\d/ // 00 - 99
const match3 = /\d{3}/ // 000 - 999
const match4 = /\d{4}/ // 0000 - 9999
const match1to2 = /\d\d?/ // 0 - 99
const matchUpperCaseAMPM = /[AP]M/
const matchLowerCaseAMPM = /[ap]m/
const matchSigned = /[+-]?\d+/ // -inf - inf
const matchOffset = /[+-]\d\d:?\d\d/ // +00:00 -00:00 +0000 or -0000
const matchWord = /\d*[^\s\d-:/()]+/ // Word
Expand Down Expand Up @@ -38,13 +36,28 @@ const getLocalePart = (name) => {
part.indexOf ? part : part.s.concat(part.f)
)
}

const meridiemMatch = (input, isLowerCase) => {
let isAfternoon
const { meridiem } = locale
if (!meridiem) {
isAfternoon = input === (isLowerCase ? 'pm' : 'PM')
} else {
for (let i = 1; i <= 24; i += 1) {
// todo: fix input === meridiem(i, 0, isLowerCase)
if (input.indexOf(meridiem(i, 0, isLowerCase)) > -1) {
isAfternoon = i > 12
break
}
}
}
return isAfternoon
}
const expressions = {
A: [matchUpperCaseAMPM, function (input) {
this.afternoon = input === 'PM'
A: [matchWord, function (input) {
this.afternoon = meridiemMatch(input, false)
}],
a: [matchLowerCaseAMPM, function (input) {
this.afternoon = input === 'pm'
a: [matchWord, function (input) {
this.afternoon = meridiemMatch(input, true)
}],
S: [match1, function (input) {
this.milliseconds = +input * 100
Expand Down
21 changes: 18 additions & 3 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import customParseFormat from '../../src/plugin/customParseFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
import '../../src/locale/ru'
import uk from '../../src/locale/uk'
import '../../src/locale/zh-cn'
import '../../src/locale/ru'
import customParseFormat from '../../src/plugin/customParseFormat'
import localizedFormats from '../../src/plugin/localizedFormat'

dayjs.extend(customParseFormat)
dayjs.extend(localizedFormats)
Expand Down Expand Up @@ -305,3 +305,18 @@ describe('Array format support', () => {
expect(dayjs(input, format, 'zh-cn', true).format('YYYY MMMM DD')).toBe(input)
})
})

describe('meridiem locale', () => {
const format = 'YYYY年M月D日Ah点mm分ss秒'
const format2 = 'YYYY-MM-DD HH:mm:ss'
it('AM', () => {
const input = '2018-05-02 01:02:03'
const date = dayjs(input).locale('zh-cn').format(format)
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})
it('PM', () => {
const input = '2018-05-02 20:02:03'
const date = dayjs(input).locale('zh-cn').format(format)
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})
})