Share one date formatter, and stop describing files that are gone

Two components each carried the same four lines turning a date into an ISO
string from local parts. They now share one helper, deliberately separate
from the one that formats in UTC: substituting one for the other shifts the
day by one anywhere east of Greenwich, Sweden included, so a test pins the
difference rather than a comment asking nicely.

Creating a period ran its own copy of the sync-and-report sequence purely so
it could name a wish the lookup could not yet see; it now passes that wish to
the ordinary path instead.

Two comments still explained their relationship to components deleted some
time ago, which would send the next reader looking for files that are not
there, and some styling outlived the markup it described.
This commit is contained in:
2026-09-18 01:12:58 +02:00
parent 9ac67933d4
commit f2b3920cbc
8 changed files with 112 additions and 99 deletions
+31 -1
View File
@@ -1,7 +1,8 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect, afterEach, vi } from 'vitest'
import {
formatDateForDisplay,
formatDateForInput,
formatDateLocalISO,
parseInputDate,
isValidDate,
getDayName,
@@ -81,6 +82,35 @@ describe('dates.ts utilities', () => {
})
})
describe('formatDateLocalISO', () => {
afterEach(() => {
vi.unstubAllEnvs()
})
it('formats a Date built from local year/month/day components', () => {
const date = new Date(2026, 5, 15) // local June 15, 2026
expect(formatDateLocalISO(date)).toBe('2026-06-15')
})
it('pads single-digit month and day', () => {
const date = new Date(2026, 0, 5) // local January 5, 2026
expect(formatDateLocalISO(date)).toBe('2026-01-05')
})
it('reads the local calendar date, not the UTC one — this is the whole point of the function', () => {
// Pacific/Kiritimati is UTC+14. A moment at 23:30 UTC on Jan 1 is
// already local Jan 2 there. `formatDateForInput` (UTC getters) must
// report Jan 1; `formatDateLocalISO` (local getters) must report
// Jan 2. If this ever collapses to the same value, someone has
// swapped in the UTC variant and reintroduced the off-by-one-day bug
// this function exists to avoid.
vi.stubEnv('TZ', 'Pacific/Kiritimati')
const date = new Date(Date.UTC(2026, 0, 1, 23, 30))
expect(formatDateForInput(date)).toBe('2026-01-01')
expect(formatDateLocalISO(date)).toBe('2026-01-02')
})
})
describe('parseInputDate', () => {
it('parses ISO date string to Date object', () => {
const result = parseInputDate('2026-06-15')