# Offset-Preserving Instants and Local Resolution

Industry: Core

An instant orders events globally. The UTC offset in force where the event happened renders
it as the human on the ground saw it. Neither derives from the other, which is why GS1 EPCIS
2.0 requires both fields (`eventTime` plus `eventTimeZoneOffset`). UN/EDIFACT DTM has format
codes `205` to `208` for the pair (a date and time with a `±HHMM` offset), and DICOM appends
`&ZZXX` to a `DT` value.

The `instant/` namespace is that pair:
[`toOffsetInstant`](/gmt/reference/instant/convert/toOffsetInstant/) and
[`fromOffsetInstant`](/gmt/reference/instant/convert/fromOffsetInstant/).

```typescript
import { fromOffsetInstant, toOffsetInstant } from "@northguild/gmt";

toOffsetInstant("2024-07-15T12:00:00+10:00[Australia/Sydney]");
// { instant: "2024-07-15T02:00:00Z", offset: "+10:00", timeZone: "Australia/Sydney" }

// Most feeds send no zone at all — the offset alone is what happened.
toOffsetInstant("2024-07-15T12:00:00+10:00");
// { instant: "2024-07-15T02:00:00Z", offset: "+10:00" }

// A UTC-only feed plus a zone you know from somewhere else.
toOffsetInstant("2024-07-15T02:00:00Z", "Australia/Sydney");
// { instant: "2024-07-15T02:00:00Z", offset: "+10:00", timeZone: "Australia/Sydney" }

fromOffsetInstant({ instant: "2024-07-15T02:00:00Z", offset: "+10:00" });
// "2024-07-15T12:00:00+10:00"
```

## An offset is not a zone

`+10:00` does not identify `Australia/Sydney`. It is every zone sitting at `+10:00` that
day, and it says nothing about what that zone will do next spring.

**Store the zone for anything still to be scheduled. Store the offset for anything that
already happened.**

`timeZone` is optional because most feeds do not send one. A string whose offset contradicts
its own bracketed zone returns `null` rather than a guess.

## Validate the offset

[`isValidUtcOffset`](/gmt/reference/instant/validate/isValidUtcOffset/) checks the `offset` half
of the pair: `±HH:MM`, or `±HH:MM:SS` for a zone that was not on a whole minute. A function
whose result has no zone in it takes this offset in its time zone position too. A function
that returns a zoned string, or formats with `Intl`, takes a time zone identifier only: check
that with [`isValidTimeZone`](/gmt/reference/zoned/validate/isValidTimeZone/).

```typescript
import { isValidTimeZone, isValidUtcOffset } from "@northguild/gmt";

isValidUtcOffset("+10:00"); // true
isValidUtcOffset("-00:44:30"); // true (Africa/Monrovia before 1972)
isValidUtcOffset("Z"); // false (a designator, not an offset)
isValidUtcOffset("+0530"); // false (no colon)

isValidTimeZone("-00:44:30"); // false (an offset with seconds is not a time zone identifier)
isValidTimeZone("+0530"); // true (an offset to the minute is one)
```

## Resolve a wall time that has no offset

The other half of the namespace is the reverse problem: a wall time that arrives with no
offset at all ("gate-out 08:00"). Resolving one needs a zone the sender did not send, plus a
policy for the two days a year the mapping is not one-to-one.
[`classifyLocal`](/gmt/reference/instant/convert/classifyLocal/) and
[`resolveLocal`](/gmt/reference/instant/convert/resolveLocal/) do it:

```typescript
import { classifyLocal, resolveLocal } from "@northguild/gmt";

classifyLocal("2024-10-27T01:30:00", "Europe/London"); // "ambiguous"
classifyLocal("2024-03-31T01:30:00", "Europe/London"); // "nonexistent"
classifyLocal("2024-07-15T12:00:00", "Europe/London"); // "unique"

resolveLocal("2024-10-27T01:30:00", "Europe/London");
// "2024-10-27T00:30:00Z" — the default "compatible" takes the first 01:30

resolveLocal("2024-10-27T01:30:00", "Europe/London", { disambiguation: "later" });
// "2024-10-27T01:30:00Z" — the same wall clock, an hour of real time later

resolveLocal("2024-10-27T01:30:00", "Europe/London", { disambiguation: "reject" });
// "" — ambiguous, and not guessed
```

