# Calendar Boundaries and Zone-Aware Buckets

Industry: Core

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.

## Week and period identifiers

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`](/gmt/reference/calendar/calculate/getIsoWeekDate/),
[`getOrdinalDate`](/gmt/reference/calendar/calculate/getOrdinalDate/),
[`getQuarter`](/gmt/reference/calendar/calculate/getQuarter/) and
[`getFiscalPeriod`](/gmt/reference/calendar/calculate/getFiscalPeriod/).

```typescript
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:

```typescript
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.

## Zone-aware bucketing

"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`](/gmt/reference/calendar/calculate/floorToZone/) floors an instant to the start of
its calendar unit in a zone:

```typescript
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.

## Buckets are deliberately not uniform

[`bucketRange`](/gmt/reference/calendar/calculate/bucketRange/) returns the boundaries spanning a
range, and **the buckets are deliberately not uniform in length**:

```typescript
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.

## Flooring a zoned or epoch value

For a zoned or epoch value, [`startOfZoned`](/gmt/reference/zoned/calculate/startOfZoned/) and
[`startOfUnix`](/gmt/reference/unix/calculate/startOfUnix/) with no options return the same real
boundary.

Do not floor with [`roundZoned`](/gmt/reference/zoned/calculate/roundZoned/) or
[`roundUnix`](/gmt/reference/unix/calculate/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

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`](/gmt/reference/calendar/validate/isValidFiscalCalendar/),
[`isValidFiscalPattern`](/gmt/reference/calendar/validate/isValidFiscalPattern/) and
[`isValidZoneBucketUnit`](/gmt/reference/calendar/validate/isValidZoneBucketUnit/).

```typescript
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
```

## See also

- [Zoned Operations](../zoned-operations/): start, end and round of a zoned value
- [DST Disambiguation](/gmt/guides/concepts/dst-disambiguation/): why a day is sometimes 23 or 25 hours
- [Splitting and Counting](/gmt/guides/intervals/splitting-and-counting/): counting the same local buckets in an interval
