mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
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:
@@ -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.
|
||||
|
||||
@@ -10,7 +10,7 @@ import { useAddonStore } from '../../../store/addonStore'
|
||||
import { entityGradient } from '../../../utils/gradients'
|
||||
import { useCurrencyConverter } from '../../../components/Dashboard/useCurrencyConverter'
|
||||
import { useCollectionLists } from '../../../components/Dashboard/useCollectionLists'
|
||||
import { shortZone, useDashboardTimezones } from '../../../components/Dashboard/useDashboardTimezones'
|
||||
import { shortZone, useDashboardTimezones, zoneOptionLabel } from '../../../components/Dashboard/useDashboardTimezones'
|
||||
import { formatTime, splitReservationDateTime } from '../../../utils/formatters'
|
||||
import { normalizeAppearance, MOBILE_DASH_TOKENS, type MobileDashToken } from '@trek/shared'
|
||||
import { MOMENT_LABEL, upcomingKey, type UpcomingReservation } from '../../../pages/dashboard/dashboardModel'
|
||||
@@ -231,7 +231,7 @@ function MTimezonesWidget(): React.ReactElement {
|
||||
>
|
||||
<option value="" disabled>{t('dashboard.tz.searchPlaceholder')}</option>
|
||||
{allZones.filter(z => !zones.includes(z)).map(z => (
|
||||
<option key={z} value={z}>{z.replace(/_/g, ' ')}</option>
|
||||
<option key={z} value={z}>{zoneOptionLabel(z)}</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
|
||||
@@ -25,7 +25,7 @@ import { IcsSubscribeModal } from '../components/Planner/IcsSubscribeModal'
|
||||
import CollectionsWidget from '../components/Dashboard/CollectionsWidget'
|
||||
import { useCurrencyConverter } from '../components/Dashboard/useCurrencyConverter'
|
||||
import { useDashboardPlugins } from '../components/Dashboard/useDashboardPlugins'
|
||||
import { shortZone, useDashboardTimezones } from '../components/Dashboard/useDashboardTimezones'
|
||||
import { shortZone, useDashboardTimezones, zoneOptionLabel } from '../components/Dashboard/useDashboardTimezones'
|
||||
import PluginWidgets from '../components/Plugins/PluginWidgets'
|
||||
import PluginFrame from '../components/Plugins/PluginFrame'
|
||||
import { TripCardBadges } from '../components/Plugins/TripCardBadges'
|
||||
@@ -701,7 +701,7 @@ function TimezoneTool({ locale }: { locale: string }): React.ReactElement {
|
||||
|
||||
const tzOptions = allZones
|
||||
.filter(z => !zones.includes(z))
|
||||
.map(z => ({ value: z, label: z.replace(/_/g, ' '), searchLabel: z }))
|
||||
.map(z => ({ value: z, label: zoneOptionLabel(z), searchLabel: `${z} ${zoneOptionLabel(z)}` }))
|
||||
|
||||
return (
|
||||
<div className="tool">
|
||||
|
||||
Reference in New Issue
Block a user