Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(metrics): make minute/second date ranges resolve — and roll from now (#68081)

Daniel Viscac02d422c7ee7
diff --git a/frontend/src/lib/utils/dateFilters.test.ts b/frontend/src/lib/utils/dateFilters.test.ts
index 57ffa3d0d5b6..ce2f74d60672 100644
--- a/frontend/src/lib/utils/dateFilters.test.ts
+++ b/frontend/src/lib/utils/dateFilters.test.ts
@@ -212,6 +212,16 @@ describe('dateFilters utils', () => {
expect(dateStringToDayJs('1999-12-31')?.utc(true).toISOString()).toEqual('1999-12-31T00:00:00.000Z')
})
+ it('anchors sub-day units at now, not start of day', () => {
+ // frozen: 2012-03-02T11:38:49.321Z. A "-30M" range must mean 30
+ // minutes ago — day-anchoring would yield yesterday 23:30 and the
+ // metrics/logs "last N minutes" pickers would show a ~24h window.
+ expect(dateStringToDayJs('-30M')?.toISOString()).toEqual('2012-03-02T11:08:49.321Z')
+ expect(dateStringToDayJs('-5M')?.toISOString()).toEqual('2012-03-02T11:33:49.321Z')
+ expect(dateStringToDayJs('-1h')?.toISOString()).toEqual('2012-03-02T10:38:49.321Z')
+ expect(dateStringToDayJs('-45s')?.toISOString()).toEqual('2012-03-02T11:38:04.321Z')
+ })
+
it('handles various units', () => {
expect(dateStringToDayJs('d')?.utc(true).toISOString()).toEqual('2012-03-02T00:00:00.000Z')
expect(dateStringToDayJs('m')?.utc(true).toISOString()).toEqual('2012-03-02T00:00:00.000Z')
diff --git a/frontend/src/lib/utils/dateFilters.ts b/frontend/src/lib/utils/dateFilters.ts
index 191eff6e52ee..1b6aa4e637dd 100644
--- a/frontend/src/lib/utils/dateFilters.ts
+++ b/frontend/src/lib/utils/dateFilters.ts
@@ -237,7 +237,7 @@ export type DateComponents = {
clip: 'Start' | 'End'
}
-export const isStringDateRegex = /^([-+]?)([0-9]*)([hdwmqy])(|Start|End)$/
+export const isStringDateRegex = /^([-+]?)([0-9]*)([hdwmqyMs])(|Start|End)$/
export function dateStringToComponents(date: string | null): DateComponents | null {
if (!date) {
@@ -306,7 +306,11 @@ export function dateStringToDayJs(date: string | null, timezone: string = 'UTC')
if (!dateComponents) {
return null
}
- const offset: dayjs.Dayjs = dayjs().tz(timezone).startOf('day')
+ // Calendar units anchor at the start of today; sub-day units are rolling
+ // windows from now ("-30M" must mean 30 minutes ago, not today 00:00 minus
+ // 30 minutes = yesterday 23:30) — matching the backend's relative_date_parse.
+ const isSubDay = ['hour', 'minute', 'second'].includes(dateComponents.unit)
+ const offset: dayjs.Dayjs = isSubDay ? dayjs().tz(timezone) : dayjs().tz(timezone).startOf('day')
const response = componentsToDayJs(dateComponents, offset, timezone)
return response
}