Skip to content
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
25 changes: 18 additions & 7 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,25 @@ const getLocalePart = (name) => {
}
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)
// Use locale meridiem function or create a default one
const meridiem = locale.meridiem || ((hour, minute, isLower) => {
const m = hour < 12 ? 'am' : 'pm'
return isLower ? m : m.toUpperCase()
})

// Check PM hours (13-23) first, then AM hours (1-12)
// This ensures correct matching for locales
// meridiem returns the same string for all AM or all PM hours
for (let i = 13; i <= 23; i += 1) {
if (input.indexOf(meridiem(i, 0, isLowerCase)) > -1) {
isAfternoon = true
break
}
}
if (isAfternoon === undefined) {
for (let i = 1; i <= 12; i += 1) {
if (input.indexOf(meridiem(i, 0, isLowerCase)) > -1) {
isAfternoon = i > 12
isAfternoon = false
break
}
}
Expand Down
157 changes: 156 additions & 1 deletion 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/ko'
import '../../src/locale/ja'
import '../../src/locale/ar-dz'
import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
Expand Down Expand Up @@ -334,9 +337,11 @@ describe('Array format support', () => {
})
})

describe('meridiem locale', () => {
describe('meridiem locale - zh-cn', () => {
const format = 'YYYYεΉ΄M月Dζ—₯Ahη‚Ήmmεˆ†ssη§’'
const format2 = 'YYYY-MM-DD HH:mm:ss'

// Original roundtrip tests
it('AM', () => {
const input = '2018-05-02 01:02:03'
const date = dayjs(input).locale('zh-cn').format(format)
Expand All @@ -347,6 +352,156 @@ describe('meridiem locale', () => {
const date = dayjs(input).locale('zh-cn').format(format)
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})

// Test all 6 Chinese meridiem periods
describe('format - all time periods', () => {
it('ε‡Œζ™¨ (0:00-5:59) - early morning', () => {
const input = '2018-05-02 03:30:00'
const result = dayjs(input).locale('zh-cn').format('A h:mm')
expect(result).toBe('ε‡Œζ™¨ 3:30')
})
it('ζ—©δΈŠ (6:00-8:59) - morning', () => {
const input = '2018-05-02 07:30:00'
const result = dayjs(input).locale('zh-cn').format('A h:mm')
expect(result).toBe('ζ—©δΈŠ 7:30')
})
it('上午 (9:00-10:59) - late morning', () => {
const input = '2018-05-02 09:30:00'
const result = dayjs(input).locale('zh-cn').format('A h:mm')
expect(result).toBe('上午 9:30')
})
it('中午 (11:00-12:59) - noon', () => {
const input = '2018-05-02 12:00:00'
const result = dayjs(input).locale('zh-cn').format('A h:mm')
expect(result).toBe('中午 12:00')
})
it('δΈ‹εˆ (13:00-17:59) - afternoon', () => {
const input = '2018-05-02 15:30:00'
const result = dayjs(input).locale('zh-cn').format('A h:mm')
expect(result).toBe('δΈ‹εˆ 3:30')
})
it('ζ™šδΈŠ (18:00-23:59) - evening', () => {
const input = '2018-05-02 20:30:00'
const result = dayjs(input).locale('zh-cn').format('A h:mm')
expect(result).toBe('ζ™šδΈŠ 8:30')
})
})

describe('parse - all time periods', () => {
it('ε‡Œζ™¨ - early morning', () => {
const input = 'ε‡Œζ™¨ 3:30'
const result = dayjs(input, 'A h:mm', 'zh-cn')
expect(result.hour()).toBe(3)
})
it('ζ—©δΈŠ - morning', () => {
const input = 'ζ—©δΈŠ 7:30'
const result = dayjs(input, 'A h:mm', 'zh-cn')
expect(result.hour()).toBe(7)
})
it('上午 - late morning', () => {
const input = '上午 9:30'
const result = dayjs(input, 'A h:mm', 'zh-cn')
expect(result.hour()).toBe(9)
})
it('中午 - noon', () => {
const input = '中午 11:30'
const result = dayjs(input, 'A h:mm', 'zh-cn')
expect(result.hour()).toBe(11)
})
it('δΈ‹εˆ - afternoon', () => {
const input = 'δΈ‹εˆ 3:30'
const result = dayjs(input, 'A h:mm', 'zh-cn')
expect(result.hour()).toBe(15)
})
it('ζ™šδΈŠ - evening', () => {
const input = 'ζ™šδΈŠ 8:30'
const result = dayjs(input, 'A h:mm', 'zh-cn')
expect(result.hour()).toBe(20)
})
})
})

describe('parsing meridiem - ko', () => {
it('AM', () => {
const input = 'μ˜€μ „ 1:30'
const format = 'A h:mm'
const result = dayjs(input, format, 'ko')
expect(result.hour()).toBe(1)
})
it('PM', () => {
const input = 'μ˜€ν›„ 3:45'
const format = 'A h:mm'
const result = dayjs(input, format, 'ko')
expect(result.hour()).toBe(15)
})
it('noon (12 PM)', () => {
const input = 'μ˜€ν›„ 12:00'
const format = 'A h:mm'
const result = dayjs(input, format, 'ko')
expect(result.hour()).toBe(12)
})
it('midnight (12 AM)', () => {
const input = 'μ˜€μ „ 12:00'
const format = 'A h:mm'
const result = dayjs(input, format, 'ko')
expect(result.hour()).toBe(0)
})
})

describe('parsing meridiem - ja', () => {
it('AM', () => {
const input = 'εˆε‰ 1:30'
const format = 'A h:mm'
const result = dayjs(input, format, 'ja')
expect(result.hour()).toBe(1)
})
it('PM', () => {
const input = '午後 3:45'
const format = 'A h:mm'
const result = dayjs(input, format, 'ja')
expect(result.hour()).toBe(15)
})

it('noon (12 PM)', () => {
const input = '午後 12:00'
const format = 'A h:mm'
const result = dayjs(input, format, 'ja')
expect(result.hour()).toBe(12)
})
it('midnight (12 AM)', () => {
const input = 'εˆε‰ 12:00'
const format = 'A h:mm'
const result = dayjs(input, format, 'ja')
expect(result.hour()).toBe(0)
})
})

describe('parsing meridiem - ar-dz', () => {
it('AM', () => {
const input = 'Ψ΅ 1:30'
const format = 'A h:mm'
const result = dayjs(input, format, 'ar-dz')
expect(result.hour()).toBe(1)
})
it('PM', () => {
const input = 'Ω… 3:45'
const format = 'A h:mm'
const result = dayjs(input, format, 'ar-dz')
expect(result.hour()).toBe(15)
})

it('noon (12 PM)', () => {
const input = 'Ω… 12:00'
const format = 'A h:mm'
const result = dayjs(input, format, 'ar-dz')
expect(result.hour()).toBe(12)
})
it('midnight (12 AM)', () => {
const input = 'Ψ΅ 12:00'
const format = 'A h:mm'
const result = dayjs(input, format, 'ar-dz')
expect(result.hour()).toBe(0)
})
})

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