From 63a7cbdde4ad7722a2572d7d805e1698104f83ad Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Mon, 18 May 2026 20:15:23 -0400 Subject: [PATCH] feat(provenance): locale-independent formatPostDate util Co-Authored-By: Claude Opus 4.7 (1M context) --- frontend/src/utils/date.js | 15 +++++++++++++++ frontend/test/date.spec.js | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 frontend/src/utils/date.js create mode 100644 frontend/test/date.spec.js diff --git a/frontend/src/utils/date.js b/frontend/src/utils/date.js new file mode 100644 index 0000000..f0ade54 --- /dev/null +++ b/frontend/src/utils/date.js @@ -0,0 +1,15 @@ +// Locale-independent post-date formatting. Uses a fixed month table and +// UTC getters so the rendered string never varies by browser/CI locale +// or timezone (toLocaleDateString would make tests flaky). + +const MONTHS = [ + 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' +] + +export function formatPostDate(iso) { + if (!iso) return null + const d = new Date(iso) + if (isNaN(d.getTime())) return null + return `${MONTHS[d.getUTCMonth()]} ${d.getUTCDate()}, ${d.getUTCFullYear()}` +} diff --git a/frontend/test/date.spec.js b/frontend/test/date.spec.js new file mode 100644 index 0000000..60415b5 --- /dev/null +++ b/frontend/test/date.spec.js @@ -0,0 +1,15 @@ +import { describe, it, expect } from 'vitest' +import { formatPostDate } from '../src/utils/date.js' + +describe('formatPostDate', () => { + it('formats an ISO datetime as "Mon D, YYYY" in UTC', () => { + expect(formatPostDate('2023-08-01T00:00:00+00:00')).toBe('Aug 1, 2023') + expect(formatPostDate('2023-12-25T23:59:59Z')).toBe('Dec 25, 2023') + }) + + it('returns null for null/empty/invalid', () => { + expect(formatPostDate(null)).toBe(null) + expect(formatPostDate('')).toBe(null) + expect(formatPostDate('not-a-date')).toBe(null) + }) +})