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

Fixes issue #2442 Time validation Fix for other languages #2443

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/locale/zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const locale = {
y: '1 年',
yy: '%d 年'
},
meridiemHour: (hour, meridiem) => {
if (hour === 12) {
hour = 0
}
if (meridiem.indexOf('中午') > -1) {
return hour >= 11 ? hour : hour + 12
} else if (meridiem.indexOf('下午') > -1 || meridiem.indexOf('晚上') > -1) {
return hour + 12
}
return hour
},
meridiem: (hour, minute) => {
const hm = (hour * 100) + minute
if (hm < 600) {
Expand Down
11 changes: 11 additions & 0 deletions src/locale/zh-tw.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
y: '1 年',
yy: '%d 年'
},
meridiemHour: (hour, meridiem) => {

Check warning on line 46 in src/locale/zh-tw.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh-tw.js#L46

Added line #L46 was not covered by tests
if (hour === 12) {
hour = 0

Check warning on line 48 in src/locale/zh-tw.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh-tw.js#L48

Added line #L48 was not covered by tests
}
if (meridiem.indexOf('中午') > -1) {
return hour >= 11 ? hour : hour + 12
} else if (meridiem.indexOf('下午') > -1 || meridiem.indexOf('晚上') > -1) {
return hour + 12

Check warning on line 53 in src/locale/zh-tw.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh-tw.js#L53

Added line #L53 was not covered by tests
}
return hour

Check warning on line 55 in src/locale/zh-tw.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh-tw.js#L55

Added line #L55 was not covered by tests
},
meridiem: (hour, minute) => {
const hm = (hour * 100) + minute
if (hm < 600) {
Expand Down
11 changes: 11 additions & 0 deletions src/locale/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
y: '1 年',
yy: '%d 年'
},
meridiemHour: (hour, meridiem) => {

Check warning on line 48 in src/locale/zh.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh.js#L48

Added line #L48 was not covered by tests
if (hour === 12) {
hour = 0

Check warning on line 50 in src/locale/zh.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh.js#L50

Added line #L50 was not covered by tests
}
if (meridiem.indexOf('中午') > -1) {
return hour >= 11 ? hour : hour + 12
} else if (meridiem.indexOf('下午') > -1 || meridiem.indexOf('晚上') > -1) {
return hour + 12

Check warning on line 55 in src/locale/zh.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh.js#L55

Added line #L55 was not covered by tests
}
return hour

Check warning on line 57 in src/locale/zh.js

View check run for this annotation

Codecov / codecov/patch

src/locale/zh.js#L57

Added line #L57 was not covered by tests
},
meridiem: (hour, minute) => {
const hm = (hour * 100) + minute
if (hm < 600) {
Expand Down
12 changes: 7 additions & 5 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ const getLocalePart = (name) => {
part.indexOf ? part : part.s.concat(part.f)
)
}
const meridiemMatch = (input, isLowerCase) => {
const meridiemMatch = (input, isLowerCase, hour = 0) => {
let isAfternoon
const { meridiem } = locale
const { meridiemHour, meridiem } = locale
if (!meridiem) {
isAfternoon = input === (isLowerCase ? 'pm' : 'PM')
} else if (meridiemHour) {
isAfternoon = meridiemHour(hour, input) >= 12
} 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
isAfternoon = i >= 12
break
}
}
Expand All @@ -61,10 +63,10 @@ const meridiemMatch = (input, isLowerCase) => {
}
const expressions = {
A: [matchWord, function (input) {
this.afternoon = meridiemMatch(input, false)
this.afternoon = meridiemMatch(input, false, this.hours)
}],
a: [matchWord, function (input) {
this.afternoon = meridiemMatch(input, true)
this.afternoon = meridiemMatch(input, true, this.hours)
}],
S: [match1, function (input) {
this.milliseconds = +input * 100
Expand Down
62 changes: 62 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import dayjs from '../../src'
import '../../src/locale/ru'
import uk from '../../src/locale/uk'
import '../../src/locale/zh-cn'
import '../../src/locale/zh-tw'
import '../../src/locale/zh'
import '../../src/locale/ja'
import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
Expand Down Expand Up @@ -335,6 +338,7 @@ describe('Array format support', () => {
describe('meridiem locale', () => {
const format = 'YYYY年M月D日Ah点mm分ss秒'
const format2 = 'YYYY-MM-DD HH:mm:ss'
const timeFormat = 'h:mm A'
it('AM', () => {
const input = '2018-05-02 01:02:03'
const date = dayjs(input).locale('zh-cn').format(format)
Expand All @@ -345,6 +349,64 @@ describe('meridiem locale', () => {
const date = dayjs(input).locale('zh-cn').format(format)
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})
it('parse pm and am correctly for zh-cn', () => {
const lang = 'zh-cn'
let date = dayjs('11:00 中午', timeFormat, lang).format(timeFormat)
expect(date).toBe('11:00 中午')
date = dayjs('12:00 中午', timeFormat, lang).format(timeFormat)
expect(date).toBe('12:00 中午')
date = dayjs('5:00 凌晨', timeFormat, lang).format(timeFormat)
expect(date).toBe('5:00 凌晨')
date = dayjs('6:00 早上', timeFormat, lang).format(timeFormat)
expect(date).toBe('6:00 早上')
date = dayjs('5:00 下午', timeFormat, lang).format(timeFormat)
expect(date).toBe('5:00 下午')
date = dayjs('11:00 晚上', timeFormat, lang).format(timeFormat)
expect(date).toBe('11:00 晚上')
date = dayjs('10:00 上午', timeFormat, lang).format(timeFormat)
expect(date).toBe('10:00 上午')
})
it('parse pm and am correctly for zh', () => {
const lang = 'zh-cn'
let date = dayjs('11:00 中午', timeFormat, lang).format(timeFormat)
expect(date).toBe('11:00 中午')
date = dayjs('12:00 中午', timeFormat, lang).format(timeFormat)
expect(date).toBe('12:00 中午')
date = dayjs('5:00 凌晨', timeFormat, lang).format(timeFormat)
expect(date).toBe('5:00 凌晨')
date = dayjs('6:00 早上', timeFormat, lang).format(timeFormat)
expect(date).toBe('6:00 早上')
date = dayjs('5:00 下午', timeFormat, lang).format(timeFormat)
expect(date).toBe('5:00 下午')
date = dayjs('11:00 晚上', timeFormat, lang).format(timeFormat)
expect(date).toBe('11:00 晚上')
date = dayjs('10:00 上午', timeFormat, lang).format(timeFormat)
expect(date).toBe('10:00 上午')
})
it('parse pm and am correctly for zh-tw', () => {
const lang = 'zh-cn'
let date = dayjs('11:00 中午', timeFormat, lang).format(timeFormat)
expect(date).toBe('11:00 中午')
date = dayjs('12:00 中午', timeFormat, lang).format(timeFormat)
expect(date).toBe('12:00 中午')
date = dayjs('5:00 凌晨', timeFormat, lang).format(timeFormat)
expect(date).toBe('5:00 凌晨')
date = dayjs('6:00 早上', timeFormat, lang).format(timeFormat)
expect(date).toBe('6:00 早上')
date = dayjs('5:00 下午', timeFormat, lang).format(timeFormat)
expect(date).toBe('5:00 下午')
date = dayjs('11:00 晚上', timeFormat, lang).format(timeFormat)
expect(date).toBe('11:00 晚上')
date = dayjs('10:00 上午', timeFormat, lang).format(timeFormat)
expect(date).toBe('10:00 上午')
})
it('parse pm and am correctly for japanese', () => {
const lang = 'ja'
let date = dayjs('9:00 午後', timeFormat, lang).format(timeFormat)
expect(date).toBe('9:00 午後')
date = dayjs('9:00 午前', timeFormat, lang).format(timeFormat)
expect(date).toBe('9:00 午前')
})
})

it('parse a string for MMM month format with underscore delimiter', () => {
Expand Down