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
65 changes: 63 additions & 2 deletions src/lib/utilities/format-date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
43 changes: 34 additions & 9 deletions src/lib/utilities/format-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ export const timestampFormats: Record<
export type TimestampFormat = keyof typeof timestampFormats;

const dateTimeFormatters = new Map<string, Intl.DateTimeFormat>();
const isoFormatters = new Map<string, Intl.DateTimeFormat>();

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;
Expand All @@ -81,17 +109,17 @@ 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;

const hour12 = getHour12Option(hourFormat);
const formatter = new Intl.DateTimeFormat(undefined, {
...timestampFormats[format],
...(hour12 !== undefined && { hour12 }),
...(timeZone && { timeZone }),
timeZone,
});
dateTimeFormatters.set(cacheKey, formatter);
return formatter;
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/timezone-select.desktop.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
Loading