/** Date helpers for the temporal pages (UTC, YYYY-MM-DD). Server-safe, no data. */ export const ISO_DAY = /^\d{4}-\d{2}-\d{2}$/; export function todayUtc(): string { return new Date().toISOString().slice(0, 10); } export function daysBefore(iso: string, days: number): string { const d = new Date(`${iso}T00:00:00Z`); d.setUTCDate(d.getUTCDate() - days); return d.toISOString().slice(0, 10); } export function monthsBefore(iso: string, months: number): string { const d = new Date(`${iso}T00:00:00Z`); d.setUTCMonth(d.getUTCMonth() - months); return d.toISOString().slice(0, 10); } export function firstOfMonth(iso: string): string { return `${iso.slice(0, 7)}-01`; } export function firstOfQuarter(iso: string): string { const m = Number(iso.slice(5, 7)); const q = Math.floor((m - 1) / 3) * 3 + 1; return `${iso.slice(0, 4)}-${String(q).padStart(2, '0')}-01`; } export function validDay(v: string | undefined | null): string | null { if (!v || !ISO_DAY.test(v)) return null; const d = new Date(`${v}T00:00:00Z`); return Number.isNaN(d.getTime()) ? null : v; }