Skip to content
Merged
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
12 changes: 6 additions & 6 deletions apps/meteor/client/hooks/useTimezoneNameList.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getTimezoneNames } from '@rocket.chat/tools';
import { useMemo } from 'react';

const getTimeZoneNames = (): string[] => {
const intl = Intl as typeof Intl & { supportedValuesOf?(key: 'timeZone'): string[] };
return typeof intl.supportedValuesOf === 'function' ? intl.supportedValuesOf('timeZone') : [];
};

export const useTimezoneNameList = (): string[] => useMemo(() => getTimeZoneNames(), []);
export const useTimezoneNameList = (): string[] =>
useMemo(() => {
const names = getTimezoneNames();
return names.includes('UTC') ? names : ['UTC', ...names];
}, []);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, FieldHint, FieldLabel, FieldRow, Select } from '@rocket.chat/fuselage';
import { canonicalizeTimezone } from '@rocket.chat/tools';
import type { ReactElement } from 'react';

import { useTimezoneNameList } from '../../../../../hooks/useTimezoneNameList';
Expand Down Expand Up @@ -38,7 +39,7 @@ function SelectTimezoneSettingInput({
<FieldRow>
<Select
id={_id}
value={value}
value={typeof value === 'string' ? canonicalizeTimezone(value) : value}
placeholder={placeholder}
disabled={disabled}
readOnly={readonly}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ILivechatBusinessHour, LivechatBusinessHourTypes, Serialized } from '@rocket.chat/core-typings';
import { Box, Button, ButtonGroup } from '@rocket.chat/fuselage';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { canonicalizeTimezone } from '@rocket.chat/tools';
import { Page, PageFooter, PageHeader, PageScrollableContentWithShadow } from '@rocket.chat/ui-client';
import { useToastMessageDispatch, useTranslation, useRouter, useEndpoint } from '@rocket.chat/ui-contexts';
import { useId } from 'react';
Expand All @@ -14,7 +15,7 @@ import { useRemoveBusinessHour } from './useRemoveBusinessHour';

const getInitialData = (businessHourData: Serialized<ILivechatBusinessHour> | undefined) => ({
name: businessHourData?.name || '',
timezoneName: businessHourData?.timezone?.name || 'America/Sao_Paulo',
timezoneName: canonicalizeTimezone(businessHourData?.timezone?.name || 'America/Sao_Paulo'),
daysOpen: (businessHourData?.workHours || defaultWorkHours()).filter(({ open }) => !!open).map(({ day }) => day),
daysTime: (businessHourData?.workHours || defaultWorkHours())
.filter(({ open }) => !!open)
Expand Down
57 changes: 57 additions & 0 deletions packages/tools/src/timezone.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { canonicalizeTimezone, getTimezoneNames } from './timezone';

describe('canonicalizeTimezone', () => {
it('returns the same value for a canonical IANA zone', () => {
expect(canonicalizeTimezone('America/Sao_Paulo')).toBe('America/Sao_Paulo');
});

it('resolves plain UTC to UTC', () => {
expect(canonicalizeTimezone('UTC')).toBe('UTC');
});

it('resolves Etc/UTC and Etc/GMT to UTC', () => {
expect(canonicalizeTimezone('Etc/UTC')).toBe('UTC');
expect(canonicalizeTimezone('Etc/GMT')).toBe('UTC');
});

it('resolves legacy moment aliases to their canonical zone', () => {
expect(canonicalizeTimezone('GMT')).toBe('UTC');
expect(canonicalizeTimezone('Zulu')).toBe('UTC');
expect(canonicalizeTimezone('Universal')).toBe('UTC');
expect(canonicalizeTimezone('US/Pacific')).toBe('America/Los_Angeles');
expect(canonicalizeTimezone('Japan')).toBe('Asia/Tokyo');
});

it('resolves legacy IANA names to modern canonical names', () => {
expect(canonicalizeTimezone('Asia/Calcutta')).toBe('Asia/Kolkata');
expect(canonicalizeTimezone('Asia/Katmandu')).toBe('Asia/Kathmandu');
expect(canonicalizeTimezone('Asia/Rangoon')).toBe('Asia/Yangon');
expect(canonicalizeTimezone('Asia/Saigon')).toBe('Asia/Ho_Chi_Minh');
expect(canonicalizeTimezone('Europe/Kiev')).toBe('Europe/Kyiv');
expect(canonicalizeTimezone('America/Godthab')).toBe('America/Nuuk');
expect(canonicalizeTimezone('Pacific/Enderbury')).toBe('Pacific/Kanton');
});

it('preserves modern canonical names as-is', () => {
expect(canonicalizeTimezone('Asia/Kolkata')).toBe('Asia/Kolkata');
expect(canonicalizeTimezone('Europe/Kyiv')).toBe('Europe/Kyiv');
expect(canonicalizeTimezone('Asia/Ho_Chi_Minh')).toBe('Asia/Ho_Chi_Minh');
});

it('returns the input unchanged when it is not a recognized zone', () => {
const input = 'Not/A_Zone';
expect(canonicalizeTimezone(input)).toBe(input);
});
});

describe('getTimezoneNames', () => {
it('returns modern canonical names instead of legacy ones', () => {
const names = getTimezoneNames();
expect(names).toContain('Asia/Kolkata');
expect(names).not.toContain('Asia/Calcutta');
expect(names).toContain('Europe/Kyiv');
expect(names).not.toContain('Europe/Kiev');
expect(names).toContain('Asia/Yangon');
expect(names).not.toContain('Asia/Rangoon');
});
});
44 changes: 42 additions & 2 deletions packages/tools/src/timezone.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
// Zones where Node/browser Intl returns a legacy IANA name instead of the
// current canonical one. Workaround until Temporal lands (~late 2026).
// Source: https://data.iana.org/time-zones/tzdb/backward
// Ref: https://github.com/tc39/proposal-temporal/issues/3249
const LEGACY_TO_CANONICAL: Record<string, string> = {
'America/Buenos_Aires': 'America/Argentina/Buenos_Aires',
'America/Catamarca': 'America/Argentina/Catamarca',
'America/Cordoba': 'America/Argentina/Cordoba',
'America/Godthab': 'America/Nuuk',
'America/Indianapolis': 'America/Indiana/Indianapolis',
'America/Jujuy': 'America/Argentina/Jujuy',
'America/Louisville': 'America/Kentucky/Louisville',
'America/Mendoza': 'America/Argentina/Mendoza',
'Asia/Calcutta': 'Asia/Kolkata',
'Asia/Katmandu': 'Asia/Kathmandu',
'Asia/Rangoon': 'Asia/Yangon',
'Asia/Saigon': 'Asia/Ho_Chi_Minh',
'Atlantic/Faeroe': 'Atlantic/Faroe',
'Europe/Kiev': 'Europe/Kyiv',
'Pacific/Enderbury': 'Pacific/Kanton',
};

export const canonicalizeTimezone = (name: string): string => {
try {
const resolved = new Intl.DateTimeFormat(undefined, { timeZone: name }).resolvedOptions().timeZone;
return LEGACY_TO_CANONICAL[resolved] ?? resolved;
} catch {
return name;
}
};

export const getTimezoneNames = (): string[] => {
const intl = Intl as typeof Intl & { supportedValuesOf?(key: 'timeZone'): string[] };
const zones = typeof intl.supportedValuesOf === 'function' ? intl.supportedValuesOf('timeZone') : [];
return zones.map((name) => LEGACY_TO_CANONICAL[name] ?? name).sort();
};

export const guessTimezoneFromOffset = (offset: string | number): string => {
const hours = Number(offset);
const totalMinutes = Math.round(hours * 60);
Expand All @@ -21,7 +58,7 @@ export const guessTimezoneFromOffset = (offset: string | number): string => {
const tzHours = match[1] ? parseInt(match[1], 10) : 0;
const tzMinutes = match[2] ? parseInt(match[2], 10) * (tzHours < 0 ? -1 : 1) : 0;
if (tzHours * 60 + tzMinutes === totalMinutes) {
return tz;
return LEGACY_TO_CANONICAL[tz] ?? tz;
}
}

Expand All @@ -33,4 +70,7 @@ export const guessTimezoneFromOffset = (offset: string | number): string => {
return `Etc/GMT${intHours > 0 ? '-' : '+'}${Math.abs(intHours)}`;
};

export const guessTimezone = (): string => new Intl.DateTimeFormat().resolvedOptions().timeZone;
export const guessTimezone = (): string => {
const resolved = new Intl.DateTimeFormat().resolvedOptions().timeZone;
return LEGACY_TO_CANONICAL[resolved] ?? resolved;
};
Loading