@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.
118 lines (116 loc) • 3.62 kB
JavaScript
import { ENGLISH_MONTH_NAMES, ENGLISH_WEEKDAY_NAMES } from "./englishCalendarNames.js";
// src/internal/rfc5322DateTime.ts
const NAMED_ZONE_OFFSETS = {
UT: "+00:00",
GMT: "+00:00",
EST: "-05:00",
EDT: "-04:00",
CST: "-06:00",
CDT: "-05:00",
MST: "-07:00",
MDT: "-06:00",
PST: "-08:00",
PDT: "-07:00"
};
const COMMENT = "\0";
const C = "(?:[ \\t\\u0000]|\\r\\n[ \\t])*";
const RECEIVER_DATE_TIME = new RegExp(`^${C}(?:([A-Za-z]{3})${C},${C})?(\\d{1,2})${C}([A-Za-z]{3})${C}(\\d{2,})${C}(\\d{2})${C}:${C}(\\d{2})${C}(?::${C}(\\d{2})${C})?(?:(?<=[ \\t])([+-]\\d{4})|([A-Za-z]+))${C}$`);
function foldComments(value) {
if (hasUnquotedNul(value)) return null;
let folded = "";
let index = 0;
while (index < value.length) {
const char = value[index];
if (char === ")") return null;
if (char === "(") {
index = commentEnd(value, index);
if (index < 0) return null;
folded += COMMENT;
} else {
folded += char;
index += 1;
}
}
return folded;
}
function commentEnd(value, start) {
let depth = 0;
for (let index = start; index < value.length; index++) {
const char = value[index];
if (char === "\\") index += 1;
else if (char === "(") depth += 1;
else if (char === ")") {
depth -= 1;
if (depth === 0) return index + 1;
}
}
return -1;
}
function hasUnquotedNul(value) {
let backslashRun = 0;
for (const char of value) {
if (char === COMMENT && backslashRun % 2 === 0) return true;
backslashRun = char === "\\" ? backslashRun + 1 : 0;
}
return false;
}
function interpretYear(digits) {
const year = Number(digits);
if (digits.length === 2) return year < 50 ? year + 2e3 : year + 1900;
if (digits.length === 3) return year + 1900;
return year;
}
function numericZoneOffset(zone) {
if (Number(zone.slice(3)) > 59) return null;
return `${zone.slice(0, 3)}:${zone.slice(3)}`;
}
function alphabeticZoneOffset(zone) {
const upper = zone.toUpperCase();
const named = NAMED_ZONE_OFFSETS[upper];
if (named !== void 0) return named;
if (upper === "J") return null;
return "+00:00";
}
function indexOfName(names, name) {
const wanted = name.toLowerCase();
return names.findIndex((candidate) => candidate.toLowerCase() === wanted);
}
/**
* Read the fields of an RFC 5322 `date-time` as a conformant receiver must:
* the §3.3 syntax plus the §4 obsolete syntax (CFWS between tokens, 2- and
* 3-digit years, alphabetic zones), with case-insensitive names.
*
* - Shape and name checks only: the calendar date, the time of day and the
* day of week are left to the caller (`Temporal.PlainDateTime.from` with
* `overflow: "reject"`).
*
* @param value candidate RFC 5322 date-time
* @returns the fields, or `null` when `value` is not a `date-time`
*/
function readRfc5322DateTime(value) {
const folded = foldComments(value);
if (folded === null) return null;
const match = RECEIVER_DATE_TIME.exec(folded);
if (match === null) return null;
const [, dayName, day, monthName, year, hour, minute, second, numericZone, alphabeticZone] = match;
const month = indexOfName(ENGLISH_MONTH_NAMES, monthName) + 1;
if (month === 0) return null;
let dayOfWeek;
if (dayName !== void 0) {
dayOfWeek = indexOfName(ENGLISH_WEEKDAY_NAMES, dayName) + 1;
if (dayOfWeek === 0) return null;
}
const offset = numericZone === void 0 ? alphabeticZoneOffset(alphabeticZone) : numericZoneOffset(numericZone);
if (offset === null) return null;
return {
dayOfWeek,
year: interpretYear(year),
month,
day: Number(day),
hour: Number(hour),
minute: Number(minute),
second: second === void 0 ? 0 : Number(second),
offset
};
}
export { readRfc5322DateTime };