@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.
103 lines • 6.27 kB
TypeScript
import { RecurrenceRule } from "../types/recurrence.js";
import "../types/index.js";
/** A local wall time: an epoch day and the second of that day, 0 to 86399. */
export type LocalMoment = {
day: number;
second: number;
};
/**
* Why `expandLocalMoments` returned: `"stopped"` when `visit` returned `false`, `"capped"` when
* the work budget ran out, `"end"` when a pick fell after `lastDay` (default and ceiling:
* `MAX_EPOCH_DAY`, the last Temporal date). The last period is evaluated in full, so `"end"` can
* come partway through it.
*/
export type ExpansionStatus = "stopped" | "capped" | "end";
/**
* Work units one expansion may spend: periods visited, years looked up, days evaluated while a
* year of the day cache fills, and moments considered.
*/
export declare const RECURRENCE_WORK_CAP = 2000000;
/**
* The week of `day` in its own week-year, with weeks starting on ISO weekday `firstDayOfWeek` and
* week 1 the first week with at least four days in the year (RFC 5545 §3.3.10, BYWEEKNO).
* With `firstDayOfWeek` 1 this is ISO 8601 `weekOfYear` and `yearOfWeek`.
*
* @example weekYearWeek(epochDayOf(2024, 12, 30), 1) // { weekYear: 2025, week: 1, weeks: 52 }
*/
export declare function weekYearWeek(day: number, firstDayOfWeek: number): {
weekYear: number;
week: number;
weeks: number;
};
/**
* How many candidates an expansion from DTSTART visits, after `start` itself, before the first
* one an expansion from `fromDay` visits: the ones in the aligned periods or steps the
* fast-forward skips. `COUNT` counts from DTSTART, so a `COUNT` rule needs this to expand from
* `fromDay`. From the first visit on, both expansions visit the same candidates.
*
* Its cost does not follow the distance. A daily or sub-daily step's time of day repeats every
* `K` steps, at most 86,400, and the day filter every 400 years: the count reads the `K` steps once,
* then the passing days in the span, or, for a span of 400 years or more, every day of one cycle.
* Yearly to weekly periods repeat once the 400-year cycle and the interval line up, after at most
* 20,871 periods, which it reads once. So the whole is bounded by a few hundred thousand units.
*
* @returns the count and the units spent, or `undefined` when `budget` ran out.
*
* @example
* const rule = { frequency: "daily", interval: 1, firstDayOfWeek: 1 } as const;
* countMomentsBefore(rule, { day: 0, second: 0 }, 1_000_000); // { count: 999999, spent: 0 }
*/
export declare function countMomentsBefore(rule: RecurrenceRule, start: LocalMoment, fromDay: number, budget?: number): {
count: number;
spent: number;
} | undefined;
/**
* Expands a recurrence rule over local wall times (RFC 8984 §4.3.3.1, RFC 5545 §3.3.10) and
* calls `visit` with each candidate in strictly ascending order. No zone is involved: the caller
* resolves each moment, and applies `UNTIL`, `COUNT` and windows.
*
* `rule` has already passed `checkRecurrenceRule`. `start` is DTSTART's wall time and is visited
* first, always, even when it does not match the rule (RFC 8984 §4.3.3.1 extra rule 1). Every
* later visit is a candidate strictly after `start`. `fromDay` is a fast-forward floor: periods
* that end before it are skipped in closed form, so the cost depends on the window, not on the
* distance from DTSTART; a caller that applies `COUNT` counts the skipped moments with
* {@link countMomentsBefore}. The first period visited may still yield moments
* before `fromDay`; the caller filters them. Pass `start.day` to expand from DTSTART.
*
* Complexity: `O(P + Y + F + R)` for the periods visited (skip jumps included), the years looked
* up (one unit each, whatever the year holds), the days evaluated while the day cache fills and
* the moments considered. The day filter repeats every 400 years (146,097 days), so each year of
* the cycle is evaluated once, at one unit a day (a month outside `BYMONTH` is one unit): `F` is
* at most 146,097 for the whole call. A day or period that fails the filter jumps to the next
* passing day, found in at most 400 years' lookups; none in 400 years means none ever. The sum is
* bounded by `budget` (default {@link RECURRENCE_WORK_CAP}).
*
* A walk that has had nothing to pick after `lcm(interval, periods in 400 years)` steps from its
* first one ends as `"end"`, because the filter and the alignment have both repeated. So a rule
* that never matches again, such as February 30, or an interval that never lines up, such as every
* 7th day on Mondays from a Tuesday, ends as `"end"` and not `"capped"`. `"capped"` is left for a
* budget too small for that.
*
* The sub-daily walk (`HOURLY`, `MINUTELY`, `SECONDLY`) does not visit every aligned step. The time
* of day of step `k` repeats every `K = perDay / gcd(perDay, interval)` steps (at most 86,400), so
* with a `BYHOUR`, `BYMINUTE`, `BYSECOND` or `BYSETPOS` the `K` steps are scanned once, one unit
* each, for the ones that can ever hit. The walk then goes from hit to hit, and a hit on a failing
* day jumps to the next passing day. Its cost is `O(K + H + D)` for the scan, the hits visited and
* the passing days jumped to, however large the interval or however rarely the steps hit. No hit at
* all in `K` steps ends as `"end"` after that one scan.
*
* `lastDay` (epoch day, default and ceiling `MAX_EPOCH_DAY`) ends the expansion: no pick after it
* is visited. It may be a function, read at every period, so `visit` can bring the end forward
* once it has what it needs. The period holding `lastDay` is still evaluated in full, so a rule
* that never matches ends as `"end"` once the periods pass `lastDay`, not as `"capped"`.
*
* @returns `"stopped"` when `visit` returned `false`, `"capped"` when the budget ran out,
* `"end"` when a pick fell after `lastDay`.
*
* @example
* const days: number[] = [];
* const rule = { frequency: "daily", interval: 1, firstDayOfWeek: 1 } as const;
* expandLocalMoments(rule, { day: 0, second: 0 }, 0, (m) => days.push(m.day) < 3); // "stopped"
* // days is [0, 1, 2]
*/
export declare function expandLocalMoments(rule: RecurrenceRule, start: LocalMoment, fromDay: number, visit: (moment: LocalMoment) => boolean, budget?: number, lastDay?: number | (() => number)): ExpansionStatus;