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
7 changes: 7 additions & 0 deletions .changeset/time-zone-preference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/common-utils': patch
'@hyperdx/app': patch
'@hyperdx/api': patch
---

refactor: Store the time display setting as a time zone instead of a UTC switch, converting existing settings, and remove the `isUTC` option from `formatDate`
10 changes: 7 additions & 3 deletions packages/api/src/tasks/checkAlerts/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Metadata } from '@hyperdx/common-utils/dist/core/metadata';
import { getPromqlSeries } from '@hyperdx/common-utils/dist/core/promql';
import { renderChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig';
import { buildSearchChartConfig } from '@hyperdx/common-utils/dist/core/searchChartConfig';
import { formatDate, objectHash } from '@hyperdx/common-utils/dist/core/utils';
import {
formatDate,
objectHash,
UTC_TIME_ZONE,
} from '@hyperdx/common-utils/dist/core/utils';
import {
isPromqlSavedChartConfig,
isRawSqlSavedChartConfig,
Expand Down Expand Up @@ -881,9 +885,9 @@ export const renderAlertTemplate = async ({
};

const timeRangeMessage = `Time Range (UTC): [${formatDate(view.startTime, {
isUTC: true,
timeZone: UTC_TIME_ZONE,
})} - ${formatDate(view.endTime, {
isUTC: true,
timeZone: UTC_TIME_ZONE,
})})`;
let rawTemplateBody;

Expand Down
8 changes: 6 additions & 2 deletions packages/app/src/UserPreferencesModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { Badge } from '@clickhouse/click-ui';
import { UTC_TIME_ZONE } from '@hyperdx/common-utils/dist/core/utils';
import {
Autocomplete,
Divider,
Expand All @@ -19,6 +20,7 @@ import {
} from './config/fonts';
import { useAppTheme } from './theme/ThemeProvider';
import { isValidThemeName, themes } from './theme';
import { LOCAL_TIME_ZONE } from './timeZone';
import {
DEFAULT_ROW_CLICK_ACTION,
RowClickAction,
Expand Down Expand Up @@ -114,10 +116,12 @@ export const UserPreferencesModal = ({
<Switch
size="md"
onLabel="UTC"
checked={userPreferences.isUTC}
checked={userPreferences.timeZone === UTC_TIME_ZONE}
onChange={e =>
setUserPreference({
isUTC: e.currentTarget.checked,
timeZone: e.currentTarget.checked
? UTC_TIME_ZONE
: LOCAL_TIME_ZONE,
})
}
/>
Expand Down
38 changes: 38 additions & 0 deletions packages/app/src/__tests__/timeZone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
getTimeZoneShortName,
isValidTimeZone,
LOCAL_TIME_ZONE,
resolveTimeZonePreference,
} from '@/timeZone';

describe('resolveTimeZonePreference', () => {
it('returns undefined for the local setting', () => {
expect(resolveTimeZonePreference(LOCAL_TIME_ZONE)).toBeUndefined();
});

it('returns a valid IANA name', () => {
expect(resolveTimeZonePreference('Etc/UTC')).toBe('Etc/UTC');
expect(resolveTimeZonePreference('Asia/Tokyo')).toBe('Asia/Tokyo');
});

it('falls back to local for a zone the browser does not recognise', () => {
expect(isValidTimeZone('Mars/Olympus_Mons')).toBe(false);
expect(resolveTimeZonePreference('Mars/Olympus_Mons')).toBeUndefined();
});
});

describe('getTimeZoneShortName', () => {
it('uses the abbreviation in effect on the date', () => {
expect(
getTimeZoneShortName('America/Los_Angeles', new Date('2026-07-01Z')),
).toBe('PDT');
expect(
getTimeZoneShortName('America/Los_Angeles', new Date('2026-12-01Z')),
).toBe('PST');
});

it('names UTC and zones without an abbreviation', () => {
expect(getTimeZoneShortName('Etc/UTC')).toBe('UTC');
expect(getTimeZoneShortName('Asia/Tokyo')).toBe('GMT+9');
});
});
69 changes: 58 additions & 11 deletions packages/app/src/__tests__/useUserPreferences.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('migrateUserPreferences', () => {
const result = migrateUserPreferences(legacyData);

expect(result).toEqual({
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
colorMode: 'dark',
font: 'IBM Plex Mono',
Expand All @@ -84,7 +84,7 @@ describe('migrateUserPreferences', () => {
const result = migrateUserPreferences(legacyData);

expect(result).toEqual({
isUTC: true,
timeZone: 'Etc/UTC',
timeFormat: '24h',
colorMode: 'light',
font: 'Inter',
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('migrateUserPreferences', () => {
const result = migrateUserPreferences(legacyData);

expect(result).toEqual({
isUTC: true,
timeZone: 'Etc/UTC',
timeFormat: '24h',
colorMode: 'light',
font: 'Roboto Mono',
Expand All @@ -154,7 +154,7 @@ describe('migrateUserPreferences', () => {
const result = migrateUserPreferences(legacyData);

expect(result).toEqual({
isUTC: false, // From DEFAULT_PREFERENCES
timeZone: 'local', // From DEFAULT_PREFERENCES
timeFormat: '12h', // From DEFAULT_PREFERENCES
colorMode: 'dark', // Migrated from theme
font: 'IBM Plex Mono', // From DEFAULT_PREFERENCES
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('migrateUserPreferences', () => {
const result = migrateUserPreferences(partialData);

expect(result).toEqual({
isUTC: false, // From DEFAULT_PREFERENCES
timeZone: 'local', // From DEFAULT_PREFERENCES
timeFormat: '12h', // From DEFAULT_PREFERENCES
colorMode: 'light', // Migrated from theme
font: 'IBM Plex Mono', // From DEFAULT_PREFERENCES
Expand All @@ -241,7 +241,7 @@ describe('migrateUserPreferences', () => {
const result = migrateUserPreferences(legacyData);

expect(result).toEqual({
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
colorMode: 'dark',
font: 'IBM Plex Mono',
Expand All @@ -254,7 +254,7 @@ describe('migrateUserPreferences', () => {
describe('Migration idempotency', () => {
it('should return already migrated data unchanged', () => {
const migratedData: UserPreferences = {
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
colorMode: 'dark',
font: 'IBM Plex Mono',
Expand All @@ -269,7 +269,7 @@ describe('migrateUserPreferences', () => {

it('should accept colorMode "system" as valid', () => {
const dataWithSystem: UserPreferences = {
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
colorMode: 'system',
font: 'IBM Plex Mono',
Expand All @@ -286,7 +286,7 @@ describe('migrateUserPreferences', () => {
const mixedData = JSON.stringify({
theme: 'light',
colorMode: 'dark',
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
font: 'IBM Plex Mono',
});
Expand Down Expand Up @@ -323,6 +323,53 @@ describe('migrateUserPreferences', () => {
});
});

describe('isUTC migration (isUTC → timeZone)', () => {
it.each([
[true, 'Etc/UTC'],
[false, 'local'],
])('replaces isUTC=%s with timeZone=%s and saves it', (isUTC, timeZone) => {
const stored = JSON.stringify({
isUTC,
timeFormat: '24h',
colorMode: 'dark',
font: 'Inter',
});

const result = migrateUserPreferences(stored);

expect(result).toEqual({
timeZone,
timeFormat: '24h',
colorMode: 'dark',
font: 'Inter',
});
expect(localStorageMock.setItem).toHaveBeenCalledWith(
STORAGE_KEY,
JSON.stringify(result),
);
});

it('defaults to local when neither isUTC nor timeZone is stored', () => {
const result = migrateUserPreferences(
JSON.stringify({ colorMode: 'dark', timeFormat: '12h' }),
);
expect(result?.timeZone).toBe('local');
});

it('keeps a stored timeZone over isUTC', () => {
const result = migrateUserPreferences(
JSON.stringify({
isUTC: true,
timeZone: 'Asia/Tokyo',
colorMode: 'dark',
timeFormat: '12h',
}),
);
expect(result?.timeZone).toBe('Asia/Tokyo');
expect(result).not.toHaveProperty('isUTC');
});
});

describe('localStorage error handling', () => {
it('should handle localStorage.setItem errors gracefully', () => {
localStorageMock.setItem.mockImplementation(() => {
Expand All @@ -339,7 +386,7 @@ describe('migrateUserPreferences', () => {
// Should not throw, should return migrated data even if save fails
const result = migrateUserPreferences(legacyData);
expect(result).toEqual({
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
colorMode: 'dark',
font: 'IBM Plex Mono',
Expand All @@ -365,7 +412,7 @@ describe('migrateUserPreferences', () => {
// Should not throw - should return migrated data
const result = migrateUserPreferences(legacyData);
expect(result).toEqual({
isUTC: false,
timeZone: 'local',
timeFormat: '12h',
colorMode: 'dark',
font: 'IBM Plex Mono',
Expand Down
14 changes: 7 additions & 7 deletions packages/app/src/components/AppNav/AppNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ import InstallInstructionModal from '@/InstallInstructionsModal';
import OnboardingChecklist from '@/OnboardingChecklist';
import { useSavedSearches } from '@/savedSearch';
import { useLogomark, useWordmark } from '@/theme/ThemeProvider';
import { getTimeZoneShortName } from '@/timeZone';
import { useDisplayTimeZone } from '@/useDisplayTimeZone';
import { UserPreferencesModal } from '@/UserPreferencesModal';
import { useUserPreferences } from '@/useUserPreferences';

import {
AppNavCloudBanner,
Expand Down Expand Up @@ -276,9 +277,8 @@ export default function AppNav({ fixed = false }: { fixed?: boolean }) {
{ close: closeUserPreferences, open: openUserPreferences },
] = useDisclosure(false);

const {
userPreferences: { isUTC },
} = useUserPreferences();
const timeZone = useDisplayTimeZone();
const timeZoneShortName = timeZone && getTimeZoneShortName(timeZone);

const [
showInstallInstructions,
Expand Down Expand Up @@ -373,12 +373,12 @@ export default function AppNav({ fixed = false }: { fixed?: boolean }) {
would be redundant and crowd this already-tight row. */}
<InstanceLabel
fallback={
isUTC && (
timeZoneShortName && (
<Badge
size="sm"
text="UTC"
text={timeZoneShortName}
state="neutral"
title="Showing time in UTC"
title={`Showing time in ${timeZoneShortName}`}
/>
)
}
Expand Down
9 changes: 6 additions & 3 deletions packages/app/src/components/DBRowTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ import {
MIN_LAST_COLUMN_WIDTH,
UNDEFINED_WIDTH,
} from '@/tableUtils';
import { getTimeZoneShortName } from '@/timeZone';
import { useDisplayTimeZone } from '@/useDisplayTimeZone';
import { FormatTime } from '@/useFormatTime';
import {
DEFAULT_ROW_CLICK_ACTION,
Expand Down Expand Up @@ -464,8 +466,9 @@ export const RawLogTable = memo(
);

const {
userPreferences: { isUTC, rowClickAction },
userPreferences: { rowClickAction },
} = useUserPreferences();
const timeZone = useDisplayTimeZone();
const { scale: contentFontScale } = useContentFontSize();

// The ClickHouse dashboard's slow-query list has no side panel to open, so
Expand Down Expand Up @@ -672,7 +675,7 @@ export const RawLogTable = memo(
// TODO: add support for sorting on Dynamic JSON fields
enableSorting: jsColumnType !== JSDataType.Dynamic,
accessorFn: curry(retrieveColumnValue)(column), // Columns can contain '.' and will not work with accessorKey
header: `${columnNameMap?.[column] ?? column}${isDate ? (isUTC ? ' (UTC)' : ' (Local)') : ''}`,
header: `${columnNameMap?.[column] ?? column}${isDate ? ` (${timeZone ? getTimeZoneShortName(timeZone) : 'Local'})` : ''}`,
Comment thread
karl-power marked this conversation as resolved.
Resolved
Comment thread
karl-power marked this conversation as resolved.
Resolved
cell: info => {
const value = info.getValue<any>(); // This can be any type realistically (numbers, strings, etc.)

Expand Down Expand Up @@ -743,7 +746,7 @@ export const RawLogTable = memo(
}) as ColumnDef<any>[]),
],
[
isUTC,
timeZone,
highlightedLineId,
displayedColumns,
columnSizeOpts,
Expand Down
46 changes: 46 additions & 0 deletions packages/app/src/timeZone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { UTC_TIME_ZONE } from '@hyperdx/common-utils/dist/core/utils';

/** Time zone setting value that follows the browser's zone. */
export const LOCAL_TIME_ZONE = 'local';

export function isValidTimeZone(timeZone: string): boolean {
try {
new Intl.DateTimeFormat('en-US', { timeZone });
return true;
} catch {
return false;
}
}

/**
* The IANA zone a setting refers to, or undefined for the local zone. Zones
* the browser doesn't recognise fall back to local.
*/
export function resolveTimeZonePreference(
preference: string,
): string | undefined {
return preference !== LOCAL_TIME_ZONE && isValidTimeZone(preference)
? preference
: undefined;
}

/** The setting that `isUTC`, the switch that predates it, maps to. */
export function timeZonePreferenceFromIsUTC(isUTC: unknown) {
return isUTC === true ? UTC_TIME_ZONE : LOCAL_TIME_ZONE;
}

/**
* Short name of `timeZone` at `date`, e.g. "UTC", "PDT" or "GMT+9". en-US is
* used because it has abbreviations for the most zones.
*/
export function getTimeZoneShortName(
timeZone: string,
// eslint-disable-next-line no-restricted-syntax
date: Date = new Date(),
): string {
return (
new Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'short' })
.formatToParts(date)
.find(part => part.type === 'timeZoneName')?.value ?? timeZone
);
}
7 changes: 4 additions & 3 deletions packages/app/src/useDisplayTimeZone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UTC_TIME_ZONE } from '@hyperdx/common-utils/dist/core/utils';
import { useMemo } from 'react';

import { resolveTimeZonePreference } from './timeZone';
import { useUserPreferences } from './useUserPreferences';

/**
Expand All @@ -8,7 +9,7 @@ import { useUserPreferences } from './useUserPreferences';
*/
export function useDisplayTimeZone(): string | undefined {
const {
userPreferences: { isUTC },
userPreferences: { timeZone },
} = useUserPreferences();
return isUTC ? UTC_TIME_ZONE : undefined;
return useMemo(() => resolveTimeZonePreference(timeZone), [timeZone]);
}
Loading
Loading