diff --git a/src/lib/utilities/format-date.test.ts b/src/lib/utilities/format-date.test.ts index 1a238b9274..46ef49210f 100644 --- a/src/lib/utilities/format-date.test.ts +++ b/src/lib/utilities/format-date.test.ts @@ -15,8 +15,9 @@ const DateTimeFormat = Intl.DateTimeFormat; const dateTimeFormatSpy = vi .spyOn(global.Intl, 'DateTimeFormat') .mockImplementation(function (_, options) { - const hour12 = options?.hour12 !== undefined ? options.hour12 : true; - const hourCycle = options?.hour12 !== undefined ? undefined : 'h11'; + const hour12 = options?.hourCycle ? undefined : (options?.hour12 ?? true); + const hourCycle = + options?.hourCycle ?? (options?.hour12 === undefined ? 'h11' : undefined); return new DateTimeFormat('en-US', { ...options, hour12, @@ -88,6 +89,66 @@ describe('formatDate', () => { ); }); + it('uses the current local timezone after the system timezone changes', () => { + vi.stubEnv('TZ', 'UTC'); + const utc = formatDate(date, 'local', { format: 'long', hourFormat: '24' }); + vi.stubEnv('TZ', 'Europe/Helsinki'); + const helsinki = formatDate(date, 'local', { + format: 'long', + hourFormat: '24', + }); + const explicitUtc = formatDate(date, 'UTC', { + format: 'long', + hourFormat: '24', + }); + vi.unstubAllEnvs(); + + expect(utc).toContain('16:29:35'); + expect(helsinki).toContain('19:29:35'); + expect(explicitUtc).toContain('16:29:35'); + }); + + it.each([ + ['0001-01-01T00:00:00.000Z', 'UTC', '0001-01-01T00:00:00.000Z'], + ['2026-01-15T12:00:00.123Z', 'UTC', '2026-01-15T12:00:00.123Z'], + [ + '2026-01-15T12:00:00.123Z', + 'Europe/Helsinki', + '2026-01-15T14:00:00.123+02:00', + ], + [ + '2026-07-15T12:00:00.123Z', + 'Europe/Helsinki', + '2026-07-15T15:00:00.123+03:00', + ], + [ + '2026-03-29T00:59:59.000Z', + 'Europe/Helsinki', + '2026-03-29T02:59:59.000+02:00', + ], + [ + '2026-10-25T00:59:59.000Z', + 'Europe/Helsinki', + '2026-10-25T03:59:59.000+03:00', + ], + [ + '2026-01-15T23:00:00.123Z', + 'Asia/Kathmandu', + '2026-01-16T04:45:00.123+05:45', + ], + [ + '2026-01-15T03:00:00.123Z', + 'America/New_York', + '2026-01-14T22:00:00.123-05:00', + ], + ])( + 'formats %s in %s as ISO with the correct offset', + (date, zone, expected) => { + expect(formatDate(date, zone, { format: 'iso' })).toBe(expected); + expect(new Date(expected).toISOString()).toBe(date); + }, + ); + it('should format relative local time', () => { expect(formatDate(date, 'local', { relative: true })).toContain('ago'); const currentDate = new Date(); diff --git a/src/lib/utilities/format-date.ts b/src/lib/utilities/format-date.ts index 69110086a4..c6ad44e851 100644 --- a/src/lib/utilities/format-date.ts +++ b/src/lib/utilities/format-date.ts @@ -72,6 +72,34 @@ export const timestampFormats: Record< export type TimestampFormat = keyof typeof timestampFormats; const dateTimeFormatters = new Map(); +const isoFormatters = new Map(); + +const formatIsoDate = (date: Date, timeZone: string): string => { + let formatter = isoFormatters.get(timeZone); + if (!formatter) { + formatter = new Intl.DateTimeFormat('en-US-u-ca-iso8601-nu-latn', { + timeZone, + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + fractionalSecondDigits: 3, + hourCycle: 'h23', + timeZoneName: 'longOffset', + }); + isoFormatters.set(timeZone, formatter); + } + + const parts = Object.fromEntries( + formatter.formatToParts(date).map(({ type, value }) => [type, value]), + ); + const offset = /^(GMT|UTC)([+-]00:00)?$/.test(parts.timeZoneName) + ? 'Z' + : parts.timeZoneName.slice(3); + return `${parts.year.padStart(4, '0')}-${parts.month}-${parts.day}T${parts.hour}:${parts.minute}:${parts.second}.${parts.fractionalSecond}${offset}`; +}; const getHour12Option = (hourFormat: HourFormat): boolean | undefined => { if (hourFormat === 'system') return undefined; @@ -81,9 +109,9 @@ const getHour12Option = (hourFormat: HourFormat): boolean | undefined => { const getDateTimeFormatter = ( format: TimestampFormat, hourFormat: HourFormat, - timeZone?: string, + timeZone: string, ): Intl.DateTimeFormat => { - const cacheKey = `${timeZone ?? BASE_TIME_FORMAT_OPTIONS.LOCAL}|${format}|${hourFormat}`; + const cacheKey = `${timeZone}|${format}|${hourFormat}`; const cachedFormatter = dateTimeFormatters.get(cacheKey); if (cachedFormatter) return cachedFormatter; @@ -91,7 +119,7 @@ const getDateTimeFormatter = ( const formatter = new Intl.DateTimeFormat(undefined, { ...timestampFormats[format], ...(hour12 !== undefined && { hour12 }), - ...(timeZone && { timeZone }), + timeZone, }); dateTimeFormatters.set(cacheKey, formatter); return formatter; @@ -156,16 +184,13 @@ export function formatDate( ); } + const timeZone = getTimezone(timeFormat); + // Handle ISO format if (format === 'iso') { - return parsed.toISOString(); - } - - if (timeFormat === BASE_TIME_FORMAT_OPTIONS.LOCAL) { - return getDateTimeFormatter(format, hourFormat).format(parsed); + return formatIsoDate(parsed, timeZone); } - const timeZone = getTimezone(timeFormat); return getDateTimeFormatter(format, hourFormat, timeZone).format(parsed); } catch (e) { console.error('Error formatting date:', e); diff --git a/tests/integration/timezone-select.desktop.spec.ts b/tests/integration/timezone-select.desktop.spec.ts new file mode 100644 index 0000000000..3833e5b7c6 --- /dev/null +++ b/tests/integration/timezone-select.desktop.spec.ts @@ -0,0 +1,24 @@ +import { expect, test } from '@playwright/test'; + +import { mockWorkflowsApis } from '~/test-utilities/mock-apis'; + +test.use({ timezoneId: 'Europe/Helsinki' }); + +test('applies UTC and local timezone to the ISO timestamp preview', async ({ + page, +}) => { + await mockWorkflowsApis(page); + await page.clock.setFixedTime(new Date('2026-01-15T12:00:00.000Z')); + await page.goto('/namespaces/default/workflows'); + await page.getByTestId('timezones-menu-button').click(); + await page.getByRole('button', { name: 'ISO', exact: true }).click(); + + const preview = page.locator('#timezones-menu p.text-xs'); + await page.getByTestId('timezones-UTC').click(); + await page.getByTestId('timezones-menu-button').click(); + await expect(preview).toHaveText('2026-01-15T12:00:00.000Z'); + + await page.getByTestId('timezones-local').click(); + await page.getByTestId('timezones-menu-button').click(); + await expect(preview).toHaveText('2026-01-15T14:00:00.000+02:00'); +});