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.

98 lines • 6.46 kB
import { Temporal } from "../temporal.js"; import { ResolvedRelativeTo } from "../resolveDurationRelativeTo.js"; export type DateUnit = "year" | "month" | "week" | "day"; export type TimeUnit = "hour" | "minute" | "second" | "millisecond" | "microsecond" | "nanosecond"; export type Unit = DateUnit | TimeUnit; export declare const NANOSECONDS_PER_DAY: bigint; /** TC39 Table "Temporal units by descending magnitude", "Length in Nanoseconds" column. */ export declare const NANOSECONDS_PER_TIME_UNIT: Record<TimeUnit, bigint>; /** An RFC 9557 time zone annotation (`[Europe/London]`, `[!+01:00]`), never a `key=value` one. */ export declare const timeZoneAnnotation: RegExp; interface DateDuration { years: number; months: number; weeks: number; days: number; } /** TC39 Internal Duration Record. */ export interface InternalDuration { date: DateDuration; time: bigint; } /** * The time zone, calendar and origin wall clock (`isoDateTime`) the rounding steps share. An unset * `timeZone` is the spec's plain context: exact times are `GetUTCEpochNanoseconds`, never checked * against the instant range. */ interface RoundingContext { timeZone: string | undefined; calendar: string; wall: Temporal.PlainDateTime; } export interface RoundingSettings { largestUnit: Unit; smallestUnit: Unit; increment: number; roundingMode: Temporal.RoundingMode; } /** Options accepted by `ZonedDateTime#until`. */ export type ZonedDifferenceOptions = Temporal.DifferenceOptions<Temporal.DateTimeUnit>; /** Options accepted by `Duration#round`, with GMT's `relativeTo` forms. */ export interface DurationRoundOptions { largestUnit?: Temporal.LargestUnit<Temporal.DateTimeUnit>; smallestUnit?: Temporal.SmallestUnit<Temporal.DateTimeUnit>; roundingIncrement?: number; roundingMode?: Temporal.RoundingMode; relativeTo?: ResolvedRelativeTo; } /** A Temporal unit name, singular or plural, or null when it is not one. */ export declare function toUnit(value: unknown): Unit | null; /** TC39 `TemporalUnitCategory(unit)` is ~date~. */ export declare function isDateUnit(unit: Unit): unit is DateUnit; /** TC39 `LargerOfTwoTemporalUnits`. */ export declare function largerUnit(one: Unit, two: Unit): Unit; export declare function bigintSign(value: bigint): -1 | 0 | 1; export declare function bigintAbs(value: bigint): bigint; /** TC39 `ToInternalDurationRecord`. */ export declare function toInternalDuration(duration: Temporal.Duration): InternalDuration; /** TC39 `DefaultTemporalLargestUnit`. */ export declare function defaultLargestUnit(duration: Temporal.Duration): Unit; /** TC39 `GetISODateTimeFor`. */ export declare function isoDateTimeFor(timeZone: string, epochNs: bigint): Temporal.PlainDateTime; /** * TC39 `CombineISODateAndTimeRecord`. A combination outside the PlainDateTime limits throws here; * the spec throws for it at the `GetEpochNanosecondsFor` that always follows. */ export declare function atTimeOf(date: Temporal.PlainDate, wall: Temporal.PlainDateTime): Temporal.PlainDateTime; /** TC39 `CalendarDateUntil`, through the Temporal compat layer. */ export declare function calendarDateUntil(calendar: string, one: Temporal.PlainDate, two: Temporal.PlainDate, largestUnit: DateUnit): DateDuration; /** TC39 `TemporalDurationFromInternal` for a time `largestUnit`, balanced by `Instant#until`. */ export declare function temporalDurationFromInternal(duration: InternalDuration, largestUnit: TimeUnit): Temporal.Duration; /** TC39 `RoundRelativeDuration` with a time zone. */ export declare function roundRelativeDuration(duration: InternalDuration, originEpochNs: bigint, destEpochNs: bigint, context: RoundingContext, settings: RoundingSettings): InternalDuration; /** An ISO date far from the limits, used only to validate options. */ export declare const PLAIN_DATE_PROBE: Temporal.PlainDate; /** TC39 `IsCalendarUnit`. */ export declare function isCalendarUnit(unit: Unit): unit is "year" | "month" | "week"; export declare function plainContext(calendar: string, wall: Temporal.PlainDateTime): RoundingContext; /** TC39 `DifferencePlainDateTimeWithRounding`. */ export declare function differencePlainDateTimeWithRounding(one: Temporal.PlainDateTime, two: Temporal.PlainDateTime, calendar: string, settings: RoundingSettings): InternalDuration; /** TC39 `DateDurationDays`. */ export declare function dateDurationDays(date: DateDuration, plain: Temporal.PlainDate): number; /** Runs `validate` (a polyfill call far from the limits); null when the options are invalid. */ export declare function optionsAreValid(validate: () => unknown): boolean; /** TC39 `DifferenceTemporalZonedDateTime(~until~, …)` for a date `largestUnit`. */ export declare function untilAtLimit(one: Temporal.ZonedDateTime, two: Temporal.ZonedDateTime, options: ZonedDifferenceOptions, settings: RoundingSettings): Temporal.Duration | null; /** `Duration#total` with a zoned `relativeTo`: TC39 `DifferenceZonedDateTimeWithTotal`. */ export declare function totalAtLimit(duration: Temporal.Duration, zoned: Temporal.ZonedDateTime, unitOption: string, unit: Unit): number | null; /** `Duration#round` with a zoned `relativeTo`. */ export declare function roundAtLimit(duration: Temporal.Duration, zoned: Temporal.ZonedDateTime, roundTo: DurationRoundOptions, settings: RoundingSettings): Temporal.Duration | null; /** `Temporal.Duration.compare` with a zoned `relativeTo`, when either operand has date units. */ export declare function compareAtLimit(one: Temporal.Duration, two: Temporal.Duration, zoned: Temporal.ZonedDateTime): number | null; /** Resolves the units of `until`/`round` options; null when a unit name is not one Temporal knows. */ export declare function roundingSettings(largestUnitOption: unknown, smallestUnitOption: unknown, defaultLargestUnitFor: (smallestUnit: Unit) => Unit, increment: number | undefined, roundingMode: Temporal.RoundingMode): RoundingSettings | null; /** `Duration#total` with a plain `relativeTo`: TC39 `DifferencePlainDateTimeWithTotal`. */ export declare function totalWithPlainRelativeTo(duration: Temporal.Duration, plain: Temporal.PlainDate, unitOption: string, unit: Unit): number | null; /** `Duration#round` with a plain `relativeTo`: TC39 `DifferencePlainDateTimeWithRounding`. */ export declare function roundWithPlainRelativeTo(duration: Temporal.Duration, plain: Temporal.PlainDate, roundTo: DurationRoundOptions, settings: RoundingSettings): Temporal.Duration | null; export {};