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.

170 lines (168 loc) • 6.61 kB
import { copticFieldTwin } from "./copticEra.js"; import { epochTwin } from "./islamicEpochEra.js"; // src/internal/rangeFieldValues.ts /** At most this many cached readers; past it the map clears (a cost of one rebuild each). */ const MAX_READERS = 256; const calendarFields = /* @__PURE__ */ new Map(); const periodReaders = /* @__PURE__ */ new Map(); /** Forget the cached readers (the engine's `Intl.DateTimeFormat` was replaced). */ function forgetFieldReaders() { calendarFields.clear(); periodReaders.clear(); } /** * The date and time fields of an instant in a calendar and zone, read from the engine's own * calendar data: `era`, `year` (or `relatedYear`), `month`, `day`, `hour` (0 to 23), `minute`, * `second`. * * @example localFieldsOf({ calendar: "gregory", timeZone: "UTC", locale: "en", hourCycle: undefined }, 0)["year"] // "1970" */ function localFieldsOf(resolved, ms) { const key = `${resolved.calendar}|${resolved.timeZone}`; let reader = calendarFields.get(key); if (reader === void 0) { reader = new Intl.DateTimeFormat("en", { calendar: resolved.calendar, timeZone: resolved.timeZone, numberingSystem: "latn", era: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", hourCycle: "h23" }); if (calendarFields.size >= MAX_READERS) calendarFields.clear(); calendarFields.set(key, reader); } const islamic = epochTwin(ms, resolved.calendar, resolved.timeZone); const coptic = islamic === null && resolved.calendar === "coptic" ? copticFieldTwin(ms, resolved.timeZone) : null; const twin = islamic ?? coptic; const fields = {}; for (const part of reader.formatToParts(twin === null ? ms : twin.ms)) fields[part.type] = (fields[part.type] ?? "") + part.value; if (twin !== null) fields["year"] = String(coptic === null ? 1 : coptic.year); return fields; } /** * The flexible day period the formatter prints for an instant, in its own width, or `""` when its * pattern prints none (so no two instants differ in it). The period is a property of the time as * the pattern prints it: with minutes, 12:00 is `noon` and 12:25 `in the afternoon`, while an * hour-only pattern says `noon` for the whole hour. So the reader carries the caller's own time * fields, and no date field (a week-year pattern could make a date field unsafe to read: D23). * * @example dayPeriodOf({ hour: "numeric", minute: "numeric", dayPeriod: "long" }, { calendar: "gregory", timeZone: "UTC", locale: "en", hourCycle: "h12" }, Date.UTC(2024, 6, 10, 12, 0)) // "noon" */ function dayPeriodOf(options, resolved, ms) { if (options.dayPeriod === void 0) return ""; const key = `${resolved.locale}|${resolved.calendar}|${resolved.timeZone}|${resolved.hourCycle}|${options.dayPeriod}|${options.hour}|${options.minute}|${options.second}|${options.fractionalSecondDigits}`; let reader = periodReaders.get(key); if (reader === void 0) { const twin = { calendar: resolved.calendar, timeZone: resolved.timeZone, hour: options.hour, minute: options.minute, second: options.second, fractionalSecondDigits: options.fractionalSecondDigits, dayPeriod: options.dayPeriod }; if (resolved.hourCycle !== void 0) twin.hourCycle = resolved.hourCycle; reader = new Intl.DateTimeFormat(resolved.locale, twin); if (periodReaders.size >= MAX_READERS) periodReaders.clear(); periodReaders.set(key, reader); } return reader.formatToParts(ms).find((part) => part.type === "dayPeriod")?.value ?? ""; } /** The Table 6 rows of a formatter, in table order (ECMA-402 11.5.8). */ function rowsOf(options, resolved, era) { const dateStyle = options.dateStyle !== void 0; const timeStyle = options.timeStyle !== void 0; const hour = options.hour !== void 0 || timeStyle; const minute = options.minute !== void 0 || options.second !== void 0 || timeStyle; const second = options.second !== void 0 || options.fractionalSecondDigits !== void 0 || timeStyle && options.timeStyle !== "short"; const fsd = options.fractionalSecondDigits; const field = (...types) => (ms) => { const fields = localFieldsOf(resolved, ms); return types.map((type) => fields[type] ?? "").join("|"); }; return [ { name: "era", printed: era, value: field("era") }, { name: "year", printed: options.year !== void 0 || dateStyle, value: field("year", "relatedYear", "yearName") }, { name: "month", printed: options.month !== void 0 || dateStyle, value: field("month") }, { name: "day", printed: options.day !== void 0 || dateStyle, value: field("day") }, { name: "ampm", printed: hour, value: (ms) => String(Number(localFieldsOf(resolved, ms)["hour"]) < 12) }, { name: "dayperiod", printed: hour || options.dayPeriod !== void 0, value: (ms) => dayPeriodOf(options, resolved, ms) }, { name: "hour", printed: hour, value: field("hour") }, { name: "minute", printed: minute, value: field("minute") }, { name: "second", printed: second, value: field("second") }, { name: "fractionalsecond", printed: fsd !== void 0, value: (ms) => String(Math.floor((ms % 1e3 + 1e3) % 1e3 * 10 ** ((fsd ?? 3) - 3))) } ]; } /** * Whether two instants hold equal field values in every Table 6 row ECMA-402 * PartitionDateTimeRangePattern checks, so the range is the start's single format. A row is * checked while no range pattern is selected, and from the first row the pattern prints until the * first later row it does not print (`checkMoreFields`): a month-only pattern ignores the day, a * pattern with minutes ignores the seconds. Printed text is never compared. * * @param options the formatter's options * @param resolved its resolved calendar, zone, locale and hour cycle * @param startMs the start in epoch milliseconds * @param endMs the end in epoch milliseconds * @param era whether the pattern prints an era (default: the `era` option is set) * @returns `true` when the range collapses to the start's single format * * @example rangeFieldsEqual({ year: "2-digit" }, { calendar: "gregory", timeZone: "UTC", locale: "en", hourCycle: undefined }, Date.UTC(1924, 0, 1), Date.UTC(2024, 0, 1)) // false */ function rangeFieldsEqual(options, resolved, startMs, endMs, era = options.era !== void 0) { let selected = false; for (const row of rowsOf(options, resolved, era)) { if (selected && !row.printed) return true; selected = row.printed; if (row.value(startMs) !== row.value(endMs)) return false; } return true; } export { dayPeriodOf, forgetFieldReaders, localFieldsOf, rangeFieldsEqual };