# Zoned Date Operations

Industry: Core

Zoned operations in GMT expose DST disambiguation, calendar annotations, and
timezone offset control that plain operations simply do not have. The mistakes
here cover the ways that extra power bites.

## Mistakes

### Writing the calendar annotation before the time zone

Severity: high.

A calendar-annotated zoned string is the RFC 9557 form Temporal.ZonedDateTime#toString() writes: the time zone annotation first, then [u-ca=...]. A calendar annotation before the zone is not RFC 9557, and convertZonedToCalendar and the other calendar-aware zoned functions reject it.

Wrong:
```js
convertZonedToCalendar("2024-03-15T10:00:00+09:00[u-ca=japanese][Asia/Tokyo]", "iso8601");
// "" — the calendar annotation comes after the zone
```

Right:
```js
import { convertZonedToCalendar } from "@northguild/gmt/zoned";

convertZonedToCalendar("2024-03-15T10:00:00+09:00[Asia/Tokyo][u-ca=japanese]", "iso8601");
// "2024-03-15T10:00:00+09:00[Asia/Tokyo]"
```

### Passing a calendar-annotated value to convertPlainDateTimeToZoned

Severity: high.

convertPlainDateTimeToZoned has no calendar grammar: a [u-ca=...] annotation makes the input invalid, and the result is an empty string. Convert the Gregorian wall time first, then express the zoned result in the calendar with convertZonedToCalendar.

Wrong:
```js
convertPlainDateTimeToZoned("2024-03-15T00:00:00[u-ca=japanese]", "Asia/Tokyo");
// "" — the annotation is invalid input here
```

Right:
```js
import { convertPlainDateTimeToZoned, convertZonedToCalendar } from "@northguild/gmt/zoned";

const z = convertPlainDateTimeToZoned("2024-03-15T00:00:00", "Asia/Tokyo");
// "2024-03-15T00:00:00.000+09:00[Asia/Tokyo]"
convertZonedToCalendar(z, "japanese");
// "2024-03-15T00:00:00+09:00[Asia/Tokyo][u-ca=japanese]"
```

### Assuming every zoned/ function accepts the annotation

Severity: high.

Only the calendar-aware subset — addZoned, subtractZoned, diffZoned, diffZonedAsDuration, convertZonedToCalendar and the zoned interval functions — accepts a calendar-annotated zoned string, and there calendar units resolve in that calendar. Every other zoned/ function treats the annotation as invalid input.

Wrong:
```js
startOfZoned("2024-03-15T10:00:00+09:00[Asia/Tokyo][u-ca=japanese]", "day");
// "" — startOfZoned does not accept the annotation
```

Right:
```js
import { addZoned } from "@northguild/gmt/zoned";

addZoned("2024-01-31T00:00:00+09:00[Asia/Tokyo][u-ca=japanese]", { months: 1 });
// "2024-02-29T00:00:00+09:00[Asia/Tokyo][u-ca=japanese]" — month arithmetic in the Japanese calendar
```

### Using offset instead of IANA timezone

Severity: high.

An offset like '+01:00' is a valid fixed zone, so GMT's zoned functions accept it, but it has no DST rules: it stays +01:00 all year and never changes to winter time. For a place, pass its IANA zone identifier, such as 'Europe/London'.

Wrong:
```js
getHoursInZonedDay("2024-03-31T12:00:00+01:00[+01:00]"); // 24 — a fixed offset never changes its clocks
```

Right:
```js
import { getHoursInZonedDay } from "@northguild/gmt/zoned";

getHoursInZonedDay("2024-03-31T12:00:00+01:00[Europe/London]"); // 23 — London moves to summer time that day
```

### Not validating timezone before use

Severity: high.

An invalid timezone string silently produces the invalid-input sentinel from every zoned function — an empty string, or null from those that return a number. Always run isValidTimeZone first.

Wrong:
```js
getZonedNow("not-a-zone"); // ""
```

Right:
```js
import { isValidTimeZone, getZonedNow } from "@northguild/gmt/zoned";

if (isValidTimeZone("Europe/London")) {
getZonedNow("Europe/London"); // safe
}
```

### Expecting addZoned to always move a DST-gap landing forward

Severity: high.

When the date part of the duration lands in a spring-forward gap, addZoned, subtractZoned and intervalFromDurationZoned resolve it with your disambiguation, as TC39's AddZonedDateTime resolves the intermediate date-time: 'compatible' and 'later' move it forward, 'earlier' back, and 'reject' returns an empty string. Before 1.16.0 the landing always moved forward. Pass 'earlier' only when an earlier result is what you want, and check for the sentinel when you pass 'reject'.

Wrong:
```js
addZoned("2024-03-30T01:30:00+00:00[Europe/London]", { days: 1 }, { disambiguation: "earlier" });
// assumed "2024-03-31T02:30:00+01:00[Europe/London]", but returns
// "2024-03-31T00:30:00+00:00[Europe/London]" — 01:30 on 31 March is in the gap, and "earlier" moves back
```

