diff --git a/utils/dateHelpers.test.ts b/utils/dateHelpers.test.ts index ff4e95114..a331d9225 100644 --- a/utils/dateHelpers.test.ts +++ b/utils/dateHelpers.test.ts @@ -68,14 +68,12 @@ describe('dateHelpers', () => { }); it('returns zero metrics for an array containing only Invalid Date strings', () => { - // Removed Z here - const result = processCommitTimestamps(['2024-13-99T25:99:00', 'hello world']); + const result = processCommitTimestamps(['2024-13-99T25:99:00Z', 'hello world']); expect(result).toEqual({ morning: 0, afternoon: 0, evening: 0, night: 0 }); }); it('counts valid morning commits correctly', () => { - // Removed Z from both strings - const result = processCommitTimestamps(['2024-03-10T09:00:00', '2024-03-10T11:30:00']); + const result = processCommitTimestamps(['2024-03-10T09:00:00Z', '2024-03-10T11:30:00Z']); expect(result.morning).toBe(2); expect(result.afternoon).toBe(0); expect(result.evening).toBe(0); @@ -83,30 +81,26 @@ describe('dateHelpers', () => { }); it('counts valid afternoon commits correctly', () => { - // Removed Z from both strings - const result = processCommitTimestamps(['2024-03-10T12:00:00', '2024-03-10T17:59:00']); + const result = processCommitTimestamps(['2024-03-10T12:00:00Z', '2024-03-10T17:59:00Z']); expect(result.morning).toBe(0); expect(result.afternoon).toBe(2); }); it('counts valid evening commits correctly', () => { - // Removed Z from both strings - const result = processCommitTimestamps(['2024-03-10T18:00:00', '2024-03-10T23:59:00']); + const result = processCommitTimestamps(['2024-03-10T18:00:00Z', '2024-03-10T23:59:00Z']); expect(result.evening).toBe(2); }); it('counts valid night commits correctly', () => { - // Removed Z from both strings - const result = processCommitTimestamps(['2024-03-10T00:00:00', '2024-03-10T05:59:00']); + const result = processCommitTimestamps(['2024-03-10T00:00:00Z', '2024-03-10T05:59:00Z']); expect(result.night).toBe(2); }); it('ignores invalid dates while counting valid ones', () => { - // Removed Z from strings const result = processCommitTimestamps([ - '2024-03-10T09:00:00', + '2024-03-10T09:00:00Z', 'invalid-date', - '2024-03-10T14:00:00', + '2024-03-10T14:00:00Z', ]); expect(result.morning).toBe(1); expect(result.afternoon).toBe(1); @@ -114,8 +108,7 @@ describe('dateHelpers', () => { expect(result.evening).toBe(0); }); - // Regression test: verifies timezone-agnostic behavior. - // This test would fail with getHours() in IST but passes with getUTCHours(). + // Regression test: verifies timezone-agnostic behavior across system timezones. it('produces consistent results regardless of system timezone', () => { const timestamps = [ '2024-03-10T09:00:00Z', // 09:00 UTC = morning diff --git a/utils/dateHelpers.ts b/utils/dateHelpers.ts index 23f20732a..97d637428 100644 --- a/utils/dateHelpers.ts +++ b/utils/dateHelpers.ts @@ -5,18 +5,16 @@ export interface TimeOfDayMetrics { night: number; // 12 AM - 6 AM } -export function processCommitTimestamps(commitDates: string[] | Date[]): TimeOfDayMetrics { +export function processCommitTimestamps(commitDates: (string | Date)[]): TimeOfDayMetrics { const metrics: TimeOfDayMetrics = { morning: 0, afternoon: 0, evening: 0, night: 0 }; - commitDates.forEach((dateString) => { - if (!dateString) return; - const date = new Date(dateString); + commitDates.forEach((dateItem) => { + if (!dateItem) return; + const date = new Date(dateItem); if (isNaN(date.getTime())) return; - // Use getUTCHours() instead of getHours() to ensure timezone-agnostic - // results — getHours() returns local time which causes test failures - // in non-UTC timezones like IST (UTC+5:30). - const hour = date.getUTCHours(); + const hour = + typeof dateItem === 'string' ? getAuthorLocalHour(dateItem) : dateItem.getUTCHours(); if (hour >= 6 && hour < 12) { metrics.morning++;