From f29230c8a69e04c30f8f449c58009e79da9136a1 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Thu, 11 Sep 2025 15:36:16 -0400 Subject: [PATCH] fix(web): handle buckets before year 1000 (#21832) --- web/src/lib/utils/timeline-util.spec.ts | 12 +++++++++++- web/src/lib/utils/timeline-util.ts | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/web/src/lib/utils/timeline-util.spec.ts b/web/src/lib/utils/timeline-util.spec.ts index c77aefc0b4..bc05b78ad4 100644 --- a/web/src/lib/utils/timeline-util.spec.ts +++ b/web/src/lib/utils/timeline-util.spec.ts @@ -1,6 +1,6 @@ import { locale } from '$lib/stores/preferences.store'; import { parseUtcDate } from '$lib/utils/date-time'; -import { formatGroupTitle } from '$lib/utils/timeline-util'; +import { formatGroupTitle, toISOYearMonthUTC } from '$lib/utils/timeline-util'; import { DateTime } from 'luxon'; describe('formatGroupTitle', () => { @@ -77,3 +77,13 @@ describe('formatGroupTitle', () => { expect(formatGroupTitle(date)).toBe('Invalid DateTime'); }); }); + +describe('toISOYearMonthUTC', () => { + it('should prefix year with 0s', () => { + expect(toISOYearMonthUTC({ year: 28, month: 1 })).toBe('0028-01-01T00:00:00.000Z'); + }); + + it('should prefix month with 0s', () => { + expect(toISOYearMonthUTC({ year: 2025, month: 1 })).toBe('2025-01-01T00:00:00.000Z'); + }); +}); diff --git a/web/src/lib/utils/timeline-util.ts b/web/src/lib/utils/timeline-util.ts index 6a0f12c20e..9cf4428da6 100644 --- a/web/src/lib/utils/timeline-util.ts +++ b/web/src/lib/utils/timeline-util.ts @@ -94,8 +94,11 @@ export const fromTimelinePlainYearMonth = (timelineYearMonth: TimelineYearMonth) { zone: 'local', locale: get(locale) }, ) as DateTime; -export const toISOYearMonthUTC = ({ year, month }: TimelineYearMonth): string => - `${year}-${month.toString().padStart(2, '0')}-01T00:00:00.000Z`; +export const toISOYearMonthUTC = ({ year, month }: TimelineYearMonth): string => { + const yearFull = `${year}`.padStart(4, '0'); + const monthFull = `${month}`.padStart(2, '0'); + return `${yearFull}-${monthFull}-01T00:00:00.000Z`; +}; export function formatMonthGroupTitle(_date: DateTime): string { if (!_date.isValid) {