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.

126 lines • 5.91 kB
import { Temporal } from "./temporal.js"; import { Disambiguation } from "../types/disambiguation.js"; import { DutyViolation, RuleOutcome } from "../types/duty.js"; import "../types/index.js"; import { ZoneFrame } from "./zoneFrame.js"; import { DutyLogEntry, DutyRun } from "./dutyLog.js"; import { ResolvedWallClock, UnitScale, UnitSpan } from "./dutyPeriods.js"; import { ResolvedPair, ResolvedPeriod, ResolvedRule, StatusSet } from "./dutyRules.js"; import { RunStore } from "./dutyRunIndex.js"; /** Everything one call evaluates its rules against. */ export type DutyContext = { entries: readonly DutyLogEntry[]; frame: ZoneFrame; dayStartTime: Temporal.PlainTime | null; /** How a day start or local window edge the zone repeats or skips is resolved. */ disambiguation: Disambiguation; scales: Map<string, UnitScale>; /** Local window edges already resolved, by local date-time: Temporal resolves each once. */ wallClocks: Map<string, ResolvedWallClock | null>; /** * Local window instances already resolved, by window: separate stretches of local dates, each * with the span it covers. */ windowInstances: Map<string, WindowInstances[]>; /** The whole log and the run index of each status test, built once per call. */ store: RunStore; }; /** A window a call cannot place in Temporal's range. Caught by the public function. */ export declare class Unrepresentable extends RangeError {} /** * Read the log, rules and options `evaluateDutyLog` and `dutyViolations` take, all-or-nothing. * * - `options` must be an object with a valid `timeZone`; `dayStartTime` is optional, but a rule * that counts operator days needs it; `disambiguation` is `"compatible"` when absent. Each * member is read once. * * @param log candidate duty log * @param rules candidate rules * @param options candidate `{ timeZone, dayStartTime?, disambiguation? }` * @returns the context and resolved rules, or null on invalid input */ export declare function prepareDutyCall(log: unknown, rules: unknown, options: unknown): { context: DutyContext; rules: ResolvedRule[]; } | null; /** * Read a status list a helper takes: an array of strings, `[]` allowed. Each element is read once. * * @param value candidate status list * @returns the statuses, or null when `value` is not an array of strings */ export declare function readStatusList(value: unknown): StatusSet | null; /** * Maximal runs of touching entries whose status passes `test`, merged as `mergeIntervals` merges * them (CORE-6): touching entries join, and a gap or another status ends a run. */ export declare function runsOf(log: readonly DutyLogEntry[], test: StatusSet): DutyRun[]; /** The parts of runs inside `[from, to)`, empty parts dropped. */ export declare function clipRuns(runs: readonly DutyRun[], from: bigint, to: bigint): DutyRun[]; /** Time in `statuses` inside `[from, to)`. */ export declare function timeIn(log: readonly DutyLogEntry[], statuses: StatusSet, from: bigint, to: bigint): bigint; /** * The paired rests among runs of rest: two runs in a row, each shorter than `fullRest` and at * least `pair.each`, the longer at least `pair.one` and both together at least `pair.combined`. * * - A run of `fullRest` or more is a rest on its own: it pairs with nothing, and no pair spans it. * - A run shorter than `pair.each` is neither half of a pair nor a break between two runs. * - Pairs may chain: of runs a, b and c, both (a, b) and (b, c) are returned when both qualify. * * @param runs runs of rest, in order * @param fullRest the length at which a run is a full rest * @param pair the three floors * @returns the qualifying pairs, in order */ export declare function restPairs(runs: readonly DutyRun[], fullRest: bigint, pair: ResolvedPair): { first: DutyRun; second: DutyRun; }[]; /** One local window on one local date, resolved; `rejected` as `resolveWallClock` reports it. */ type WindowInstance = { start: bigint; end: bigint; rejected: boolean; }; /** The instances of one local window on the local dates `first` to `last`, covering `[from, to)`. */ type WindowInstances = { from: bigint; to: bigint; first: Temporal.PlainDate; last: Temporal.PlainDate; instances: WindowInstance[]; }; /** * Evaluate every rule at one instant. * * @param context the call's context * @param rules the resolved rules * @param at the instant, epoch nanoseconds * @returns one outcome per rule, in order */ export declare function evaluateDutyRules(context: DutyContext, rules: readonly ResolvedRule[], at: bigint): RuleOutcome[]; /** * Call `visit` with the end of every period of `scale` that ends inside `(from, until]`, starting * with the one `from` falls in. * * - Throws `Unrepresentable` when more than `MAX_PERIOD_COUNT` periods end there, or for a period * Temporal cannot hold. * - A span of `MAX_PERIOD_COUNT + 1` times `LONGER_THAN_ANY` or more certainly holds more ends * than that, so it is refused without a lookup. A shorter span is walked, and the walk decides. */ export declare function walkBoundaries(scale: UnitScale, unit: Extract<ResolvedPeriod, { kind: "units"; }>["unit"], from: bigint, until: bigint, visit: (boundary: bigint) => void): void; /** * Every violation of the rules whose severity is `"violation"` inside a range. A moving window's * failures are one violation while no check passes between them; a count since a reset, or one * fixed period, is one violation however often it is checked. Each is reported by its worst * window and listed in the range it began in. * * @param context the call's context * @param rules the resolved rules * @param range the range, epoch nanoseconds * @returns the violations, ordered by window start and then by rule */ export declare function scanDutyViolations(context: DutyContext, rules: readonly ResolvedRule[], range: UnitSpan): DutyViolation[]; export {};