`classifyLocal` exists so code can branch **before** a policy is applied. A demurrage clock,
a medication window or a duty limit should refuse an ambiguous wall time, or route it to a
human. It should not silently accept whichever of two instants an hour apart a default
handed it. See [DST Disambiguation](/gmt/guides/concepts/dst-disambiguation/) for the four
strategies in full.

## Pass a stored offset where a time zone goes

A stored offset is the offset the library returns for a zone at one instant: `±HH:MM`, or
`±HH:MM:SS` where the zone was not on a whole minute.
[`getTimeZoneOffset`](/gmt/reference/zoned/get/getTimeZoneOffset/),
[`getZonedOffset`](/gmt/reference/zoned/parse/getZonedOffset/) and `toOffsetInstant` return one. A
fixed offset is a complete rule for local time, so it goes straight back in as the time zone:

```typescript
import {
  classifyLocal,
  convertUtcToPlainDateTime,
  getTimeZoneOffset,
  resolveLocal,
} from "@northguild/gmt";

getTimeZoneOffset("Africa/Monrovia", "1970-01-01T12:00:00Z"); // "-00:44:30"

resolveLocal("1970-01-01T12:00:00", getTimeZoneOffset("Africa/Monrovia", "1970-01-01T12:00:00Z"));
// "1970-01-01T12:44:30Z"
resolveLocal("1970-01-01T12:00:00", "Africa/Monrovia"); // "1970-01-01T12:44:30Z" (the same instant)
resolveLocal("1970-01-01T12:00:00", "+05:30:00"); // "1970-01-01T06:30:00Z" (the same as "+05:30")

convertUtcToPlainDateTime("1970-01-01T12:44:30Z", { timeZone: "-00:44:30" }); // "1970-01-01T12:00:00"

classifyLocal("1970-01-01T12:00:00", "-00:44:30"); // "unique"
resolveLocal("1970-01-01T12:00:00", "-00:44:30", { disambiguation: "reject" }); // "1970-01-01T12:44:30Z"
```

A fixed offset has no transitions, so a wall time at one is never ambiguous and never
nonexistent. `classifyLocal` returns `"unique"`, and `disambiguation` has nothing to decide.

An offset with seconds is still not a time zone identifier. A written zone cannot carry
seconds (RFC 9557), and `Intl.DateTimeFormat` takes an offset to the minute only. So the
result of a function decides what it takes:

- **The result has no zone in it** (an instant, a plain value, a number or a yes or no). The
  function takes anything `isValidTimeZone` or `isValidUtcOffset` accepts. These are
  `resolveLocal`, `classifyLocal`, `toOffsetInstant`, `getTimeZoneOffset`, the `getZoned*`
  field readers, the `utc/` and `unix/` functions that convert, parse or calculate,
  `floorToZone`, `bucketRange`, the operating-hours functions, `freeTimeExpiry`,
  `chargeableDays` and `bolTimestamp`.
- **The result carries the zone, or the zone goes to `Intl.DateTimeFormat`.** The function
  takes a time zone identifier only, and returns its empty result for an offset with seconds.
  These are the functions that return a zoned string, such as `convertUtcToZoned`, the
  functions that take a business calendar, and `formatUtc`, `formatCalendarUtc`, `formatUnix`,
  `formatCalendarUnix` and `formatTimeZoneName`. Pass the IANA name, or call
  `fromOffsetInstant` for the local time with its full offset and no bracket.

```typescript
import { convertUtcToZoned, fromOffsetInstant } from "@northguild/gmt";

convertUtcToZoned("1970-01-01T12:44:30Z", "-00:44:30"); // ""
convertUtcToZoned("1970-01-01T12:44:30Z", "Africa/Monrovia");
// "1970-01-01T12:00:00-00:45[Africa/Monrovia]" — the written offset is rounded to the minute

fromOffsetInstant({ instant: "1970-01-01T12:44:30Z", offset: "-00:44:30" });
// "1970-01-01T12:00:00-00:44:30"
```

