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.

72 lines • 4.32 kB
import { Temporal } from "./temporal.js"; import { FiscalExtraWeek, FiscalPattern, FiscalYearLabel } from "../types/fiscal-calendar.js"; import "../types/index.js"; /** * Return the multiple of 400 years that moves `year` into 2000 to 2399, where it keeps its * place in the Gregorian cycle. * * Subtract the result to move a year into that cycle, and add it to move it back. Calendar * arithmetic near Temporal's first or last date (-271821-04-19, +275760-09-13) can step past * the range on the way to an answer that lies inside it; done in the safe cycle it cannot. * * @example gregorianCycleShift(2024) // 0 * @example gregorianCycleShift(-271820) // -274000 (-271820 sits where 2180 does) * @example gregorianCycleShift(275760) // 273600 (275760 sits where 2160 does) */ export declare function gregorianCycleShift(year: number): number; /** * Return the fiscal year end falling in calendar year `year`, under the rule `anchor` states. * * The rule is "the <anchor's weekday> nearest to <anchor's month and day>", so the result is * `anchor`'s month and day in `year`, shifted at most three days either way onto `anchor`'s * weekday. A year end within three days of January 1 lands in the previous calendar year, * which is why callers must track the calendar-year argument rather than read `.year` back * off the result. * * - The month-day is moved into `year` with Temporal's `overflow: "constrain"`, so a * February 29 anchor targets February 28 in a common year before the weekday shift. A rule * tied to the end of a month ("the last Saturday of February", or the Saturday nearest the * month's last day) would instead put a February 28 anchor on February 29 in a leap year; * this does not. */ export declare function fiscalYearEndIn(anchor: Temporal.PlainDate, year: number): Temporal.PlainDate; /** * Locate the fiscal year containing `date` under the rule `anchor` states. * * - The fiscal year is the one whose end is the first at or after `date`. * - `weeks` is derived from the gap between consecutive year ends — 52 or 53 — never assumed. * - The search reads the year ends in the calendar years either side of `date`, so it throws * RangeError when one of them lies outside Temporal's range. Near -271821-04-19 or * +275760-09-13 that happens even when the date is valid. Move `date` into 2000 to 2399 with * `gregorianCycleShift` first, then add the shift back to any year read off the result: * whole 400-year cycles keep every month, day, weekday and day count. * - Returns null when the gap between year ends is neither 364 nor 371 days, which no * "nearest weekday" rule can produce and so only a corrupted anchor could reach. */ export declare function fiscalYearOf(date: Temporal.PlainDate, anchor: Temporal.PlainDate): { start: Temporal.PlainDate; weeks: number; } | null; /** * Return the 1-based period holding `week` of a `weeks`-long fiscal year under `pattern`. * * - The 53rd week, when the year has one, lengthens period 1 (`"firstPeriod"`) or period 12 * (`"lastPeriod"`), as `extraWeek` says. A 4-4-5 year then runs 5-4-5 … 4-4-5 or * 4-4-5 … 4-4-6. * - In a 52-week year `extraWeek` has no effect. */ export declare function fiscalPeriodOfWeek(week: number, weeks: number, pattern: FiscalPattern, extraWeek: FiscalExtraWeek): number; /** * Return the calendar year that names the fiscal year running `start` to `end`, both inclusive. * * - `"startYear"` is `start`'s year and `"endYear"` is `end`'s year. * - `"majorityYear"` is the calendar year holding the most of the fiscal year's days. Days are * the unit because the fiscal year and the calendar year share no other: months differ in * length, and a week can straddle 1 January. * - Every calendar year in the span is counted, not only the two end years. A 371-day year can * touch three calendar years (2024-12-29 to 2026-01-03), and then the middle one wins. * - GMT rule: when two years hold exactly half the days each (182 and 182), the later one, the * year holding the end date, wins, because IFRS 18 ¶27(c) (formerly IAS 1 ¶51(c)) lets a * reporting period be identified by the date it ends. */ export declare function fiscalYearLabel(start: Temporal.PlainDate, end: Temporal.PlainDate, label: FiscalYearLabel): number;