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.

58 lines (56 loc) • 3.66 kB
import { httpDateInstantOf } from "../../internal/httpDateFields.js"; // src/utc/parse/parseHttpDate.ts /** * Parse an HTTP header date — the RFC 9110 HTTP-date that headers like * `Last-Modified`/`Date`/`Expires` carry — into a UTC ISO 8601 datetime string. * * - **Decoding, not display.** Accepts English weekday/month names only, per * the fixed grammar (see `formatHttpDate`'s JSDoc). * - **All three HTTP-date formats**, as RFC 9110 §5.6.7 requires ("A * recipient that parses a timestamp value in an HTTP field MUST accept all * three"): IMF-fixdate (`Sun, 06 Nov 1994 08:49:37 GMT`), the obsolete * rfc850-date (`Sunday, 06-Nov-94 08:49:37 GMT`) and asctime-date * (`Sun Nov 6 08:49:37 1994`, read as UTC). * - HTTP-date is case-sensitive and fixed-width: exactly the spacing, digit * counts and the literal `GMT` of the grammar. No numeric offset is * accepted. * - **rfc850-date years read the clock.** Its two-digit year becomes the * latest year ending in those digits whose timestamp is not more than 50 * years after now (`nowInstant()`), per RFC 9110: "a timestamp * that appears to be more than 50 years in the future" is "the most recent * year in the past that had the same last two digits". The same string can * therefore parse differently decades apart; if the clock cannot be read * the result is `""`. * - **The day name must match the date, in all three formats.** RFC 9110 gives it * RFC 5322's semantics: "the day-of-week (if included) MUST be the day implied by * the date". A mismatch returns `""` — deliberately in asctime-date too, where the * day name is not separated by a comma and so is easy to overlook. `Mon Nov 6 * 08:49:37 1994` is `""`, exactly as `Mon, 06 Nov 1994 08:49:37 GMT` and * `Monday, 06-Nov-94 08:49:37 GMT` are: 6 November 1994 was a Sunday. A parser that * accepted the mismatch would have to pick which of the two fields to believe, and * there is no rule in RFC 9110 that says which. * - A `:60` leap second returns `""`, though RFC 9110 §5.6.7 allows it (its `time-of-day` * comment reads "00:00:00 - 23:59:60 (leap second)"): GMT refuses every second above 59. * - An impossible calendar date (`31 Feb`, `29 Feb 2023`) returns `""`: a parser never * invents a date, so fields are validated with `overflow: "reject"`, not clamped. * - For the IMF-fixdate shape alone, test the exported `httpDate` pattern. * - **Compatibility:** before 1.16.0 a day name that did not match the date was ignored. * Parse the same fields as an RFC 5322 date-time without the day name, then convert. * * @param value RFC 9110 HTTP-date string (e.g. "Fri, 15 Mar 2024 14:30:00 GMT") * @returns UTC ISO 8601 datetime string, or "" on invalid input * * @example parseHttpDate("Fri, 15 Mar 2024 14:30:00 GMT") // "2024-03-15T14:30:00Z" * @example parseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT") // "1994-11-06T08:49:37Z" (rfc850-date) * @example parseHttpDate("Sun Nov 6 08:49:37 1994") // "1994-11-06T08:49:37Z" (asctime-date) * @example parseHttpDate("Fri, 15 Mar 2024 14:30:00 -0400") // "" (not an HTTP-date) * @example parseHttpDate("Sat, 15 Mar 2024 14:30:00 GMT") // "" (15 Mar 2024 was a Friday) * @example parseHttpDate("Mon Nov 6 08:49:37 1994") // "" (asctime-date; 6 Nov 1994 was a Sunday) * @example convertZonedToUtc(parseRfc2822("15 Mar 2024 14:30:00 GMT")) // "2024-03-15T14:30:00Z" — the day name removed * @example parseHttpDate("Sat, 31 Feb 2024 14:30:00 GMT") // "" (impossible date) * @example parseHttpDate("not a date") // "" */ function parseHttpDate(value) { return httpDateInstantOf(value)?.toString() ?? ""; } export { parseHttpDate };