openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
30 lines (29 loc) • 1.45 kB
JavaScript
import { a as resolveUserTimezone } from "./date-time-ChxDjIEM.mjs";
//#region src/cron/active-hours.ts
function assertCronActiveHours(e) {
if (!/^([01]\d|2[0-3]):[0-5]\d$/.test(e.start) || !/^(([01]\d|2[0-3]):[0-5]\d|24:00)$/.test(e.end)) throw new Error("cron activeHours requires HH:MM start/end (24:00 is allowed only for end)");
if (e.timezone !== void 0 && e.timezone !== "user" && e.timezone !== "local") try {
new Intl.DateTimeFormat("en-US", { timeZone: e.timezone }).format(0);
} catch {
throw new Error("cron activeHours.timezone must be user, local, or an IANA timezone");
}
}
/** Wall-clock comparison preserves overnight windows and both DST occurrences. */
function isCronWithinActiveHours(e, t, n) {
if (!e) return true;
const r = e.timezone;
const i = !r || r === "user" ? resolveUserTimezone(n) : r === "local" ? Intl.DateTimeFormat().resolvedOptions().timeZone : r;
const a = new Intl.DateTimeFormat("en-US", {
timeZone: i,
hour: "2-digit",
minute: "2-digit",
hourCycle: "h23"
}).formatToParts(new Date(t));
const o = Number(a.find((e) => e.type === "hour")?.value) * 60 + Number(a.find((e) => e.type === "minute")?.value);
const minutes = (e) => Number(e.slice(0, 2)) * 60 + Number(e.slice(3));
const s = minutes(e.start);
const c = minutes(e.end);
return s === c ? false : c > s ? o >= s && o < c : o >= s || o < c;
}
//#endregion
export { isCronWithinActiveHours as n, assertCronActiveHours as t };