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.

151 lines • 8.32 kB
import { Temporal } from "./temporal.js"; import { Disambiguation } from "../types/disambiguation.js"; import { Offset } from "../types/offset.js"; import { Overflow } from "../types/overflow.js"; import "../types/index.js"; import { DATE_TIME_FIELDS, SetFields } from "./readSetFields.js"; type RoundingSmallestUnit = Temporal.SmallestUnit<"day" | "hour" | "minute" | "second" | "millisecond" | "microsecond" | "nanosecond">; interface ZonedRoundTo { smallestUnit: RoundingSmallestUnit; roundingIncrement?: number; roundingMode?: Temporal.RoundingMode; } /** * `PlainDate#toZonedDateTime(timeZone)` (start of day) or * `PlainDateTime#toZonedDateTime(timeZone, { disambiguation })`, correct at the range limits and * (start of day) before 1847. * * @param plain the date (resolved to its start of day) or date-time to place in `timeZone` * @param timeZone IANA identifier, offset time zone, or RFC 9557 string carrying one * @param disambiguation Temporal disambiguation for a PlainDateTime (ignored for a PlainDate) * @returns the resolved Temporal.ZonedDateTime; throws where TC39 Temporal throws */ export declare function plainToZoned(plain: Temporal.PlainDate | Temporal.PlainDateTime, timeZone: string, disambiguation?: Disambiguation): Temporal.ZonedDateTime; /** * `ZonedDateTime#startOfDay`, correct at the range limits and before 1847. * * @param zoned the value whose local day to start * @returns the first valid wall-clock time of that day; throws where TC39 Temporal throws */ export declare function zonedStartOfDay(zoned: Temporal.ZonedDateTime): Temporal.ZonedDateTime; /** * `ZonedDateTime#withPlainTime`, correct at the range limits and (start of day) before 1847. * * @param zoned the value whose local date to keep * @param time the new wall-clock time; omitted means the start of the day * @returns the resolved Temporal.ZonedDateTime; throws where TC39 Temporal throws */ export declare function zonedWithPlainTime(zoned: Temporal.ZonedDateTime, time?: Temporal.PlainTime | Temporal.PlainTimeLike | string): Temporal.ZonedDateTime; /** * `ZonedDateTime#hoursInDay`, correct at the range limits and before 1847. * * @param zoned the value whose local day to measure * @returns hours from this day's start to the next day's start; throws where TC39 Temporal throws */ export declare function zonedHoursInDay(zoned: Temporal.ZonedDateTime): number; /** * `ZonedDateTime#getTimeZoneTransition("next")`: TC39 `GetNamedTimeZoneNextTransition`, the first * offset change strictly after `zoned`. * * - While the D13 probe fails (two changes inside one of the polyfill's 14-day search steps are * skipped, or stall it for ever), a named zone is searched by GMT's own bounded search * (`zonedTransitionSearch.ts`) and the polyfill's search is never called. * - Otherwise the polyfill answers, corrected before 1847 (defect 3: it never searches there, so * the change is found by bisection first) and within its three-year horizon of the maximum * instant (defect 2: its forward scan gives up once a step would pass the maximum, so a `null` * is re-checked by walking back from the maximum). * * @param zoned the instant to search after (exclusive) * @param until optional: the last instant to search, in epoch nanoseconds (inclusive). A caller * that needs only changes up to there passes it so the search reads no further * @returns the next transition, or null when there is none up to `until` or, without it, none * that Temporal reports */ export declare function zonedNextTransition(zoned: Temporal.ZonedDateTime, until?: bigint): Temporal.ZonedDateTime | null; /** * `ZonedDateTime#getTimeZoneTransition("previous")`: TC39 `GetNamedTimeZonePreviousTransition`, * the last offset change strictly before `zoned`. * * - While the D13 probe fails, a named zone is searched by GMT's own bounded search and the * polyfill's search is never called (see `zonedNextTransition`). * - Otherwise the polyfill answers, corrected before 1847 (defect 3): when it finds nothing in a * named zone, the span from the minimum instant to its floor is checked by comparing offsets * and bisecting. * * @param zoned the instant to search before (exclusive) * @param since optional: where the search stops, in epoch nanoseconds (exclusive). A caller that * needs only changes after there passes it so the search reads no further back * @returns the previous transition, or null when there is none after `since` or, without it, none */ export declare function zonedPreviousTransition(zoned: Temporal.ZonedDateTime, since?: bigint): Temporal.ZonedDateTime | null; /** * `ZonedDateTime#add`, correct at the range limits and in calendars the polyfill gets wrong. * * @param zoned the starting value * @param duration the duration to add * @param options Temporal `overflow` for the calendar part * @returns the sum; throws where TC39 Temporal throws */ export declare function addToZoned(zoned: Temporal.ZonedDateTime, duration: Temporal.Duration | Temporal.DurationLike | string, options?: { overflow?: Overflow; }): Temporal.ZonedDateTime; /** * `ZonedDateTime#subtract`, correct at the range limits. * * @param zoned the starting value * @param duration the duration to subtract * @param options Temporal `overflow` for the calendar part * @returns the difference; throws where TC39 Temporal throws */ export declare function subtractFromZoned(zoned: Temporal.ZonedDateTime, duration: Temporal.Duration | Temporal.DurationLike | string, options?: { overflow?: Overflow; }): Temporal.ZonedDateTime; /** * `addToZoned` / `subtractFromZoned` with a caller-chosen `disambiguation` for the intermediate * wall-clock date-time — TC39 Temporal §6.5.5 AddZonedDateTime with the "compatible" argument of * its `GetEpochNanosecondsFor` step replaced by `disambiguation`. * * - The date portion (years, months, weeks, days) is added to the wall-clock date, keeping the * wall-clock time. That intermediate date-time is resolved with `disambiguation` whenever it is * not a single instant: in a fall-back overlap "earlier"/"compatible" take the earlier instant and * "later" the later one; in a spring-forward gap "earlier" shifts it back by the gap length and * "later"/"compatible" forward (DisambiguatePossibleEpochNanoseconds); "reject" throws for both. * - The time portion is then added in exact time and is never re-resolved. * - With no date portion, the whole duration is exact time and `disambiguation` never applies. * - Each option is converted to a string once, as Temporal GetOption converts it. * * @param zoned the starting value * @param duration the duration to add or subtract * @param sign 1 to add, -1 to subtract * @param options Temporal `overflow` for the calendar part and `disambiguation` * @returns the result; throws where TC39 Temporal throws, or when "reject" meets a gap or overlap */ export declare function addToZonedDisambiguated(zoned: Temporal.ZonedDateTime, duration: Temporal.Duration | Temporal.DurationLike | string, sign: 1 | -1, options: { overflow?: Overflow; disambiguation: Disambiguation; }): Temporal.ZonedDateTime; interface ZonedWithOptions { overflow?: Overflow; disambiguation?: Disambiguation; offset?: Offset; } /** The date and time fields a `set*` function passes, as `readSetFields` checked them. */ type ZonedWithFields = SetFields<(typeof DATE_TIME_FIELDS)[number]>; /** * `ZonedDateTime#with`, correct at the range limits. * * @param zoned the value to change * @param fields the date and time fields to set * @param options Temporal `overflow` / `disambiguation` / `offset` (default `"prefer"`) * @returns the changed value; throws where TC39 Temporal throws */ export declare function withZonedFields(zoned: Temporal.ZonedDateTime, fields: ZonedWithFields, options?: ZonedWithOptions): Temporal.ZonedDateTime; /** * `ZonedDateTime#round`, correct at the range limits and, by day, before 1847. * * @param zoned the value to round * @param roundTo Temporal `smallestUnit` (day or smaller) / `roundingIncrement` / `roundingMode` * @returns the rounded value; throws where TC39 Temporal throws */ export declare function roundZonedDateTime(zoned: Temporal.ZonedDateTime, options: ZonedRoundTo): Temporal.ZonedDateTime; export {};