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

fix: Read and show time picker ranges in the display time zone, so applying a range with "Use UTC time" on no longer shifts it for users west of UTC
32 changes: 13 additions & 19 deletions packages/app/src/DBDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ import {
useNewTimeQuery,
} from './timeQuery';
import { useConfirm } from './useConfirm';
import { useDisplayTimeZone } from './useDisplayTimeZone';
import { FormatTime } from './useFormatTime';
import { useUserPreferences } from './useUserPreferences';
import { getMetricTableName, useLocalStorage } from './utils';
import { useZIndex, ZIndexContext } from './zIndex';

Expand Down Expand Up @@ -534,16 +534,14 @@ const Tile = ({
setHasBeenVisible(true);
}

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

// Date range and granularity state local to the fullscreen view so that
// changing them does not propagate up to the dashboard.
const [fullscreenDateRange, setFullscreenDateRange] =
useState<[Date, Date]>(dateRange);
const [fullscreenInputValue, setFullscreenInputValue] = useState<string>(() =>
dateRangeToString(dateRange, isUTC),
dateRangeToString(dateRange, timeZone),
);
const [fullscreenGranularity, setFullscreenGranularity] = useState<
Granularity | 'auto' | undefined
Expand All @@ -553,21 +551,21 @@ const Tile = ({
// Reinitialize to the dashboard's current date range and granularity
// each time the fullscreen view is opened.
setFullscreenDateRange(dateRange);
setFullscreenInputValue(dateRangeToString(dateRange, isUTC));
setFullscreenInputValue(dateRangeToString(dateRange, timeZone));
setFullscreenGranularity(
(granularity as Granularity | undefined) ?? 'auto',
);
setIsFullscreen(true);
}, [dateRange, granularity, isUTC]);
}, [dateRange, granularity, timeZone]);

const handleFullscreenSearch = useCallback(
(value: string) => {
const [start, end] = parseTimeRangeInput(value, isUTC);
const [start, end] = parseTimeRangeInput(value, timeZone);
if (start != null && end != null) {
setFullscreenDateRange([start, end]);
}
},
[isUTC],
[timeZone],
);

