fix(dashboard): name Etc/GMT world clocks by their real offset

An Etc/GMT zone counts westward as positive, so Etc/GMT-4 is four hours ahead of UTC. The clock's title and the zone picker printed the id's own sign, and someone who picked GMT+4 got a clock four hours behind UTC; both now read the real offset, on the desktop and the phone.

Closes #3175
This commit is contained in:
Maurice
2026-11-23 19:42:40 +01:00
parent c8adf6e1da
commit 07eeae8486
4 changed files with 39 additions and 5 deletions
@@ -2,7 +2,7 @@
import { act, renderHook, waitFor } from '@testing-library/react';
import { useSettingsStore } from '../../store/settingsStore';
import { shortZone, useDashboardTimezones } from './useDashboardTimezones';
import { shortZone, useDashboardTimezones, zoneOptionLabel } from './useDashboardTimezones';
const updateSetting = vi.fn<(key: string, value: unknown) => Promise<void>>();
const home = Intl.DateTimeFormat().resolvedOptions().timeZone;
@@ -32,6 +32,22 @@ describe('shortZone', () => {
});
});
describe('Etc/GMT zones (#3175)', () => {
it('FE-COMP-TZHOOK-010: names an Etc/GMT zone by its real offset, which is the opposite of the sign in its id', () => {
expect(shortZone('Etc/GMT-4')).toBe('GMT+4');
expect(shortZone('Etc/GMT+4')).toBe('GMT-4');
expect(shortZone('Etc/GMT-14')).toBe('GMT+14');
expect(shortZone('Etc/GMT+0')).toBe('GMT');
expect(shortZone('Etc/UTC')).toBe('UTC');
});
it('FE-COMP-TZHOOK-011: the picker lists an Etc/GMT zone under its real offset and every other zone by its id', () => {
expect(zoneOptionLabel('Etc/GMT-4')).toBe('GMT+4');
expect(zoneOptionLabel('Etc/GMT+4')).toBe('GMT-4');
expect(zoneOptionLabel('America/New_York')).toBe('America/New York');
});
});
describe('useDashboardTimezones', () => {
it('FE-COMP-TZHOOK-002: an unset list falls back to home plus the defaults, a stored one is honoured', () => {
const fallback = renderHook(() => useDashboardTimezones('en'));
@@ -28,12 +28,30 @@ const FALLBACK_ZONES = [
'UTC',
];
// Etc/GMT±N counts westward as positive (POSIX), so Etc/GMT-4 is four hours ahead of UTC.
const ETC_GMT = /^Etc\/GMT([+-])(\d{1,2})$/;
/** The real offset of an Etc/GMT zone (Etc/GMT-4 reads GMT+4), or null for any other zone. */
function etcGmtLabel(tz: string): string | null {
const match = ETC_GMT.exec(tz);
if (!match) return null;
if (Number(match[2]) === 0) return 'GMT';
return `GMT${match[1] === '+' ? '-' : '+'}${match[2]}`;
}
/** The city part of a zone id, so America/New_York reads New York. */
export function shortZone(tz: string): string {
const etc = etcGmtLabel(tz);
if (etc) return etc;
const city = tz.split('/').pop() || tz;
return city.replace(/_/g, ' ');
}
/** How the zone picker lists a zone: its id, with Etc/GMT zones under their real offset. */
export function zoneOptionLabel(tz: string): string {
return etcGmtLabel(tz) ?? tz.replace(/_/g, ' ');
}
/**
* The dashboard's world clocks: the zone list lives in the user's settings, the
* clocks tick every 30 seconds, `adding` toggles the zone picker.