Repository navigation
fix(app): only show the year in time picker range text when needed #3362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; | ||
|
|
||
|
|
@@ -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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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')); | ||
|
|
@@ -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)( | ||
|
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); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| 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 | ||
|
|
@@ -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 | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.