Skip to content

Commit f29230c

Browse files
authored
fix(web): handle buckets before year 1000 (#21832)
1 parent 03af60e commit f29230c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

web/src/lib/utils/timeline-util.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { locale } from '$lib/stores/preferences.store';
22
import { parseUtcDate } from '$lib/utils/date-time';
3-
import { formatGroupTitle } from '$lib/utils/timeline-util';
3+
import { formatGroupTitle, toISOYearMonthUTC } from '$lib/utils/timeline-util';
44
import { DateTime } from 'luxon';
55

66
describe('formatGroupTitle', () => {
@@ -77,3 +77,13 @@ describe('formatGroupTitle', () => {
7777
expect(formatGroupTitle(date)).toBe('Invalid DateTime');
7878
});
7979
});
80+
81+
describe('toISOYearMonthUTC', () => {
82+
it('should prefix year with 0s', () => {
83+
expect(toISOYearMonthUTC({ year: 28, month: 1 })).toBe('0028-01-01T00:00:00.000Z');
84+
});
85+
86+
it('should prefix month with 0s', () => {
87+
expect(toISOYearMonthUTC({ year: 2025, month: 1 })).toBe('2025-01-01T00:00:00.000Z');
88+
});
89+
});

web/src/lib/utils/timeline-util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ export const fromTimelinePlainYearMonth = (timelineYearMonth: TimelineYearMonth)
9494
{ zone: 'local', locale: get(locale) },
9595
) as DateTime<true>;
9696

97-
export const toISOYearMonthUTC = ({ year, month }: TimelineYearMonth): string =>
98-
`${year}-${month.toString().padStart(2, '0')}-01T00:00:00.000Z`;
97+
export const toISOYearMonthUTC = ({ year, month }: TimelineYearMonth): string => {
98+
const yearFull = `${year}`.padStart(4, '0');
99+
const monthFull = `${month}`.padStart(2, '0');
100+
return `${yearFull}-${monthFull}-01T00:00:00.000Z`;
101+
};
99102

100103
export function formatMonthGroupTitle(_date: DateTime): string {
101104
if (!_date.isValid) {

0 commit comments

Comments
 (0)