@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.
54 lines (53 loc) • 2.05 kB
JavaScript
// src/internal/englishCalendarNames.ts
const ENGLISH_WEEKDAY_NAMES = [
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun"
];
const ENGLISH_MONTH_NAMES = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
/**
* The `Mon, 15 Jul 2024 12:00:00` stem RFC 5322 date-time and RFC 9110 IMF-fixdate share: English
* day and month names, zero-padded fields, read from the date-time's own wall clock.
*
* @param zdt the date-time; the caller has already rejected a year its grammar cannot hold: a
* negative one for RFC 5322 (`year = 4*DIGIT`, so five digits are written in full), and any
* outside 0000–9999 for RFC 9110 (`year = 4DIGIT`)
* @returns the stem, without a zone or `GMT` suffix
* @example englishDateTimeStem(Temporal.ZonedDateTime.from("2024-07-15T12:00:05+00:00[UTC]")) // "Mon, 15 Jul 2024 12:00:05"
* @example englishDateTimeStem(Temporal.ZonedDateTime.from("0099-01-03T04:05:06+00:00[UTC]")) // "Sat, 03 Jan 0099 04:05:06"
*/
function englishDateTimeStem(zdt) {
return `${ENGLISH_WEEKDAY_NAMES[zdt.dayOfWeek - 1]}, ${String(zdt.day).padStart(2, "0")} ${ENGLISH_MONTH_NAMES[zdt.month - 1]} ${String(zdt.year).padStart(4, "0")} ${String(zdt.hour).padStart(2, "0")}:${String(zdt.minute).padStart(2, "0")}:${String(zdt.second).padStart(2, "0")}`;
}
/**
* The RFC 9110 §5.6.7 IMF-fixdate for a UTC value, such as `Fri, 15 Mar 2024 14:30:00 GMT`.
*
* The caller passes a value already on the UTC clock; sub-second digits are not written.
*
* @param utc a zoned date-time in UTC
* @returns the HTTP-date, or `""` for a year outside 0000–9999 (`year = 4DIGIT`)
* @example imfFixdate(Temporal.ZonedDateTime.from("2024-03-15T14:30:00+00:00[UTC]")) // "Fri, 15 Mar 2024 14:30:00 GMT"
* @example imfFixdate(Temporal.ZonedDateTime.from("-000001-06-15T12:00:00+00:00[UTC]")) // ""
*/
function imfFixdate(utc) {
if (utc.year < 0 || utc.year > 9999) return "";
return `${englishDateTimeStem(utc)} GMT`;
}
export { ENGLISH_MONTH_NAMES, ENGLISH_WEEKDAY_NAMES, englishDateTimeStem, imfFixdate };