Right:
```js
const next = addZoned("2024-03-30T01:30:00+00:00[Europe/London]", { days: 1 }, { disambiguation: "reject" });
if (next === "") {
  // ask the user: the renewal time does not exist on 31 March
}
addZoned("2024-03-30T01:30:00+00:00[Europe/London]", { days: 1 });
// "2024-03-31T02:30:00+01:00[Europe/London]" — "compatible" moves forward
addZoned("2024-03-30T01:30:00+00:00[Europe/London]", { days: 1 }, { disambiguation: "earlier" });
// "2024-03-31T00:30:00+00:00[Europe/London]"
```

### Expecting disambiguation to run while offset: 'prefer' still fits

Severity: high.

The offset option ('prefer', 'use', 'ignore', 'reject') decides whether the input's own UTC offset is kept, and the disambiguation option ('compatible', 'earlier', 'later', 'reject') resolves a wall time the offset does not settle. setZoned defaults to 'prefer', as Temporal's ZonedDateTime#with does: an offset that is still valid settles the time, so disambiguation never runs. Pass offset: 'ignore' to make disambiguation decide.

Wrong:
```js
setZoned("2024-04-07T02:45:00+10:00[Australia/Sydney]", { minute: 0 }, {
  disambiguation: "reject"
}); // "2024-04-07T02:00:00+10:00[Australia/Sydney]" — +10:00 kept, reject never fired
```

Right:
```js
setZoned("2024-04-07T02:45:00+10:00[Australia/Sydney]", { minute: 0 }, {
  offset: "ignore",
  disambiguation: "reject"
}); // "" — the offset is ignored, so reject fires on the overlap
```

### Expecting disambiguation: 'reject' to throw

Severity: high.

GMT functions never throw: a rejected wall time returns an empty string. disambiguation: 'reject' fires whenever the wall time is not settled by the offset: always in a spring-forward gap, and in a fall-back overlap when you pass offset: 'ignore'. Check the result instead of wrapping the call in try/catch.

Wrong:
```js
try {
  setZoned("2024-03-09T02:30:00-05:00[America/New_York]", { day: 10 }, { disambiguation: "reject" });
} catch {
  // never reached — 02:30 on 10 March is in the gap, and the result is ""
}
```

Right:
```js
const r = setZoned("2024-03-09T02:30:00-05:00[America/New_York]", { day: 10 }, { disambiguation: "reject" }); // ""
if (r === "") {
  // invalid input, or a wall time in a gap or an overlap
}
```

### Confusing hasDaylightSaving, getDstTransitions, and isInDaylightSaving

Severity: medium.

hasDaylightSaving tells you whether a zone observes DST around a reference instant. getDstTransitions returns the transitions in one year. isInDaylightSaving tells you whether a specific instant is in DST. They answer three different questions.

Wrong:
```js
hasDaylightSaving("Europe/London", { at: "2024-01-15T12:00:00Z" }); // true — the zone observes DST
// used to check whether a specific date is in DST — wrong function
```

Right:
```js
import { isInDaylightSaving, getDstTransitions } from "@northguild/gmt/zoned";

isInDaylightSaving("2024-07-01T12:00:00+01:00[Europe/London]"); // true
getDstTransitions("Europe/London", 2024);
// [
//   { instant: "2024-03-31T01:00:00Z", offsetBefore: "+00:00", offsetAfter: "+01:00" },
//   { instant: "2024-10-27T01:00:00Z", offsetBefore: "+01:00", offsetAfter: "+00:00" },
// ]
```

### Reaching for cycleZoned when calendar arithmetic is wanted (or vice versa)

Severity: medium.

cycleZoned moves one field around its own range and wraps without carrying into the next field. addZoned/subtractZoned with calendar units carry across month and year boundaries. They are not interchangeable.

Wrong:
```js
cycleZoned("2024-12-15T00:00:00+11:00[Australia/Sydney]", "month", 1);
// "2024-01-15T00:00:00+11:00[Australia/Sydney]" — wraps within the year
```

Right:
```js
import { addZoned } from "@northguild/gmt/zoned";

addZoned("2024-12-15T00:00:00+11:00[Australia/Sydney]", { months: 1 });
// "2025-01-15T00:00:00+11:00[Australia/Sydney]" — calendar arithmetic
```

## See also

- [DST Disambiguation guide](/gmt/guides/concepts/dst-disambiguation/)
- [Zoned Operations guide](/gmt/guides/zoned-date-operations/zoned-operations/)
- [`isValidTimeZone` reference](/gmt/reference/zoned/validate/isValidTimeZone/)
- [`addZoned` reference](/gmt/reference/zoned/calculate/addZoned/)
- [`convertZonedToCalendar` reference](/gmt/reference/zoned/convert/convertZonedToCalendar/)
- [`getDstTransitions` reference](/gmt/reference/zoned/get/getDstTransitions/)
- [`setZoned` reference](/gmt/reference/zoned/calculate/setZoned/)
- [`isInDaylightSaving` reference](/gmt/reference/zoned/compare/isInDaylightSaving/)
- [`hasDaylightSaving` reference](/gmt/reference/zoned/validate/hasDaylightSaving/)
- [`cycleZoned` reference](/gmt/reference/zoned/calculate/cycleZoned/)
