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
3 changes: 2 additions & 1 deletion .changeset/date-picker-keep-year.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
---

fix: the time picker no longer resets a selected date range to the current
year. Range text now always includes the year, so it survives being re-parsed.
year. The range text now includes the year when the range isn't in the current
year (or reaches into the future), so it survives being re-parsed.
31 changes: 16 additions & 15 deletions packages/app/src/__tests__/timeQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ describe('dateRangeToString', () => {
jest.useRealTimers();
});

it.each([
[
'2024-03-03T10:00:00',
'2024-03-05T10:00:00',
'Mar 3 2024 10:00:00 - Mar 5 2024 10:00:00',
],
[
'2026-03-03T10:00:00',
'2026-03-05T10:00:00',
'Mar 3 2026 10:00:00 - Mar 5 2026 10:00:00',
],
])('always includes the year: %s - %s', (start, end, expected) => {
expect(dateRangeToString([new Date(start), new Date(end)], false)).toBe(
expected,
);
it('omits the year for a past range in the current year', () => {
expect(
dateRangeToString(
[new Date('2026-03-03T10:00:00'), new Date('2026-03-05T10:00:00')],
false,
),
).toBe('Mar 3 10:00:00 - Mar 5 10:00:00');
});

it('includes the year for ranges outside the current year', () => {
expect(
dateRangeToString(
[new Date('2024-03-03T10:00:00'), new Date('2024-03-05T10:00:00')],
false,
),
).toBe('Mar 3 2024 10:00:00 - Mar 5 2024 10:00:00');
});

it('formats in UTC when isUTC is set', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/app/src/components/TimePicker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { add, Duration, sub } from 'date-fns';
import { useAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils';
import { useHotkeys } from 'react-hotkeys-hook';
import { formatDate } from '@hyperdx/common-utils/dist/core/utils';
import {
Button,
Card,
Expand Down Expand Up @@ -34,6 +33,7 @@ import {
dateParser,
DURATION_OPTIONS,
DURATIONS,
formatTimeRangeText,
LIVE_TAIL_DURATION_MS,
LIVE_TAIL_TIME_QUERY,
parseTimeRangeInput,
Expand Down Expand Up @@ -191,9 +191,7 @@ const TimePickerComponent = ({
if (!from || !to) {
return;
}
const rangeStr = [from, to]
.map(d => formatDate(d, { format: 'withYear', clock: timeFormat }))
.join(' - ');
const rangeStr = formatTimeRangeText([from, to], { clock: timeFormat });
onChange(rangeStr);
onSearch(rangeStr);
close();
Expand Down
94 changes: 84 additions & 10 deletions packages/app/src/components/TimePicker/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { formatDate } from '@hyperdx/common-utils/dist/core/utils';

import {
dateParser,
formatTimeRangeText,
parseTimeRangeInput,
rangeNeedsExplicitYear,
timeRangeInputToSeconds,
} from '@/components/TimePicker/utils';

Expand Down Expand Up @@ -161,8 +161,55 @@ describe('parseTimeRangeInput', () => {
});
});

// Mirrors how TimePicker.handleSearch builds the range text for each clock.
describe('parseTimeRangeInput with year-qualified picker text', () => {
describe('rangeNeedsExplicitYear', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2025-06-15T12:00:00'));
});

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

it('is false for a past range in the current year', () => {
expect(
rangeNeedsExplicitYear([
new Date('2025-03-01T10:00:00'),
new Date('2025-03-02T10:00:00'),
]),
).toBe(false);
});

it('is true when either date is in another year', () => {
expect(
rangeNeedsExplicitYear([
new Date('2024-03-01T10:00:00'),
new Date('2025-03-02T10:00:00'),
]),
).toBe(true);
});

it('is true when a date in the current year is in the future', () => {
expect(
rangeNeedsExplicitYear([
new Date('2025-06-01T10:00:00'),
new Date('2025-12-25T10:00:00'),
]),
).toBe(true);
});

it('compares years in UTC when isUTC is set', () => {
jest.setSystemTime(new Date('2025-06-15T12:00:00Z'));
expect(
Comment thread
karl-power marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — The UTC tests for rangeNeedsExplicitYear and formatTimeRangeText still pass if isUTC is ignored

The start date 2024-12-31T23:00Z falls in 2024 in UTC and in every timezone west of UTC, so the result is true with or without isUTC. CI runs in UTC, and nothing in the jest config sets TZ, so CI can't tell the difference. Also, no round-trip test calls formatTimeRangeText(range, { isUTC: true }) and then parseTimeRangeInput(text, true), even though UTC is the path dateRangeToString takes for UTC users. Fix: pin a non-UTC TZ for this case, for example with a date-fns-tz/TZ override, using a date whose UTC year and local year differ (such as 2025-01-01T02:00Z under TZ=America/Los_Angeles with now = 2025-06-15Z). Then add { isUTC: true } ranges to the round-trip it.each.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

rangeNeedsExplicitYear(
[new Date('2024-12-31T23:00:00Z'), new Date('2025-01-01T01:00:00Z')],
true,
),
).toBe(true);
});
});

describe('formatTimeRangeText', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2026-06-15T12:00:00'));
Expand All @@ -172,19 +219,46 @@ describe('parseTimeRangeInput with year-qualified picker text', () => {
jest.useRealTimers();
});

it('omits the year for a past range in the current year', () => {
expect(
formatTimeRangeText([
new Date('2026-03-03T10:00:00'),
new Date('2026-03-05T10:00:00'),
]),
).toBe('Mar 3 10:00:00 - Mar 5 10:00:00');
});

it('includes the year when any date is outside the current year', () => {
expect(
formatTimeRangeText([
new Date('2025-12-30T10:00:00'),
new Date('2026-02-01T10:00:00'),
]),
).toBe('Dec 30 2025 10:00:00 - Feb 1 2026 10:00:00');
});

it('uses the requested clock', () => {
expect(
formatTimeRangeText(
[new Date('2024-03-03T13:05:09'), new Date('2024-03-05T01:00:00')],
{ clock: '12h' },
),
).toBe('Mar 3 2024 1:05:09 PM - Mar 5 2024 1:00:00 AM');
});

it.each(['12h', '24h'] as const)(
Comment thread
karl-power marked this conversation as resolved.
Resolved
'round-trips past, boundary and future ranges with a %s clock',
'round-trips past, boundary, current-year and future ranges with a %s clock',
clock => {
const ranges: [Date, Date][] = [
[new Date('2024-03-03T13:05:09'), new Date('2024-03-05T01:00:00')],
[new Date('2025-12-30T10:00:00'), new Date('2026-02-01T10:00:00')],
[new Date('2026-03-03T10:00:00'), new Date('2026-03-05T10:00:00')],
[new Date('2026-08-01T10:00:00'), new Date('2026-09-01T10:00:00')],
];
for (const [start, end] of ranges) {
const text = [start, end]
.map(d => formatDate(d, { format: 'withYear', clock }))
.join(' - ');
expect(parseTimeRangeInput(text)).toEqual([start, end]);
for (const range of ranges) {
expect(
parseTimeRangeInput(formatTimeRangeText(range, { clock })),
).toEqual(range);
}
},
);
Expand Down
37 changes: 33 additions & 4 deletions packages/app/src/components/TimePicker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import * as chrono from 'chrono-node';
import ms from 'ms';
import { formatDate } from '@hyperdx/common-utils/dist/core/utils';

function currentDate(): Date {
// eslint-disable-next-line no-restricted-syntax
return new Date();
}

function normalizeParsedDate(parsed?: chrono.ParsedComponents): Date | null {
if (!parsed) {
return null;
}

// eslint-disable-next-line no-restricted-syntax
const now = new Date();
const now = currentDate();
const parsedDate = parsed.date();

// If all of the time components have been inferred, set the time components of now
Expand Down Expand Up @@ -59,8 +64,7 @@ export function parseTimeRangeInput(
? parsedTimeResults[0]
: parsedTimeResults[1];
const start = normalizeParsedDate(parsedTimeResult.start);
// eslint-disable-next-line no-restricted-syntax
const end = normalizeParsedDate(parsedTimeResult.end) || new Date();
const end = normalizeParsedDate(parsedTimeResult.end) || currentDate();
if (end && start && end < start) {
// For date range strings that omit years, the chrono parser will infer the year
// using the current year. This can cause the start date to be in the future, and
Expand All @@ -72,6 +76,31 @@ export function parseTimeRangeInput(
}
}

// Range text without a year is re-parsed with an inferred year (the current
// one, or the previous one for dates >1 day ahead). Only past dates in the
// current year survive that, so every other range needs an explicit year.
export function rangeNeedsExplicitYear(
range: readonly [Date, Date],
isUTC: boolean = false,
): boolean {
const now = currentDate();
const getYear = (d: Date) => (isUTC ? d.getUTCFullYear() : d.getFullYear());
return range.some(d => getYear(d) !== getYear(now) || d > now);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — Some past current-year ranges drop the year but don't round-trip near Dec 31

rangeNeedsExplicitYear assumes a yearless date always comes back in the current year. It doesn't. chrono 2.x (findYearClosestToRef) picks whichever year puts the date nearest to now, and normalizeParsedDate then clamps anything up to 1 day ahead to now. Example: on Dec 31 2026 at 20:00, the range Jan 1 2026 10:00 – Jan 2 2026 10:00 is formatted as Jan 1 10:00:00 - Jan 2 10:00:00. On re-parse, the start becomes Jan 1 2027, which is within a day, so it is clamped to now. The end becomes Jan 2 2027 and is shifted back to 2026. After the swap, tq and the picker seed read Jan 2 – Dec 31 2026. Suggested fix: format without the year, run the text through parseTimeRangeInput, and fall back to withYear if the dates differ. That ties the rule to the parser instead of a hand-written copy of its rules. Also add a round-trip test with the system time set to Dec 31. Today every test fixes the clock at June 15.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

}

// Formats a range as text that parseTimeRangeInput maps back to the same dates.
// The year is only included when it's needed to do so, to keep the text short.
export function formatTimeRangeText(
range: readonly [Date, Date],
{
isUTC = false,
clock = '24h',
}: { isUTC?: boolean; clock?: '12h' | '24h' } = {},
): string {
const format = rangeNeedsExplicitYear(range, isUTC) ? 'withYear' : 'normal';
return range.map(d => formatDate(d, { isUTC, format, clock })).join(' - ');
}

// Duration of a time range input in seconds, or null if it doesn't parse.
export function timeRangeInputToSeconds(
str: string,
Expand Down
21 changes: 6 additions & 15 deletions packages/app/src/timeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,20 @@ import {
useQueryState,
useQueryStates,
} from 'nuqs';
import { formatDate } from '@hyperdx/common-utils/dist/core/utils';
import { DateRange } from '@hyperdx/common-utils/dist/types';

import { parseTimeRangeInput } from './components/TimePicker/utils';
import {
formatTimeRangeText,
parseTimeRangeInput,
} from './components/TimePicker/utils';
import { useUserPreferences } from './useUserPreferences';
import { usePrevious } from './utils';

const LIVE_TAIL_TIME_QUERY = 'Live Tail';
const LIVE_TAIL_REFRESH_INTERVAL_MS = 1000;

// The text is re-parsed into dates (picker input, `tq` URL param). Without a
// year, chrono infers one and drops anything older than the current year.
export const dateRangeToString = (range: [Date, Date], isUTC: boolean) => {
return `${formatDate(range[0], {
isUTC,
format: 'withYear',
clock: '24h',
})} - ${formatDate(range[1], {
isUTC,
format: 'withYear',
clock: '24h',
})}`;
};
export const dateRangeToString = (range: [Date, Date], isUTC: boolean) =>
formatTimeRangeText(range, { isUTC, clock: '24h' });

function isInputTimeQueryLive(inputTimeQuery: string) {
return inputTimeQuery === '' || inputTimeQuery.includes(LIVE_TAIL_TIME_QUERY);
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/ratchet-baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"app": {
"as-any": 213,
"ts-ignore": 0,
"eslint-disable": 143,
"eslint-disable": 142,
"@source": 0
},
"cli": {
Expand Down
Loading