useEffect(() => {
Expand Down Expand Up @@ -1880,9 +1878,7 @@ function DBDashboardPage({
} = dashboardProps;
const title = usePageTitle(dashboard?.name ? dashboard.name : 'Dashboard');
const confirm = useConfirm();
const {
userPreferences: { isUTC },
} = useUserPreferences();
const timeZone = useDisplayTimeZone();

const router = useRouter();
const dashboardId = router.query.dashboardId as string | undefined;
Expand Down Expand Up @@ -2169,7 +2165,7 @@ function DBDashboardPage({

const currentRelativeDateRange = timeRangeInputToSeconds(
displayedTimeInputValue,
isUTC,
timeZone,
);

setDashboard(
Expand Down Expand Up @@ -2202,7 +2198,7 @@ function DBDashboardPage({
getValues,
filterValueEntries,
onSubmit,
isUTC,
timeZone,
displayedTimeInputValue,
]);
const handleRemoveSavedQuery = useCallback(() => {
Expand Down Expand Up @@ -3542,9 +3538,7 @@ function DBDashboardPageGuarded({
dashboardId: dashboardId as string | undefined,
presetConfig,
});
const {
userPreferences: { isUTC },
} = useUserPreferences();
const timeZone = useDisplayTimeZone();

const savedDateRange = dashboardProps.dashboard?.savedDateRange;
// Keyed on the saved range, not the render: a relative range re-stringifies
Expand All @@ -3557,8 +3551,8 @@ function DBDashboardPageGuarded({
? parseRelativeTimeQuery(savedDateRange.value * 1000)
: savedDateRange.value.map(v => new Date(v));
// TODO: show relative ranges as "Past Xh" via getRelativeInterval
return dateRangeToString([start, end], isUTC);
}, [savedDateRange, isUTC]);
return dateRangeToString([start, end], timeZone);
}, [savedDateRange, timeZone]);

if (!dashboardProps || !router.isReady) return <Loader size="lg" />;

Expand Down
10 changes: 5 additions & 5 deletions packages/app/src/__tests__/timeQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('dateRangeToString', () => {
expect(
dateRangeToString(
[new Date('2026-03-03T10:00:00'), new Date('2026-03-05T10:00:00')],
false,
undefined,
),
).toBe('Mar 3 10:00:00 - Mar 5 10:00:00');
});
Expand All @@ -26,16 +26,16 @@ describe('dateRangeToString', () => {
expect(
dateRangeToString(
[new Date('2024-03-03T10:00:00'), new Date('2024-03-05T10:00:00')],
false,
undefined,
),
).toBe('Mar 3 2024 10:00:00 - Mar 5 2024 10:00:00');
});

it('formats in UTC when isUTC is set', () => {
it('formats in the given time zone', () => {
expect(
dateRangeToString(
[new Date('2024-12-31T23:00:00Z'), new Date('2025-01-01T01:00:00Z')],
true,
'Etc/UTC',
),
).toBe('Dec 31 2024 23:00:00 - Jan 1 2025 01:00:00');
});
Expand All @@ -47,7 +47,7 @@ describe('dateRangeToString', () => {
[new Date('2026-08-01T10:00:00'), new Date('2026-09-01T10:00:00')],
])('round-trips %s - %s through parseTimeRangeInput', (start, end) => {
const [parsedStart, parsedEnd] = parseTimeRangeInput(
dateRangeToString([start, end], false),
dateRangeToString([start, end], undefined),
);
expect(parsedStart).toEqual(start);
expect(parsedEnd).toEqual(end);
Expand Down
45 changes: 35 additions & 10 deletions packages/app/src/components/TimePicker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { DateInput, DateInputProps } from '@mantine/dates';
import { useDisclosure } from '@mantine/hooks';
import { IconBolt, IconCalendarFilled } from '@tabler/icons-react';

import { useDisplayTimeZone } from '@/useDisplayTimeZone';
import { useUserPreferences } from '@/useUserPreferences';

import { TimePickerMode } from './types';
Expand All @@ -34,10 +35,12 @@ import {
DURATION_OPTIONS,
DURATIONS,
formatTimeRangeText,
fromDateInputValue,
LIVE_TAIL_DURATION_MS,
LIVE_TAIL_TIME_QUERY,
parseTimeRangeInput,
RELATIVE_TIME_OPTIONS,
toDateInputValue,
} from './utils';

const modeAtom = atomWithStorage<TimePickerMode>(
Expand All @@ -56,21 +59,31 @@ const toDate = (v: Date | string | null): Date | null =>
* Wrapper around Mantine v9 DateInput that bridges the Date ↔ string gap.
* Mantine v9 DateInput expects/emits string values, but the TimePickerForm
* stores Date objects (used by date-fns). This wrapper converts in both
* directions: value (Date → string) and onChange (string → Date).
* directions: value (Date → string) and onChange (string → Date), reading
* and writing the strings as clock times in `timeZone`.
*
* `withTime` is required: by default DateInput strips the time part and
* normalizes values to midnight, even when `valueFormat` includes time
* tokens. Setting `withTime` preserves HH:mm:ss so manually-typed times
* survive blur/commit.
*/
type DateInputCmpProps = Omit<DateInputProps, 'value' | 'onChange'> & {
type DateInputCmpProps = Omit<
DateInputProps,
'value' | 'onChange' | 'minDate' | 'maxDate'
> & {
value?: Date | null;
onChange?: (value: Date | null) => void;
minDate?: Date | null;
maxDate?: Date;
timeZone: string | undefined;
};

const DateInputCmp = ({
value,
onChange: onChangeProp,
minDate,
maxDate,
timeZone,
...props
}: DateInputCmpProps) => (
<DateInput
Expand All @@ -80,15 +93,20 @@ const DateInputCmp = ({
placeholder={DATE_INPUT_PLACEHOLDER}
valueFormat={DATE_INPUT_FORMAT}
variant="filled"
dateParser={dateParser}
dateParser={input => {
const date = dateParser(input, timeZone);
return date && toDateInputValue(date, timeZone);
}}
onKeyDown={e => {
if (e.key === 'Enter' && e.target instanceof HTMLInputElement) {
e.target.blur();
}
}}
{...props}
value={value instanceof Date ? value.toISOString() : (value ?? null)}
onChange={v => onChangeProp?.(toDate(v))}
minDate={minDate ? toDateInputValue(minDate, timeZone) : undefined}
maxDate={maxDate ? toDateInputValue(maxDate, timeZone) : undefined}
value={value ? toDateInputValue(value, timeZone) : null}
onChange={v => onChangeProp?.(v ? fromDateInputValue(v, timeZone) : null)}
/>
);

Expand Down Expand Up @@ -124,6 +142,7 @@ const TimePickerComponent = ({
const {
userPreferences: { timeFormat },
} = useUserPreferences();
const timeZone = useDisplayTimeZone();

const [opened, { close, toggle }] = useDisclosure(false);

Expand All @@ -145,8 +164,8 @@ const TimePickerComponent = ({
const form = useTimePickerForm({ mode });

const dateRange = React.useMemo(() => {
return parseTimeRangeInput(value);
}, [value]);
return parseTimeRangeInput(value, timeZone);
}, [value, timeZone]);

React.useEffect(() => {
// Only update form values from external dateRange when popover is closed
Expand Down Expand Up @@ -191,12 +210,15 @@ const TimePickerComponent = ({
if (!from || !to) {
return;
}
const rangeStr = formatTimeRangeText([from, to], { clock: timeFormat });
const rangeStr = formatTimeRangeText([from, to], {
timeZone,
clock: timeFormat,
});
onChange(rangeStr);
onSearch(rangeStr);
close();
},
[close, onChange, onSearch, timeFormat],
[close, onChange, onSearch, timeFormat, timeZone],
);

const handleApply = React.useCallback(() => {
Expand Down Expand Up @@ -468,6 +490,7 @@ const TimePickerComponent = ({
<>
<H>Start time</H>
<DateInputCmp
timeZone={timeZone}
disabled={isRelative}
popoverProps={dateComponentPopoverProps}
maxDate={today}
Expand All @@ -476,9 +499,10 @@ const TimePickerComponent = ({
/>
<H>End time</H>
<DateInputCmp
timeZone={timeZone}
popoverProps={dateComponentPopoverProps}
maxDate={today}
minDate={form.values.startDate ?? undefined}
minDate={toDate(form.values.startDate)}
disabled={isRelative}
{...form.getInputProps('endDate')}
/>
Expand All @@ -487,6 +511,7 @@ const TimePickerComponent = ({
<>
<H>Time</H>
<DateInputCmp
timeZone={timeZone}
disabled={isRelative}
popoverProps={dateComponentPopoverProps}
maxDate={today}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { fireEvent, screen } from '@testing-library/react';

import { TimePicker } from '@/components/TimePicker';

let mockTimeZone: string | undefined;
jest.mock('@/useDisplayTimeZone', () => ({
useDisplayTimeZone: () => mockTimeZone,
}));

// Tests run with TZ=America/New_York, behind UTC, where a UTC range ending
// now used to be read as local time, land in the future, and get clamped.
describe('TimePicker time zone', () => {
const LAST_HOUR_UTC = 'Oct 22 14:00:00 - Oct 22 15:00:00';

beforeEach(() => {
// Only fake the clock: Mantine's popover relies on real timers.
jest.useFakeTimers({
now: new Date('2026-10-22T15:00:00Z'),
doNotFake: [
'setTimeout',
'clearTimeout',
'setInterval',
'clearInterval',
'setImmediate',
'clearImmediate',
'requestAnimationFrame',
'cancelAnimationFrame',
'queueMicrotask',
'nextTick',
],
});
});

afterEach(() => {
jest.useRealTimers();
});

function renderPicker(timeZone: string | undefined, inputValue: string) {
mockTimeZone = timeZone;
const onSearch = jest.fn();
renderWithMantine(
<TimePicker
inputValue={inputValue}
setInputValue={jest.fn()}
onSearch={onSearch}
/>,
);
fireEvent.click(screen.getByTestId('time-picker-input'));
return { onSearch };
}

it('shows and applies the range in the display time zone', async () => {
const { onSearch } = renderPicker('Etc/UTC', LAST_HOUR_UTC);

expect(
await screen.findAllByDisplayValue('2026-10-22 14:00:00'),
).not.toHaveLength(0);
expect(screen.getAllByDisplayValue('2026-10-22 15:00:00')).not.toHaveLength(
0,
);

fireEvent.click(screen.getByTestId('time-picker-apply'));
expect(onSearch).toHaveBeenCalledWith(
'Oct 22 2:00:00 PM - Oct 22 3:00:00 PM',
);
});

it('shows the range in the local zone without a display time zone', async () => {
renderPicker(undefined, 'Oct 22 10:00:00 - Oct 22 11:00:00');

expect(
await screen.findAllByDisplayValue('2026-10-22 10:00:00'),
).not.toHaveLength(0);
expect(screen.getAllByDisplayValue('2026-10-22 11:00:00')).not.toHaveLength(
0,
);
});
});
Loading
Loading