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.

32 lines • 2.24 kB
import { Temporal } from "./temporal.js"; /** Temporal's duration fields, largest first (Temporal Table 21, the unit ladder). */ declare const DURATION_FIELDS: readonly ["years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"]; /** A plural Temporal duration field name. */ export type DurationField = (typeof DURATION_FIELDS)[number]; /** How one Temporal type moves by a duration and measures the difference to another value. */ export interface DifferenceOperations<T> { add: (from: T, duration: Temporal.DurationLike) => T; until: (from: T, to: T, largestUnit: DurationField) => Temporal.Duration; } /** * Express a difference in exactly the listed units, so the record is the whole difference. * * `duration` is `start.until(end)` with `largestUnit` = the largest listed unit and the caller's * rounding options. Its fields are kept while the listed units are contiguous or every unlisted * unit between them is zero, so a single unit or adjacent units return Temporal's own fields. * When an unlisted unit between two listed units has an amount, that amount is carried into the * next smaller listed unit (and a listed `weeks` below months or years is measured afresh, because * Temporal leaves weeks in `days` unless `largestUnit` is weeks): the start is moved by the listed amounts found so far (one Temporal * `add`, which adds years and months before days), and the rest is measured from there to the * rounded end (`start.add(duration)`) with `largestUnit` = that listed unit. The record then * satisfies `start.add(record)` = `start.add(duration)`. Units smaller than the smallest listed * unit are dropped, as for a single unit. * * @param start the start value * @param duration the (rounded) difference from `start` with the largest listed unit * @param units the listed plural units, any order, non-empty * @param operations the type's `add` and `until` * @returns the amount of each listed unit, keyed by plural unit name in the order of `units` */ export declare function differenceRecord<T>(start: T, duration: Temporal.Duration, units: readonly DurationField[], operations: DifferenceOperations<T>): Record<DurationField, number>; export {};