UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

113 lines (112 loc) • 3.65 kB
//#region src/infra/format-time/format-datetime.ts let validatedTimezone; let timeZoneDayKeyFormatter; /** * Validate an IANA timezone string. Returns the string if valid, undefined otherwise. */ function resolveTimezone(e) { try { if (e !== validatedTimezone) { Intl.DateTimeFormat("en-US", { timeZone: e }); validatedTimezone = e; } return e; } catch { return; } } /** Build a stable YYYY-MM-DD formatter for instants in one IANA timezone. */ function createTimeZoneDayKeyFormatter(e) { const t = typeof e === "string" && timeZoneDayKeyFormatter?.timeZone === e ? timeZoneDayKeyFormatter.formatter : new Intl.DateTimeFormat("en-US-u-ca-iso8601-nu-latn", { timeZone: e, year: "numeric", month: "2-digit", day: "2-digit" }); if (typeof e === "string") timeZoneDayKeyFormatter = { timeZone: e, formatter: t }; return (e) => { const n = t.formatToParts(e); const pick = (e) => n.find((t) => t.type === e)?.value; const r = pick("year"); const i = pick("month"); const a = pick("day"); if (!r || !i || !a) throw new Error("Intl.DateTimeFormat omitted required calendar-day parts"); return `${r.padStart(4, "0")}-${i}-${a}`; }; } /** Resolve the first instant belonging to a calendar day in an IANA timezone. */ function resolveTimeZoneDayStartMs(e, t) { const n = /^(\d{4})-(\d{2})-(\d{2})$/.exec(e); if (!n) return; const r = Date.UTC(Number(n[1]), Number(n[2]) - 1, Number(n[3])); const i = createTimeZoneDayKeyFormatter(t); const a = 1728e5; let o = r - a; let s = r + a; while (o < s) { const t = o + Math.floor((s - o) / 2); if (i(new Date(t)) < e) o = t + 1; else s = t; } return i(new Date(o)) === e ? o : void 0; } let zonedTimestampFormatter; /** * Format a Date as a UTC timestamp string. * * Without seconds: `2024-01-15T14:30Z` * With seconds: `2024-01-15T14:30:05Z` */ function formatUtcTimestamp(e, t) { const n = String(e.getUTCFullYear()).padStart(4, "0"); const r = String(e.getUTCMonth() + 1).padStart(2, "0"); const i = String(e.getUTCDate()).padStart(2, "0"); const a = String(e.getUTCHours()).padStart(2, "0"); const o = String(e.getUTCMinutes()).padStart(2, "0"); if (!t?.displaySeconds) return `${n}-${r}-${i}T${a}:${o}Z`; return `${n}-${r}-${i}T${a}:${o}:${String(e.getUTCSeconds()).padStart(2, "0")}Z`; } /** * Format a Date with timezone display using Intl.DateTimeFormat. * * Without seconds: `2024-01-15 14:30 EST` * With seconds: `2024-01-15 14:30:05 EST` * * Returns undefined if Intl formatting fails. */ function formatZonedTimestamp(e, t) { try { const { timeZone: n, displaySeconds: r = false, displayWeekday: i = false } = t ?? {}; const a = `${n}|${r}|${i}`; let o = n !== void 0 && zonedTimestampFormatter?.key === a ? zonedTimestampFormatter.formatter : void 0; if (!o) { o = new Intl.DateTimeFormat("en-US", { timeZone: n, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", hourCycle: "h23", timeZoneName: "short", second: r ? "2-digit" : void 0, weekday: i ? "short" : void 0 }); if (n !== void 0) zonedTimestampFormatter = { key: a, formatter: o }; } const s = Object.fromEntries(o.formatToParts(e).map(({ type: e, value: t }) => [e, t])); const { year: c, month: l, day: u, hour: d, minute: f, second: p, weekday: m } = s; const h = r ? `:${p}` : ""; return `${i ? `${m} ` : ""}${c}-${l}-${u} ${d}:${f}${h} ${s.timeZoneName}`; } catch { return; } } //#endregion export { resolveTimezone as a, resolveTimeZoneDayStartMs as i, formatUtcTimestamp as n, formatZonedTimestamp as r, createTimeZoneDayKeyFormatter as t };