Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(studio): preserve calendar date ranges across daylight saving (#6419)

David Castro6227af5102b3
diff --git a/web-studio/src/routes/home/-lib/format.test.ts b/web-studio/src/routes/home/-lib/format.test.ts
new file mode 100644
index 000000000..eb1bb07a7
--- /dev/null
+++ b/web-studio/src/routes/home/-lib/format.test.ts
@@ -0,0 +1,61 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { getLastDaysRange } from './format'
+
+afterEach(() => {
+ vi.useRealTimers()
+ vi.unstubAllEnvs()
+})
+
+describe('getLastDaysRange', () => {
+ it.each([
+ ['2026-03-09T04:30:00Z', 'America/New_York', '2026-03-09', '2026-02-24'],
+ ['2026-11-03T04:30:00Z', 'America/New_York', '2026-11-02', '2026-10-20'],
+ ['2026-03-30T22:30:00Z', 'Europe/Berlin', '2026-03-31', '2026-03-18'],
+ ['2026-10-26T22:30:00Z', 'Europe/Berlin', '2026-10-26', '2026-10-13'],
+ ['2026-10-05T13:30:00Z', 'Australia/Sydney', '2026-10-06', '2026-09-23'],
+ ['2026-04-06T13:30:00Z', 'Australia/Sydney', '2026-04-06', '2026-03-24'],
+ ['2026-10-05T13:15:00Z', 'Australia/Lord_Howe', '2026-10-06', '2026-09-23'],
+ ['2026-04-06T13:15:00Z', 'Australia/Lord_Howe', '2026-04-06', '2026-03-24'],
+ ['2026-06-15T16:00:00Z', 'America/New_York', '2026-06-15', '2026-06-02'],
+ ['2026-03-09T04:30:00Z', 'Asia/Shanghai', '2026-03-09', '2026-02-24'],
+ ['2026-03-09T04:30:00Z', 'UTC', '2026-03-09', '2026-02-24'],
+ ['2026-01-03T00:30:00Z', 'UTC', '2026-01-03', '2025-12-21'],
+ ['2024-03-01T00:30:00Z', 'UTC', '2024-03-01', '2024-02-17'],
+ ])(
+ 'includes 14 calendar dates at %s in %s',
+ (now, timeZone, endDate, startDate) => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date(now))
+
+ expect(getLastDaysRange(14, timeZone)).toEqual({ endDate, startDate })
+ },
+ )
+
+ it.each([
+ [1, '2026-03-09'],
+ [365, '2025-03-10'],
+ ])(
+ 'includes %i calendar dates for the dashboard ranges',
+ (days, startDate) => {
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date('2026-03-09T04:30:00Z'))
+
+ expect(getLastDaysRange(days, 'America/New_York')).toEqual({
+ endDate: '2026-03-09',
+ startDate,
+ })
+ },
+ )
+
+ it('uses the viewer timezone when no timezone is supplied', () => {
+ vi.stubEnv('TZ', 'America/New_York')
+ vi.useFakeTimers()
+ vi.setSystemTime(new Date('2026-03-09T04:30:00Z'))
+
+ expect(getLastDaysRange(14)).toEqual({
+ endDate: '2026-03-09',
+ startDate: '2026-02-24',
+ })
+ })
+})
diff --git a/web-studio/src/routes/home/-lib/format.ts b/web-studio/src/routes/home/-lib/format.ts
index ad1ab7bd9..bef8ab320 100644
--- a/web-studio/src/routes/home/-lib/format.ts
+++ b/web-studio/src/routes/home/-lib/format.ts
@@ -60,15 +60,13 @@ export function getLastDaysRange(
startDate: string
} {
const tz = timeZone ?? getViewerTimezone()
- const now = new Date()
- // Walk back `days - 1` UTC midnights from now and convert each instant in
- // the viewer's tz; using millisecond arithmetic keeps the math safe across
- // DST transitions because we never inspect a local hour.
- const dayMs = 24 * 60 * 60 * 1000
- const earlier = new Date(now.getTime() - (days - 1) * dayMs)
+ const endDate = formatDateInTimezone(new Date(), tz)
+ // Use UTC only for calendar arithmetic on the viewer's date, not elapsed time.
+ const earlier = new Date(`${endDate}T00:00:00Z`)
+ earlier.setUTCDate(earlier.getUTCDate() - (days - 1))
return {
- endDate: formatDateInTimezone(now, tz),
- startDate: formatDateInTimezone(earlier, tz),
+ endDate,
+ startDate: earlier.toISOString().slice(0, 10),
}
}