refactor: pass hourCycle inline and assert literal expected times

This commit is contained in:
Benjamin Lu
2026-07-09 21:56:56 -07:00
parent 85d1e5821f
commit a9bbcb33e4
2 changed files with 11 additions and 20 deletions

View File

@@ -212,19 +212,13 @@ describe('formatShortMonthDay', () => {
describe('formatClockTime', () => {
it('formats time with hours, minutes, and seconds', () => {
const ts = new Date(2024, 5, 15, 14, 5, 6).getTime()
const hourCycle = new Intl.DateTimeFormat(undefined, {
const { hourCycle } = new Intl.DateTimeFormat(undefined, {
hour: 'numeric'
}).resolvedOptions().hourCycle
const options = {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
hourCycle
} satisfies Intl.DateTimeFormatOptions
const expected = new Intl.DateTimeFormat('en-GB', options).format(ts)
const result = formatClockTime(ts, 'en-GB')
}).resolvedOptions()
const expected =
hourCycle === 'h11' || hourCycle === 'h12' ? '2:05:06 pm' : '14:05:06'
expect(result).toBe(expected)
expect(formatClockTime(ts, 'en-GB')).toBe(expected)
})
it('uses app locale with explicit 12-hour preference', () => {

View File

@@ -98,16 +98,13 @@ export const formatClockTime = (
clockPreferenceLocale?: string
): string => {
const d = new Date(ts)
const hourCycle = new Intl.DateTimeFormat(clockPreferenceLocale, {
const { hourCycle } = new Intl.DateTimeFormat(clockPreferenceLocale, {
hour: 'numeric'
}).resolvedOptions().hourCycle
const options: Intl.DateTimeFormatOptions = {
}).resolvedOptions()
return new Intl.DateTimeFormat(locale, {
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
}
if (hourCycle !== undefined) {
options.hourCycle = hourCycle
}
return new Intl.DateTimeFormat(locale, options).format(d)
second: '2-digit',
hourCycle
}).format(d)
}