There is one limit. With an offset with seconds, a function that does arithmetic returns its
empty result within the offset's seconds (under a minute) of one end of the range of instants
Temporal supports: the last instant for an offset east of UTC, the first for one west. A
function that only reads a value has no limit, and neither have `resolveLocal`,
`classifyLocal`, `toOffsetInstant`, `getTimeZoneOffset` and `isBetweenUnix`. An offset to the
minute has no limit at all.

## Read a stored zoned time after a rule change

A zoned string stored last year was true under the rules of that day. When the zone's rules
change, the offset in the string and the offset the zone now has can disagree, and
[`classifyStoredZoned`](/gmt/reference/zoned/classify/classifyStoredZoned/) says how. It never
throws and never guesses. It returns a kind, and the instants that kind allows, or `null`
for a string that is not a stored zoned time.

```typescript
import { classifyStoredZoned } from "@northguild/gmt/zoned/classify";

// Turkey stayed on +03:00 from 2016-09-07, so a winter time stored at +02:00 is an hour off.
classifyStoredZoned("2016-12-01T09:00:00+02:00[Europe/Istanbul]");
// { kind: "offsetMismatch", storedOffset: "+02:00", zoneOffset: "+03:00",
//   atStoredOffset: "2016-12-01T07:00:00Z", atZoneOffset: "2016-12-01T06:00:00Z" }

// A time in the hour a clock change skipped (London, 2026-03-29).
classifyStoredZoned("2026-03-29T01:30:00+02:00[Europe/London]");
// { kind: "nonexistent", earlier: "2026-03-29T00:30:00Z",
//   later: "2026-03-29T01:30:00Z", atStoredOffset: "2026-03-28T23:30:00Z" }

// A string with no offset is not a stored zoned time.
classifyStoredZoned("2016-12-01T09:00:00[Europe/Istanbul]"); // null
```

The other kinds are `valid`, `validInOverlap` and `ambiguous`. Which instant you take from
an `offsetMismatch` is your decision. Take `atStoredOffset` when the offset was recorded at
the event, and `atZoneOffset` when the wall time was. The answer depends on the host's time
zone database, which a browser does not report. The
[Stored Time Audit](/gmt/tools/stored-time-audit/) shows each kind with the rule behind it, and
[`getDstTransitions`](/gmt/reference/zoned/get/getDstTransitions/) lists the clock changes of a
year.

## Two notes on the boundaries of this namespace

- **`resolveLocal` returns an instant. `convertPlainDateTimeToZoned` returns a zoned
  string.** They share the underlying resolution but differ in output and precision.
  `resolveLocal` is exact to the nanosecond, while
  [`convertPlainDateTimeToZoned`](/gmt/reference/zoned/convert/convertPlainDateTimeToZoned/)
  defaults to milliseconds. Neither takes an `offset` option. Reach for whichever shape you
  need next.
- **`offset` is `±HH:MM`, except where it is not.** Most zones kept local mean time before
  they adopted a standard time, and local mean time is rarely a whole minute.
  `Africa/Monrovia` stayed at `-00:44:30` until 1972. For those instants the offset is
  `±HH:MM:SS`. Rounding it would put the pair up to thirty seconds from the event it describes.

## See also

- [DST Disambiguation](/gmt/guides/concepts/dst-disambiguation/): the four strategies behind `resolveLocal`
- [Nanosecond Precision](/gmt/guides/core-date-operations/nanosecond-precision/): how zoned strings with sub-minute offsets are read
- [Intermodal: EDI Timestamps](/gmt/guides/industries/intermodal-edi-timestamps/): the interchange formats that carry an offset
- [Stored Time Audit](/gmt/tools/stored-time-audit/): paste stored zoned times and see which ones a rule change has moved
