UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

43 lines (41 loc) • 1.9 kB
import { Temporal } from "../../internal/temporal.js"; import { imfFixdate } from "../../internal/englishCalendarNames.js"; import { isValidUtc } from "../validate/isValidUtc.js"; // src/utc/format/formatHttpDate.ts /** * Format a UTC ISO 8601 datetime string as an HTTP header date — the RFC 9110 * IMF-fixdate that headers like `Last-Modified`/`Date`/`Expires` require. * * - **Fixed grammar, not a display format.** RFC 9110 mandates English * weekday/month abbreviations and a literal `GMT` suffix regardless of * caller locale — there is no locale-appropriate alternative ordering to * lose. * - Always `GMT`; HTTP-date carries no offset variation to preserve. * - Fractional seconds in `value` are truncated — IMF-fixdate has no * sub-second field. * - Day, hour, minute, and second are always zero-padded to 2 digits; year * is zero-padded to 4 digits. * - Returns `""` for a year outside 0000–9999: IMF-fixdate's `year = 4DIGIT`. * - **Compatibility:** before 1.16.0 such years gave strings outside the * grammar (`"00-1"`, `"275760"`). For the value itself use * `Temporal.Instant#toString()`. * * @param value UTC ISO 8601 datetime string (e.g. "2024-03-15T14:30:00Z") * @returns RFC 9110 IMF-fixdate string, or "" on invalid input or a year outside 0000–9999 * * @example formatHttpDate("2024-03-15T14:30:00Z") // "Fri, 15 Mar 2024 14:30:00 GMT" * @example formatHttpDate("2024-03-15T14:30:00.500Z") // "Fri, 15 Mar 2024 14:30:00 GMT" * @example formatHttpDate("-000001-06-15T12:00:00Z") // "" * @example Temporal.Instant.from("-000001-06-15T12:00:00Z").toString() // "-000001-06-15T12:00:00Z" * @example formatHttpDate("invalid") // "" */ function formatHttpDate(value) { if (!isValidUtc(value)) return ""; try { const zdt = Temporal.Instant.from(value).toZonedDateTimeISO("UTC"); return imfFixdate(zdt); } catch { return ""; } } export { formatHttpDate };