@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.
59 lines • 3.05 kB
TypeScript
import { Temporal } from "../temporal.js";
/** A date's fields in one calendar. `month` is ordinal. */
export interface CalendarDateParts {
year: number;
month: number;
monthCode: string;
day: number;
}
export interface YearMonth {
year: number;
month: number;
}
export interface DateDurationFields {
years: number;
months: number;
weeks: number;
days: number;
}
export type ArithmeticOverflow = "constrain" | "reject";
/**
* Integer questions about one calendar's months. `null` answers mean "this month lies outside the
* TC39 PlainDate range, so nothing about it can be read": the algorithms only ask about such a month
* when the date they would build is out of range too, which the spec turns into a RangeError or, in
* `NonISODateSurpasses`, into "surpasses".
*/
export interface ArithmeticModel {
/** Temporal calendar id. */
calendarId: string;
fields(date: Temporal.PlainDate): CalendarDateParts;
/** Ordinal of `monthCode` in `year`; `"missing"` when that year has no such month (a leap month). */
monthOrdinal(year: number, monthCode: string): number | "missing" | null;
/** `BalanceNonISODate(year, month + months, 1)`: `month` may exceed the year's month count. */
addMonths(start: YearMonth, months: number): YearMonth | null;
/** Signed month count between two month starts (an estimate is enough: callers adjust). */
monthsBetween(from: YearMonth, to: YearMonth): number;
daysInMonth(month: YearMonth): number | null;
/** The ISO-calendared date of these fields; throws RangeError when they are invalid or out of range. */
toDate(year: number, month: number, day: number): Temporal.PlainDate;
}
/** `ParseMonthCode` ordering: `M05 < M05L < M06` (grammar, not calendar data). */
export declare function compareMonthCodes(a: string, b: string): number;
/**
* `NonISODateAdd(calendar, date, duration, overflow)`. Weeks and days are ISO day arithmetic, as in
* the spec's final `BalanceNonISODate` + `CalendarIntegersToISO`.
*
* @returns the result as an ISO-calendared PlainDate; throws RangeError out of range or on reject
*/
export declare function nonIsoDateAdd(model: ArithmeticModel, date: Temporal.PlainDate, duration: DateDurationFields, overflow: ArithmeticOverflow): Temporal.PlainDate;
/**
* `NonISODateUntil(calendar, one, two, largestUnit)` for `largestUnit` year or month (days and
* weeks are calendar-independent: callers use ISO `until`).
*/
export declare function nonIsoDateUntil(model: ArithmeticModel, one: Temporal.PlainDate, two: Temporal.PlainDate, largestUnit: "year" | "month"): DateDurationFields;
/**
* True when `result` is exactly `NonISODateUntil(one, two, largestUnit)`: it does not surpass, its
* signs agree, and adding one more of each unit it could hold does surpass (so no unit is short).
* The per-call guard that keeps a correct polyfill answer.
*/
export declare function isNonIsoDateUntilResult(model: ArithmeticModel, one: Temporal.PlainDate, two: Temporal.PlainDate, largestUnit: "year" | "month", result: DateDurationFields): boolean;