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.

57 lines (55 loc) • 3.01 kB
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js"; import { englishDateTimeStem } from "../../internal/englishCalendarNames.js"; // src/zoned/format/formatRfc2822.ts const NANOSECONDS_PER_MINUTE = 6e10; /** * Format a zoned ISO 8601 datetime string as an RFC 5322 (RFC 2822) date-time * — the fixed grammar email `Date:` headers require. * * - **Fixed grammar, not a display format.** RFC 5322 mandates English * weekday/month abbreviations and a numeric `+HHMM`/`-HHMM` offset * regardless of caller locale — there is no locale-appropriate alternative * ordering to lose, so this does not conflict with GMT's exclusion of a * general token formatter. * - Uses the input's own UTC offset at that instant (DST-aware); never emits * an obsolete named zone like "GMT" or "EST" on output, only numeric. * - Day, hour, minute, and second are always zero-padded to 2 digits; year * is zero-padded to 4 digits. * - Fractional seconds are truncated: `second = 2DIGIT` has no fraction. * - Returns `""` when the offset is not a whole number of minutes (local mean * time, e.g. Africa/Monrovia −00:44:30 before 1972): `zone` is * `("+" / "-") 4DIGIT`, meaning ±(hh·60 + mm) minutes. * - Returns `""` for a negative year: `year = 4*DIGIT` has no sign. Years of * five or more digits are written in full. * - **Compatibility:** before 1.16.0 these inputs gave strings outside the * grammar (`"-0044.5"`, `"00-1"`). For the value itself use * `Temporal.ZonedDateTime#toString()`. * * @param value zoned ISO 8601 datetime string (e.g. "2024-03-15T14:30:00+05:30[Asia/Kolkata]") * @returns RFC 5322 date-time string, or "" on invalid input, a sub-minute offset or a negative year * * @example formatRfc2822("2024-03-15T14:30:00+05:30[Asia/Kolkata]") // "Fri, 15 Mar 2024 14:30:00 +0530" * @example formatRfc2822("2024-01-05T09:00:00+00:00[UTC]") // "Fri, 05 Jan 2024 09:00:00 +0000" * @example formatRfc2822("2024-03-15T14:30:00.999+05:30[Asia/Kolkata]") // "Fri, 15 Mar 2024 14:30:00 +0530" * @example formatRfc2822("1969-12-31T23:15:30-00:45[Africa/Monrovia]") // "" (offset −00:44:30) * @example formatRfc2822("-000001-06-15T12:00:00+00:00[UTC]") // "" * @example Temporal.ZonedDateTime.from("-000001-06-15T12:00:00+00:00[UTC]").toString() // "-000001-06-15T12:00:00+00:00[UTC]" * @example formatRfc2822("invalid") // "" */ function formatRfc2822(value) { const zdt = zonedDateTimeOf(value); if (zdt === null) return ""; try { if (zdt.offsetNanoseconds % NANOSECONDS_PER_MINUTE !== 0) return ""; if (zdt.year < 0) return ""; const totalOffsetMinutes = zdt.offsetNanoseconds / NANOSECONDS_PER_MINUTE; const sign = totalOffsetMinutes < 0 ? "-" : "+"; const absMinutes = Math.abs(totalOffsetMinutes); const offsetHH = String(Math.trunc(absMinutes / 60)).padStart(2, "0"); const offsetMM = String(absMinutes % 60).padStart(2, "0"); return `${englishDateTimeStem(zdt)} ${sign}${offsetHH}${offsetMM}`; } catch { return ""; } } export { formatRfc2822 };