# Business Calendars and Roll Conventions

Industry: Core

A business day answers "is this a working day here". The answer depends on the weekend and
on the holidays of one place, so every function in this family takes a `BusinessCalendar`
that states both. This guide covers the calendar, the day counting and stepping functions,
the roll conventions, and how to merge calendars when a date has to work in two places at
once. For the first look at weekdays and business-day arithmetic, see
[Comparisons](../comparisons/) and
[Plain Arithmetic](../plain-arithmetic/).

## The calendar

A `BusinessCalendar` is `{ weekend: number[], holidays: string[], timeZone: string }`.

- **The weekend is explicit.** It is a list of ISO weekday numbers, because Saturday and
  Sunday are not universal. Much of the Middle East is Friday and Saturday, and some
  markets keep a one-day weekend.
- **Holidays are yours to supply.** The library bundles no holiday table, because holiday
  data belongs to a jurisdiction and changes every year, sometimes with days of notice.
  [`buildBusinessCalendar`](/gmt/reference/calendar/rules/buildBusinessCalendar/) builds a
  calendar from the rules you supply (see [Build a calendar from rules](#build-a-calendar-from-rules)).
- **`timeZone` records which locality the calendar describes.** The business-day functions
  take and return local dates and never read it. If you hold an instant, reduce it to a
  local date with [`floorToZone`](/gmt/reference/calendar/calculate/floorToZone/) first.

## Count and step

[`isBusinessDay`](/gmt/reference/plain/compare/isBusinessDay/),
[`addBusinessDays`](/gmt/reference/plain/calculate/addBusinessDays/) and
[`subtractBusinessDays`](/gmt/reference/plain/calculate/subtractBusinessDays/) all take a
calendar as an optional trailing argument. The rest of the family requires one, because
there is no default weekend to fall back on:
[`businessDaysBetween`](/gmt/reference/calendar/business/businessDaysBetween/),
[`nextBusinessDay`](/gmt/reference/calendar/business/nextBusinessDay/),
[`previousBusinessDay`](/gmt/reference/calendar/business/previousBusinessDay/),
[`rollDate`](/gmt/reference/calendar/business/rollDate/) and
[`mergeCalendars`](/gmt/reference/calendar/business/mergeCalendars/).

`businessDaysBetween` counts with the start exclusive and the end inclusive.
`nextBusinessDay` and `previousBusinessDay` are strict: they never return the date you
gave them.

```typescript
import {
  businessDaysBetween,
  nextBusinessDay,
  previousBusinessDay,
  rollDate,
  mergeCalendars,
  isValidBusinessCalendar,
  isBusinessDay,
} from "@northguild/gmt";

const london = {
  weekend: [6, 7],
  holidays: ["2024-05-31", "2024-12-25"],
  timeZone: "Europe/London",
};

businessDaysBetween("2024-12-23", "2024-12-27", london);
// 3 — start exclusive, end inclusive, and 25 December is a holiday

nextBusinessDay("2024-12-24", london);
// "2024-12-26" — strictly after, skipping the holiday

previousBusinessDay("2024-12-26", london);
// "2024-12-24"
```

## Roll a date onto a working day

[`rollDate`](/gmt/reference/calendar/business/rollDate/) moves a date onto a working day by an
explicit convention. The six conventions are `following`, `modifiedFollowing`,
`preceding`, `modifiedPreceding`, `endOfMonth` and `none`.

`following` and `preceding` mean on-or-after and on-or-before, so they leave a working day
alone. `nextBusinessDay` and `previousBusinessDay` are the strict neighbours.

```typescript
rollDate("2024-05-31", "following", london);
// "2024-06-03" — forward past the weekend

rollDate("2024-05-31", "modifiedFollowing", london);
// "2024-05-30" — backward instead, because forward leaves May

rollDate("2024-03-15", "endOfMonth", london);
// "2024-03-29" — March's last working day; March ends on a Sunday

rollDate("2024-06-01", "none", london);
// "2024-06-01" — unadjusted, Saturday or not
```

## Merge calendars across jurisdictions

[`mergeCalendars`](/gmt/reference/calendar/business/mergeCalendars/) composes jurisdictions.
Weekend rules and holidays union, so a date survives only if it is a working day in
**every** input. That is the two-currency intersection that FX settlement needs, and the
two-port one that an intermodal move needs.

The merged `timeZone` is the first calendar's, because the merged set spans localities
that may disagree. An empty list has no calendar to return, so `mergeCalendars([])` is
`null`.

```typescript
const tokyo = { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Asia/Tokyo" };

const both = mergeCalendars([london, tokyo]);
// { weekend: [6, 7], holidays: ["2024-05-31", "2024-07-04", "2024-12-25"], timeZone: "Europe/London" }
// timeZone is the first calendar's; the merged set spans localities that may disagree

isBusinessDay("2024-07-04", both);
// false — a holiday of either calendar closes the merged one

mergeCalendars([]);
// null
```

## Build a calendar from rules

A holiday list is a set of dates, but a holiday is a rule: "1 January", "the second Monday
of October", "two days before Easter". [`buildBusinessCalendar`](/gmt/reference/calendar/rules/buildBusinessCalendar/)
takes a zone, a weekend, a list of rules and the years to expand them over, and returns a
`BusinessCalendar`. It returns `null` when any part is invalid. A [`HolidayRule`](/gmt/reference/types/HolidayRule/)
has one of five kinds:

- `fixed`: a month and a day in the ISO calendar.
- `calendarFixed`: a month code and a day in another calendar, such as `M01` in the Hebrew calendar.
  The day must be one the month has in some year. A year whose month is shorter has no date.
- `nthWeekday`: the first to fifth or last weekday of a month.
- `easterRelative`: a number of days before or after Easter, by the Gregorian or the Julian method.
- `dates`: a list of dates you give, for a day that no rule produces.

A `fixed` or `calendarFixed` rule can also carry an `observed` policy: for each weekday it
names where the holiday moves, to a given weekday before or after, or to the next working
day. **Observance has no default.** A rule with no policy closes its own date, and the
library never fills one in for you.

A `"nextWorkingDay"` shift walks past every closed day: the weekend, every holiday that
does not walk, and the walks of the rules above it. Two holidays on a Saturday and a
Sunday, each moved to the next working day, close Monday and then Tuesday, because the
second one finds Monday already closed. The order of the rules decides only which holiday
takes which day; the days that close are the same in any order.

```typescript
import { buildBusinessCalendar } from "@northguild/gmt";

const nextWorking = { 6: "nextWorkingDay", 7: "nextWorkingDay" } as const;

buildBusinessCalendar({
  timeZone: "UTC",
  weekend: [6, 7],
  years: { from: 2021, to: 2021 },
  rules: [
    { name: "a", kind: "fixed", month: 12, day: 25, observed: nextWorking },
    { name: "b", kind: "fixed", month: 12, day: 26, observed: nextWorking },
  ],
})?.holidays;
// ["2021-12-27", "2021-12-28"] — the second shift passes the first, in either order
```

**A moved holiday closes one day.** The calendar closes the observed date, not the date the
holiday started on. To close both days, list the rule twice, once with no policy:

```typescript
buildBusinessCalendar({
  timeZone: "UTC",
  weekend: [6, 7],
  years: { from: 2021, to: 2021 },
  rules: [
    { name: "a", kind: "fixed", month: 12, day: 25, observed: nextWorking },
    { name: "a", kind: "fixed", month: 12, day: 25 },
  ],
})?.holidays;
// ["2021-12-25", "2021-12-27"] — the moved day and the day itself
```

Three tools draw the same calls on a calendar:

- [Holiday Rule Builder](/gmt/tools/holiday-rule-builder/): write the rules and see which rule closed which day.
- [Observance Comparator](/gmt/tools/observance-comparator/): put one holiday under several policies side by side.
- [Moving Feasts](/gmt/tools/moving-feasts/): the Gregorian and Julian Easter, and the n-th weekday of a month.

## Tell a bad calendar from bad input

[`isValidBusinessCalendar`](/gmt/reference/calendar/validate/isValidBusinessCalendar/) and
[`isValidRollConvention`](/gmt/reference/calendar/validate/isValidRollConvention/) narrow a
candidate. When a function returns its sentinel, they let you tell a misconfigured
calendar or contract term apart from bad date input.

## See also

- [Comparisons](../comparisons/): weekends, business days and now-relative predicates
- [Plain Arithmetic](../plain-arithmetic/): adding and subtracting business days
- [Operating Hours and Open-Time SLAs](../operating-hours/): hours of the day, not only days
- [Holiday Rule Builder](/gmt/tools/holiday-rule-builder/): holidays as rules, and which day each one closes
- [Observance Comparator](/gmt/tools/observance-comparator/): where one holiday is observed under different policies
- [Moving Feasts](/gmt/tools/moving-feasts/): Easter by two methods, and the n-th weekday of a month
