@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.
90 lines (88 loc) • 2.99 kB
JavaScript
import { Temporal } from "./temporal.js";
import { resolveRequiredLocale } from "./resolveLocale.js";
import { cachedFormatter } from "./formatterCache.js";
import { zonedDateTimeFrom } from "./zonedWallClock.js";
import { getLocaleFirstDayOfWeek } from "./getLocaleFirstDayOfWeek.js";
// src/internal/localeNames.ts
function listFor(locale, build) {
try {
const resolvedLocale = resolveRequiredLocale(locale);
return resolvedLocale === null ? [] : build(resolvedLocale);
} catch {
return [];
}
}
function widthOf(style) {
if (style === "long" || style === "short" || style === "narrow") return style;
throw new RangeError(`Invalid style: ${String(style)}`);
}
function monthNamesFor(locale, style = "long") {
return listFor(locale, (resolvedLocale) => {
const formatter = cachedFormatter("DateTimeFormat", resolvedLocale, {
month: widthOf(style),
calendar: "gregory",
timeZone: "UTC"
});
const names = [];
for (let month = 1; month <= 12; month++) {
const date = Temporal.PlainDate.from(`2024-${String(month).padStart(2, "0")}-15`);
names.push(formatter.format(date.toZonedDateTime("UTC").epochMilliseconds));
}
return names;
});
}
function eraNamesFor(locale, style = "long") {
return listFor(locale, (resolvedLocale) => {
const formatter = cachedFormatter("DateTimeFormat", resolvedLocale, {
calendar: "gregory",
era: widthOf(style),
timeZone: "UTC"
});
const eraAt = (year) => formatter.formatToParts(zonedDateTimeFrom({
year,
month: 1,
day: 1,
hour: 12,
timeZone: "UTC",
calendar: "gregory"
}).toInstant().epochMilliseconds).find((part) => part.type === "era")?.value ?? "";
return [eraAt(0), eraAt(1)];
});
}
function meridiemsFor(locale) {
return listFor(locale, (resolvedLocale) => {
const formatter = cachedFormatter("DateTimeFormat", resolvedLocale, {
timeZone: "UTC",
hour: "numeric",
hour12: true
});
const dayPeriodAt = (epochMilliseconds) => formatter.formatToParts(epochMilliseconds).find((part) => part.type === "dayPeriod")?.value ?? "";
return [dayPeriodAt(0), dayPeriodAt(432e5)];
});
}
function weekdayNamesFor(locale, style = "long") {
try {
const resolvedLocale = resolveRequiredLocale(locale);
if (resolvedLocale === null) return [];
const firstDay = getLocaleFirstDayOfWeek(resolvedLocale);
if (firstDay === null) return [];
try {
const resolved = widthOf(style);
const formatter = cachedFormatter("DateTimeFormat", resolvedLocale, {
weekday: resolved,
timeZone: "UTC"
});
const isoOrder = [];
for (let dayOfWeek = 1; dayOfWeek <= 7; dayOfWeek++) {
const date = Temporal.PlainDate.from("2024-01-15").add({ days: dayOfWeek - 1 });
isoOrder.push(formatter.format(date.toZonedDateTime("UTC").epochMilliseconds));
}
return isoOrder.slice(firstDay - 1).concat(isoOrder.slice(0, firstDay - 1));
} catch {
return [];
}
} catch {
return [];
}
}
export { eraNamesFor, meridiemsFor, monthNamesFor, weekdayNamesFor };