Skip to content

Commit

Permalink
fix: fix customParseFormat plugin parse meridiem bug (#1169)
Browse files Browse the repository at this point in the history
fix #1168
  • Loading branch information
allmoviestvshowslistsfilmography28 authored and iamkun committed Oct 30, 2020
1 parent e432118 commit b054ee0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
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)
})
})

0 comments on commit b054ee0

Please sign in to comment.