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.

64 lines • 5.84 kB
import { ExpandRecurrenceOptions, OccurrenceWindow, RecurrenceOccurrence, RecurrenceRule } from "../../types/recurrence.js"; import "../../types/index.js"; /** * Expand a recurrence rule into its occurrences inside a window, in a time zone. * * DTSTART is `start`, a wall time in `timeZone`. The rule is expanded over wall times * (RFC 5545 §3.3.10, RFC 8984 §4.3.3.1) and each wall time is placed in the zone: * * - DTSTART is always the first occurrence and counts toward `COUNT`, even when it does not match * the rule or is after `UNTIL`. * - `COUNT` counts wall times before any are placed, so a wall time the zone skips still counts, * and two that land on one instant (a skipped 02:00 and the 03:00 after it) are one row, the * earlier wall time's. * - `UNTIL` is a UTC instant and inclusive; it limits the rule, not `rdate`. * - `rdate` adds occurrences and `exdate` removes them. An entry with an offset or `Z` is an exact * instant, an entry without one is a wall time in `timeZone`. A wall-time `exdate` names the * instant that wall time takes under `disambiguation`, as a wall-time `rdate` does, and removes * the occurrence at that instant. An `rdate` or `exdate` that is a date, a period or has * fractional seconds returns `[]`, and so does a `start`, `rdate` or `exdate` with a bracketed * annotation (`[Europe/Paris]`), which would name a second zone. * - A skipped or repeated wall time takes the instant `disambiguation` names: `"compatible"` * (default; the earlier of a repeat, the later of a skip), `"earlier"`, `"later"`, or `"reject"`, * which runs the call as `"compatible"` and then returns `[]` when `start`, a wall-time `rdate` * or `exdate` entry (wherever it falls), or the wall time of a returned occurrence is skipped or * repeated. Occurrences outside the window do not count, and an entry with an offset or `Z` * never refuses. * - An occurrence past the range Temporal holds (up to `+275760-09-13T00:00:00Z`, from * `-271821-04-20T00:00:00Z`) is left out, not an error. * - The window is an interval (`start` up to but not including `end`) or the first `count` * occurrences after an instant (1 to 10,000). * - `timeZone` is a time zone identifier or a fixed offset (`+05:30`, `-00:44:30`). * * Returns `[]` for an invalid argument and for a result of more than 10,000 rows. A * `{ after, count }` call for a rule with fewer than `count` occurrences returns the ones it has, * as a `COUNT`-limited rule does: `FREQ=DAILY;BYMONTH=2;BYMONTHDAY=30` gives DTSTART alone. A * rule from `YEARLY` to `DAILY` finds up to `count` occurrences however rarely it matches, as far * as Temporal's date range reaches. An `HOURLY`, `MINUTELY` or `SECONDLY` rule with a large * `INTERVAL` costs one scan of at most 86,400 steps to learn which times of day its steps can * land on, then one unit per row and per passing day. So `FREQ=MINUTELY;INTERVAL=7;BYHOUR=3;BYMINUTE=0` * fills a window of 10,000 rows, and a sub-daily rule that never matches again gives DTSTART alone. * `[]` also comes back when the work budget of 2,000,000 units runs out. * * Complexity: O(W + K log K) for W the work the expansion spends, bounded by 2,000,000 units, and * K the rows kept, at most 10,000. A rule that matches rarely pays one unit for each year it * crosses, not one for each day, after at most 146,097 units to learn the 400-year calendar. * The cost follows the window, not the distance from DTSTART: a `COUNT` rule counts the * occurrences before the window instead of walking them, in at most about 400,000 units, so * `FREQ=DAILY;COUNT=2000000` from 2024-01-01 runs to its last occurrence, on 7499-10-24. * * @param rule a recurrence rule, from `parseRecurrenceRule` * @param start DTSTART, an ISO date-time without offset or sub-second part * @param timeZone an IANA time zone or fixed offset * @param window the interval or `{ after, count }` to expand * @param options extra and removed occurrences, and how a skipped or repeated wall time is placed * @returns the occurrences in ascending instant order, or `[]` * * @example expandRecurrence({ frequency: "daily", interval: 1, firstDayOfWeek: 1, count: 2 }, "2024-03-09T02:30:00", "America/New_York", { start: "2024-03-10T00:00:00Z", end: "2024-03-11T00:00:00Z" }) // [{ local: "2024-03-10T02:30:00", instant: "2024-03-10T07:30:00Z", classification: "nonexistent", earlier: "2024-03-10T06:30:00Z", later: "2024-03-10T07:30:00Z" }] * @example expandRecurrence(parseRecurrenceRule("FREQ=DAILY;COUNT=2"), "2024-03-09T02:30:00", "America/New_York", { start: "2024-01-01T00:00:00Z", end: "2025-01-01T00:00:00Z" }).map((row) => row.instant) // ["2024-03-09T07:30:00Z", "2024-03-10T07:30:00Z"] * @example expandRecurrence(parseRecurrenceRule("FREQ=DAILY"), "2024-01-01T09:00:00", "UTC", { after: "2024-01-01T09:00:00Z", count: 1 }).map((row) => row.local) // ["2024-01-02T09:00:00"] * @example expandRecurrence(parseRecurrenceRule("FREQ=DAILY;COUNT=2"), "2024-11-02T01:30:00", "America/New_York", { after: "2024-01-01T00:00:00Z", count: 5 }, { disambiguation: "reject" }) // [] (01:30 repeats on November 3) * @example expandRecurrence(parseRecurrenceRule("FREQ=DAILY;BYMONTH=2;BYMONTHDAY=30"), "2024-01-01T09:00:00", "UTC", { after: "2024-01-01T00:00:00Z", count: 1 }).map((row) => row.instant) // ["2024-01-01T09:00:00Z"] (DTSTART is the only occurrence) * @example expandRecurrence(parseRecurrenceRule("FREQ=DAILY;COUNT=1"), "2024-06-01T12:00:00", "America/New_York", { after: "2024-01-01T00:00:00Z", count: 5 }, { rdate: ["2024-06-05T12:00:00[Europe/Paris]"] }) // [] (an annotation is not read) */ export declare function expandRecurrence(rule: RecurrenceRule, start: string, timeZone: string, window: OccurrenceWindow, options?: ExpandRecurrenceOptions): RecurrenceOccurrence[];