Skip to content

Calendar Boundaries and Zone-Aware Buckets

  • The core functions, which every industry uses.

Two absences with outsized consequences turn out to be the same absence twice. A calendar boundary is not a fixed number of hours, and it is not in UTC.

Vessel schedules are published by week number, and retail runs on 52/53-week fiscal calendars. Both come back as whole identifiers, not as fields to combine by hand. A week number without its week-numbering year is ambiguous at both ends of a year.

The functions are getIsoWeekDate, getOrdinalDate, getQuarter and getFiscalPeriod.

import { getFiscalPeriod, getIsoWeekDate, getOrdinalDate, getQuarter } from "@northguild/gmt";
getIsoWeekDate("2027-01-01"); // { year: 2026, week: 53, weekday: 5 } — week-year 2026, not 2027
getOrdinalDate("2024-12-31"); // { year: 2024, dayOfYear: 366 }
getQuarter("2024-03-31", { fiscalYearStartMonth: 4, yearLabel: "startYear" }); // { year: 2023, quarter: 4 }
// The NRF 4-5-4 calendar (US) adds a 53rd week when "there are four or more days left in
// January", at the end of the year, and names each year by the year it starts in.
const nrf = { pattern: "4-5-4", yearEndsOn: "2026-01-31", extraWeek: "lastPeriod", yearLabel: "startYear" } as const;
getFiscalPeriod("2024-06-15", nrf); // { year: 2024, period: 5, week: 19 }
getFiscalPeriod("2024-01-28", nrf); // { year: 2023, period: 12, week: 53 } — a 53-week year

A 52-week year is 52 × 7 = 364 days, so a 53rd week is inserted every five or six years and lands in the period extraWeek names.

Both extraWeek and yearLabel are required, because published calendars disagree on where the 53rd week goes and how the year is named. ONS retail trading periods (UK) put it in the first period and name the year by the calendar year holding most of its days:

const ons = { pattern: "4-4-5", yearEndsOn: "2022-12-31", extraWeek: "firstPeriod", yearLabel: "majorityYear" } as const;
getFiscalPeriod("2024-12-29", ons); // { year: 2025, period: 1, week: 1 } — ONS 2025 opens in December 2024
getFiscalPeriod("2025-02-01", ons); // { year: 2025, period: 1, week: 5 } — a five-week first period

yearEndsOn states the rule, by example, not one year’s end. Years end on that date’s weekday, nearest that date’s month and day. The library bundles no fiscal calendar, because there is no single correct retail calendar to bundle.

“Group by day in America/Sao_Paulo” over UTC timestamps is the most common observability bug there is. The same operation decides how many chargeable days a container accrued, because free time is counted in terminal-local calendar days.

floorToZone floors an instant to the start of its calendar unit in a zone:

import { bucketRange, floorToZone } from "@northguild/gmt";
floorToZone("2024-06-15T02:00:00Z", "day", "America/Sao_Paulo"); // "2024-06-14T03:00:00Z"
floorToZone("2024-06-15T02:00:00Z", "day", "UTC"); // "2024-06-15T00:00:00Z"

It is the same instant on a different calendar day. 02:00 UTC on 15 June is still 14 June in São Paulo, and flooring it to the UTC day is wrong for most of the world for most of the day.

The zone is an argument because the library has no ambient one and must not acquire one.

bucketRange returns the boundaries spanning a range, and the buckets are deliberately not uniform in length:

bucketRange("2024-10-04T14:00:00Z", "2024-10-07T13:00:00Z", "day", "Australia/Sydney");
// ["2024-10-04T14:00:00Z", "2024-10-05T14:00:00Z", "2024-10-06T13:00:00Z"]
// 24h, then 23h — the second day springs forward
bucketRange("2024-10-25T23:00:00Z", "2024-10-29T00:00:00Z", "day", "Europe/London");
// ["2024-10-25T23:00:00Z", "2024-10-26T23:00:00Z", "2024-10-28T00:00:00Z"]
// 24h, then 25h — the second day falls back

Forcing 24 hours here is what makes a daily aggregate drift an hour twice a year.

The same honesty applies to boundaries that do not exist:

  • The calendar day Samoa deleted crossing the date line is absent from the list.
  • A local day whose midnight is skipped by a spring-forward starts at 01:00.
  • In a zone that falls back by half an hour (Australia/Lord_Howe), the local 01:00 hour bucket is genuinely 90 minutes long.

For a zoned or epoch value, startOfZoned and startOfUnix with no options return the same real boundary.

Do not floor with roundZoned or roundUnix and roundingMode: "trunc". They follow TC39 ZonedDateTime.round, which rounds the wall clock and re-resolves it in the zone. So "2024-09-29T03:50:00+13:45[Pacific/Chatham]" truncated to the hour gives 04:00, which is after the input.

Narrow a calendar, pattern or unit from config

Section titled “Narrow a calendar, pattern or unit from config”

A fiscal calendar, a pattern or a bucket unit the namespace does not recognise returns the same sentinel an unusable date or instant does. Each has a type guard, to narrow one that arrived from config, an env var or a form: isValidFiscalCalendar, isValidFiscalPattern and isValidZoneBucketUnit.

import { isValidFiscalCalendar, isValidFiscalPattern, isValidZoneBucketUnit } from "@northguild/gmt";
isValidFiscalCalendar({ pattern: "4-5-4", yearEndsOn: "2026-01-31", extraWeek: "lastPeriod", yearLabel: "startYear" }); // true
isValidFiscalCalendar({ pattern: "4-5-4", yearEndsOn: "2026-01-31" }); // false — no extraWeek or yearLabel
isValidFiscalPattern("4-5-4"); // true — the NRF retail calendar
isValidFiscalPattern("4-5-5"); // false — a 53-week year's last quarter under `lastPeriod`
isValidZoneBucketUnit("day"); // true
isValidZoneBucketUnit("days"); // true — singular or plural, as floorToZone accepts
isValidZoneBucketUnit("year"); // false — the year question is getQuarter's
Accessibility
© 2026 NorthGuild · Licences

Code examples: MIT-0. Packages: MIT. Third-party data and components: their own licences, credited where they appear. Everything else: © 2026 NorthGuild, all rights reserved.