From a9bbcb33e4bc7cc5f3572cb2d6b0b28d1ece9c8a Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Thu, 9 Jul 2026 21:56:56 -0700 Subject: [PATCH] refactor: pass hourCycle inline and assert literal expected times --- src/utils/dateTimeUtil.test.ts | 16 +++++----------- src/utils/dateTimeUtil.ts | 15 ++++++--------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/utils/dateTimeUtil.test.ts b/src/utils/dateTimeUtil.test.ts index d6d2d07cbe..93f9ee5f56 100644 --- a/src/utils/dateTimeUtil.test.ts +++ b/src/utils/dateTimeUtil.test.ts @@ -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', () => { diff --git a/src/utils/dateTimeUtil.ts b/src/utils/dateTimeUtil.ts index f86444d270..0b76375b2a 100644 --- a/src/utils/dateTimeUtil.ts +++ b/src/utils/dateTimeUtil.ts @@ -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) }