@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.
87 lines • 4.89 kB
TypeScript
import { Temporal } from "./temporal.js";
import { ResolvedBusinessCalendar } from "./businessCalendar.js";
/**
* The tariff terms reduced to what the walk reads. `calendar` is the working-day calendar free
* days are counted against, `null` on the calendar basis; `chargeCalendar` is the one charged days
* are counted against, `null` when every local day after expiry is charged.
*/
export type FreeTimeTerms = {
/**
* The counting zone as the caller wrote it: a time zone identifier or a stored UTC offset
* (`±HH:MM[:SS]`), so always readable by `zoneFrame`. The walk's zoned values are placed with
* `frameZoned` and turned back with `frameEpochNanoseconds` and `Temporal.Instant.fromEpochNanoseconds`.
*/
timeZone: string;
firstDay: "eventDay" | "nextDay";
calendar: ResolvedBusinessCalendar | null;
chargeCalendar: ResolvedBusinessCalendar | null;
};
/**
* Read a `FreeTimeOptions` bag, or `null` when it is not one: a non-object, an unknown `basis`
* or `firstDay`, a missing `firstDay` (no default: the two conventions differ by a day of
* charges), an invalid `timeZone`, or a `"working"` basis without a valid `BusinessCalendar`.
* The `calendar` is read once; it is required when a basis is `"working"`, and when given it must
* be a valid `BusinessCalendar` on either basis.
*
* With `readChargeBasis`, `chargeBasis` is required too, and has no default for the same reason:
* where free time is in working days, tariffs mostly charge every calendar day after it, but some
* tariffs charge working days only, and the two differ by every closed day after expiry.
*/
export declare function parseFreeTimeTerms(options: unknown, readChargeBasis?: boolean): FreeTimeTerms | null;
/** A whole number of free days no smaller than `minimum`, or `null`. */
export declare function parseFreeDays(freeDays: unknown, minimum: 0 | 1): number | null;
/**
* Read a `tiers` option: omitted means no bands, otherwise a strictly ascending list of positive
* safe whole chargeable-day ordinals, each the last day of its band, with no holes. Anything
* else is `null`.
*/
export declare function parseTiers(tiers: unknown): number[] | null;
/**
* Split `chargeable` days into the bands `tiers` describes: `[5, 10]` is days 1–5, 6–10 and 11
* onward. Every band is listed, an empty one with `days: 0`, so a caller's rate table lines up
* by index; the last band is always open (`to: null`).
*/
export declare function bandsByTier(tiers: number[], chargeable: number): {
from: number;
to: number | null;
days: number;
}[];
/**
* What a free-time walk returns, on epoch days (days from 1970-01-01, `epochDay.ts`) and epoch
* nanoseconds, so the dwell's length costs no Temporal work: callers format the few values they
* output.
*/
export interface FreeTimeLedger {
/** The first free day; `null` with no free days. */
firstFree: number | null;
/** The last free day; `null` with no free days. */
lastFree: number | null;
/**
* The first instant of the local day after the last free day (with no free days, of day one), in
* epoch nanoseconds. It includes the frame's shift, so it is the real instant.
*/
expiresAtNs: bigint;
/** Free days the half-open dwell `[clockStart, clockEnd)` touched; `0` with no `clockEnd`. */
used: number;
/** Counted days from `expiresAt` that the dwell touched, ascending; empty with no `clockEnd`. */
charged: number[];
}
/**
* Lay out free time and charges from the clock start, in epoch days (see `FreeTimeLedger`).
*
* The local days of the terminal are the dates from the event day on, each once, without a date
* the zone deleted. Free days are the first `freeDays` counted ones (every date on a calendar
* basis, a working day of `calendar` otherwise); `expiresAt` is the first instant of the visited
* date after the last of them, or with `freeDays` of `0` of the first counted one. With a
* `clockEnd` the half-open dwell `[clockStart, clockEnd)` touches a date when it starts before
* `clockEnd`, and a zero-length dwell touches the date it sits on, as `dwellTime` counts. `used` is
* the free days touched, `charged` the dates from `expiresAt` touched that `chargeCalendar` charges.
* The event day is neither free nor charged under `"nextDay"`.
*
* Counts come from closed forms over the zone's offset changes, so the dwell's length costs no
* work per date. Returns `null` when the walk would visit more than `MAX_WALKED_DAYS` dates, when
* a day boundary cannot be found, or when the expiry lies outside the representable range. A clock
* within `MAX_WALKED_DAYS` dates of Temporal's limits, or in a pass that re-enters its date, is
* laid out day by day (`walkFreeTimeByDay`).
*/
export declare function walkFreeTime(clockStart: Temporal.ZonedDateTime, freeDays: number, terms: FreeTimeTerms, clockEnd?: Temporal.ZonedDateTime): FreeTimeLedger | null;