@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.
104 lines • 6.15 kB
TypeScript
import { Temporal } from "./temporal.js";
/**
* Consecutive split steps allowed to resolve to the previous boundary before a split gives up.
*
* A deleted local day costs one skipped step; the cap only stops a zone (or a stub) that never
* advances from looping forever.
*/
export declare const MAX_STALLED_SPLIT_STEPS = 8;
/**
* Whether a plural duration unit is an exact time unit (hours and smaller).
*
* - Exact units never clamp, so split functions step them from the previous boundary.
* - Calendar units (`years`, `months`, `weeks`, `days`) return `false` and stay anchored to the
* interval start.
*
* @param unit plural duration unit, as returned by `resolveDurationUnit`
* @returns `true` for `hours` through `nanoseconds`, otherwise `false`
*
* @example isExactDurationUnit("hours") // true
* @example isExactDurationUnit("days") // false
* @example isExactDurationUnit("hour") // false
*/
export declare function isExactDurationUnit(unit: string): boolean;
/**
* Tile `[start, end)` into `amount × unit` steps for one Temporal type.
*
* - Calendar units are anchored: boundary k is `start.add({ [unit]: amount × k })`, so month-end
* starts don't drift.
* - Exact units step from the previous boundary (`previous.add({ [unit]: amount })`), which never
* clamps and avoids `amount × k` passing `Number.MAX_SAFE_INTEGER`.
* - A step equal to the previous boundary is skipped (a deleted local day); more than
* `MAX_STALLED_SPLIT_STEPS` in a row returns `null`.
* - A step that goes backwards returns `null`.
* - More than `maxSlices` slices returns `null` as soon as the next one is due, so the work stays
* bounded by the limit (owner decision A2, CORE-8).
* - The last slice's end is `end` itself, whether the last step passes `end` (trimmed) or lands on
* it. Callers handle `start >= end` before calling.
* - A step that throws a RangeError (it lands past Temporal's last representable value, so after
* `end`) closes the split with a last slice ending at `end`.
*
* Generic over the value type only — each caller passes its own Temporal type and comparator, so
* plain, zoned and instant values are never mixed.
*
* @param start interval start
* @param end interval end, strictly after `start`
* @param compare the Temporal type's `compare`
* @param unit plural duration unit
* @param amount positive number of units per step
* @param maxSlices most slices to build; one more returns `null` before it is built
* @param add how a duration is added to a value (default `value.add(duration)`; the plain date
* caller passes its calendar-correct add)
* @returns `[sliceStart, sliceEnd]` pairs, or `null` when stepping goes backwards, stalls, or
* would build more than `maxSlices`
*
* @example tileByUnit(Temporal.PlainDate.from("2024-01-01"), Temporal.PlainDate.from("2024-01-04"), Temporal.PlainDate.compare, "days", 2, 100) // [[2024-01-01, 2024-01-03], [2024-01-03, 2024-01-04]]
* @example tileByUnit(Temporal.PlainDate.from("2024-01-01"), Temporal.PlainDate.from("2024-01-04"), Temporal.PlainDate.compare, "hours", 1, 100) // null (PlainDate ignores hours, so no step advances)
*/
export declare function tileByUnit<T extends {
add(duration: Temporal.DurationLike): T;
}>(start: T, end: T, compare: (a: T, b: T) => number, unit: string, amount: number, maxSlices: number, add?: (value: T, duration: Temporal.DurationLike) => T): Array<[T, T]> | null;
/**
* The step a split takes when its boundaries are then floored to whole `floorUnit`s.
*
* - An exact step shorter than one `floorUnit` moves each boundary by less than one unit, so the
* floored boundaries are every whole unit from the start to the end, each one repeated once per
* step that falls inside it. Stepping by one `floorUnit` gives those boundaries once each: no
* empty piece, and no work spent on steps the result cannot show.
* - Any other step is returned unchanged: a step of one `floorUnit` or more never floors two
* boundaries to the same value, a calendar step is never shorter than a second, and a
* fractional `amount` is left for Temporal to reject.
* - The split's start must itself be a whole `floorUnit`, as a Unix epoch is.
*
* @param unit plural duration unit, as returned by `resolveDurationUnit`
* @param amount positive number of units per step
* @param floorUnit plural exact unit the boundaries are floored to
* @returns the `[unit, amount]` to step by
*
* @example stepNoFinerThan("milliseconds", 500, "seconds") // ["seconds", 1]
* @example stepNoFinerThan("milliseconds", 1500, "seconds") // ["milliseconds", 1500]
* @example stepNoFinerThan("days", 1, "seconds") // ["days", 1]
*/
export declare function stepNoFinerThan(unit: string, amount: number, floorUnit: string): [unit: string, amount: number];
/**
* A lower bound on the number of slices `tileByUnit` returns for a span, without stepping.
*
* - Exact units (hours and smaller) never clamp or stall: the bound is `ceil(span / step)`.
* - Calendar units step at most `amount ×` the longest unit, so reaching `end` takes at least
* `ceil(span / longest step)` steps.
* - `zoned` values may also shift by an offset change (under 48 hours) and may stall up to
* `MAX_STALLED_SPLIT_STEPS` steps in a row, so only one step in `MAX_STALLED_SPLIT_STEPS + 1`
* is guaranteed to be a slice.
* - Callers compare the bound with `maxPieces` to return the sentinel before building a huge split;
* `tileByUnit`'s own `maxSlices` settles counts the bound cannot.
*
* @param spanNs exact span between start and end, in nanoseconds
* @param unit plural duration unit, as returned by `resolveDurationUnit`
* @param amount units per step
* @param zoned whether the values carry a time zone with offset transitions
* @returns a count no greater than the slices `tileByUnit` would build (0 when unknown)
*
* @example minSlicesForSpan(3 * 3_600_000_000_000, "hours", 1, false) // 3
* @example minSlicesForSpan(62 * 86_400_000_000_000, "months", 1, false) // 2
*/
export declare function minSlicesForSpan(spanNs: number, unit: string, amount: number, zoned: boolean): number;