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.

61 lines • 5.35 kB
import { CronExpression, CronRun, CronTransitionPolicy } from "../../types/cron.js"; import { OccurrenceWindow } from "../../types/recurrence.js"; import "../../types/index.js"; /** * The runs of a five-field cron schedule inside a window, in a time zone. * * `expression` is a parsed schedule (`parseCronExpression`). Its wall times are read in `timeZone` * and the seconds are always 0. * * - **Day rule.** The month always has to match. Day of month and day of week are ORed when both * are restricted (neither is `*`) and ANDed otherwise (crontab(5) NOTES; cronie `cron.c`). * POSIX's wording also ORs when the month is restricted; no shipped cron does, and GMT does not. * - **Window.** An interval (`start` up to but not including `end`) or the first `count` runs after * an instant (1 to 10,000). * - **`policy` is required**, because real cron daemons differ and a default would pick one * silently. It says what happens to a scheduled wall time that a zone change skips or repeats, * in the vocabulary of `classifyLocal`: `"unique"`, `"nonexistent"` (skipped) or `"ambiguous"` * (repeated). Each run's `classification` says which it was. * - `"crontabMatch"` matches the schedule against each real wall time (crontab(5)). A * `"nonexistent"` time "will never match": no run. An `"ambiguous"` time runs twice, once at * each instant. * - `"daemonCatchUp"` models what cron(8) documents for a running daemon (Debian and * cronie), applied to every offset change; cronie's code reacts only to a change of the DST * flag. For a fixed-time job (neither minute nor hour is `*`), a `"nonexistent"` time in a gap * of under 3 hours runs once at the transition, `local` still the wall time the schedule * named, and an `"ambiguous"` time in an overlap of at most 3 hours runs once at the earlier * instant, not again (cron.c adds a one-minute tick to the shift, so a gap of exactly 3 hours * is "large" and an overlap of exactly 3 hours is not). For any other job or a larger change, * it behaves like `"crontabMatch"`. It leaves out cron(8)'s minute-level case for a "small * change" (a clock step of five minutes or less), because GMT models offset changes and not * clock steps. It also leaves out `CRON_TZ`. * - Runs that share an instant stay separate rows: each is a separate execution. * - `timeZone` is a time zone identifier or a fixed offset (`+05:30`). * * Returns `[]` for an invalid argument (including a missing or unknown `policy`), a schedule that * never matches (such as February 30), a walk that needs more than 2,000,000 work units, and a * result of more than 10,000 runs. The work is one unit per step of the day scan (a day tested, or * a jump over the months the schedule skips) and one per wall time of each matching day, so a * sparse schedule such as `0 0 29 2 *` or `@yearly` stays inside the budget for any `count` up to * 10,000. A run whose instant is past Temporal's range (after +275760-09-13T00:00:00Z) is past the * end of the schedule: it is left out and the runs before it are returned. * * Complexity: O(S + R + K) for S the days scanned (the window's days, or at most 146,097 for an * expression that never matches, with a jump over unlisted months counting as one), R the runs * and K the zone transitions near them. The walk spends at most 2,000,000 work units and returns * `[]` if it needs more. * * @param expression a cron schedule, from `parseCronExpression` * @param timeZone an IANA time zone or fixed offset * @param window the interval or `{ after, count }` to list * @param policy `"crontabMatch"` or `"daemonCatchUp"` * @returns the runs sorted by `instant`, then by `local`, or `[]` * * @example cronRuns({ minute: { values: [30], star: false }, hour: { values: [2], star: false }, dayOfMonth: { values: [10], star: false }, month: { values: [3], star: false }, dayOfWeek: { values: [1, 2, 3, 4, 5, 6, 7], star: true } }, "America/New_York", { start: "2024-03-10T00:00:00Z", end: "2024-03-11T00:00:00Z" }, "daemonCatchUp") // [{ local: "2024-03-10T02:30:00", instant: "2024-03-10T07:00:00Z", classification: "nonexistent" }] * @example cronRuns(parseCronExpression("30 2 * * *"), "America/New_York", { start: "2024-03-09T00:00:00Z", end: "2024-03-12T00:00:00Z" }, "crontabMatch").map((run) => run.instant) // ["2024-03-09T07:30:00Z", "2024-03-11T06:30:00Z"] (no 02:30 on March 10) * @example cronRuns(parseCronExpression("30 1 * * *"), "America/New_York", { start: "2024-11-03T00:00:00Z", end: "2024-11-04T00:00:00Z" }, "crontabMatch").map((run) => run.instant) // ["2024-11-03T05:30:00Z", "2024-11-03T06:30:00Z"] (01:30 repeats) * @example cronRuns(parseCronExpression("0 0 29 2 *"), "UTC", { after: "2025-01-01T00:00:00Z", count: 2 }, "crontabMatch").map((run) => run.local) // ["2028-02-29T00:00:00", "2032-02-29T00:00:00"] * @example cronRuns(parseCronExpression("0 0 * * *"), "UTC", { after: "2024-01-01T00:00:00Z", count: 1 }, undefined) // [] (a policy is required) * @example cronRuns(parseCronExpression("0 0 30 2 *"), "UTC", { after: "2024-01-01T00:00:00Z", count: 1 }, "crontabMatch") // [] (February 30 never comes) */ export declare function cronRuns(expression: CronExpression, timeZone: string, window: OccurrenceWindow, policy: CronTransitionPolicy): CronRun[];