# Foreign Epoch Bridges

Industry: Core

Other systems do not count from 1970. NTP counts from 1900 and rolls over in 2036. Windows
`FILETIME` counts 100-nanosecond intervals from 1601. .NET ticks count the same interval
from year 1. Excel uses a day serial with a deliberate 1900 leap-year bug. PostgreSQL stores
microseconds from 2000-01-01. Parquet's deprecated `INT96` is a Julian day plus nanoseconds
of the day, an Arrow column is an `int64` in a unit the column names, and protobuf's
`Timestamp` and `Duration` are a `seconds` and `nanos` pair.

Every integration re-derives these constants, usually wrongly. The `precision/` namespace
has a pair of functions for each: `toNtpTimestamp` and `fromNtpTimestamp`, `toFileTime` and
`fromFileTime`, `toDotNetTicks` and `fromDotNetTicks`, `toExcelSerial` and
`fromExcelSerial`, `toPgMicroseconds` and `fromPgMicroseconds`, `toParquetInt96` and
`fromParquetInt96`, `toArrowTimestamp` and `fromArrowTimestamp` (with `toArrowLocalTimestamp` and `fromArrowLocalTimestamp` for a column without a zone), `toProtoTimestamp` and
`fromProtoTimestamp`, and `toProtoDuration` and `fromProtoDuration`. For plain Unix and
nanosecond conversion, see [Nanosecond Precision](/gmt/guides/core-date-operations/nanosecond-precision/)
and [Converting Between Types](../converting-types/).

```typescript
import {
  toNtpTimestamp,
  fromNtpTimestamp,
  toFileTime,
  fromFileTime,
  toDotNetTicks,
  fromDotNetTicks,
  toExcelSerial,
  fromExcelSerial,
  toPgMicroseconds,
  fromPgMicroseconds,
  toParquetInt96,
  fromParquetInt96,
  toArrowTimestamp,
  fromArrowTimestamp,
  toArrowLocalTimestamp,
  fromArrowLocalTimestamp,
  toProtoTimestamp,
  fromProtoTimestamp,
  toProtoDuration,
  fromProtoDuration,
} from "@northguild/gmt";

toFileTime("1970-01-01T00:00:00Z");
// 116444736000000000n — 100 ns intervals since 1601-01-01

toDotNetTicks("1970-01-01T00:00:00Z");
// 621355968000000000n — same unit, counted from 0001-01-01

toPgMicroseconds("2024-03-10T12:00:00Z");
// 763387200000000n — microseconds since 2000-01-01
```

## NTP timestamps are era-ambiguous by design

The seconds field is unsigned 32-bit, so it wraps every 136.19 years or so, and the era is
not part of the value. [`fromNtpTimestamp`](/gmt/reference/precision/convert/fromNtpTimestamp/)
takes it as an explicit argument:

```typescript
toNtpTimestamp("2036-02-07T06:28:15Z");
// 18446744069414584320n — the last second of era 0

toNtpTimestamp("2036-02-07T06:28:16Z");
// 0n — era 1 begins, and the seconds field wraps

fromNtpTimestamp(0n);
// "1900-01-01T00:00:00Z"

fromNtpTimestamp(0n, 1);
// "2036-02-07T06:28:16Z" — the same value, one era on
```

## Excel's serial 60 is the phantom 29 February 1900

That date never existed, because 1900 was not a leap year. Lotus 1-2-3 got it wrong and
Excel keeps the bug for compatibility, so the serial day `[60, 61)` is a hole rather than a
date.

"Fixing" it would put the library one day out from Excel for every date before March 1900.

```typescript
fromExcelSerial(59);
// "1900-02-28T00:00:00Z"

fromExcelSerial(60);
// "" — the phantom 1900-02-29

fromExcelSerial(61);
// "1900-03-01T00:00:00Z"

toExcelSerial("2024-03-10T12:00:00Z");
// 45361.5 — days, with the time of day as the fraction

toExcelSerial("2024-03-10T12:00:00Z", { system: "1904" });
// 43899.5 — the legacy Mac system, exactly 1462 days lower
```

## Data-platform timestamps

Parquet, Arrow and protobuf each store a time in a shape that is easy to misread.

Parquet's `INT96` is deprecated. It is a Julian day and the nanoseconds into that day, and it
records no zone. [`fromParquetInt96`](/gmt/reference/precision/convert/fromParquetInt96/) always reads it
as UTC, which is what the Arrow and Spark readers do. A file written by Impala holds local
wall-clock time instead, and nothing in the file says so.

```typescript
toParquetInt96("2024-03-10T12:34:56.789Z");
// { julianDay: 2460380, nanosOfDay: 45296789000000n }

fromParquetInt96({ julianDay: 2440588, nanosOfDay: 0n });
// "1970-01-01T00:00:00Z"
```

An Arrow `Timestamp` column without a timezone holds wall-clock time of an unknown zone, and
Arrow's schema says it must not be read as UTC. So
there are two pairs. [`fromArrowTimestamp`](/gmt/reference/precision/convert/fromArrowTimestamp/) is for a column with a `timeZone` and returns an instant; [`fromArrowLocalTimestamp`](/gmt/reference/precision/convert/fromArrowLocalTimestamp/) is for a column with none and returns a wall time with no offset:

```typescript
fromArrowLocalTimestamp(1710061200000n, "millisecond");
// "2024-03-10T09:00:00"

fromArrowTimestamp(1710061200000n, "millisecond");
// "2024-03-10T09:00:00Z"

toArrowLocalTimestamp("2024-03-10T09:00:00", "millisecond");
// 1710061200000n
```

The column's `timeZone` only says the values are instants. The instant is always written in UTC,
whatever zone the column names.

A protobuf `Timestamp` counts `nanos` forward, also when `seconds` is negative, so half a
second before 1970 is `{ seconds: -1n, nanos: 500000000 }`. A `Duration` is the opposite: its
`nanos` take the sign of its `seconds`, so `-1.5` seconds is `{ seconds: -1n, nanos: -500000000 }`.
`Duration.seconds` and `nanos` are bounded separately: `seconds` from −315 576 000 000 to
+315 576 000 000 and `nanos` from −999 999 999 to +999 999 999, so `PT315576000000.000000001S` is a valid `Duration`.

```typescript
toProtoTimestamp("1969-12-31T23:59:59.5Z");
// { seconds: -1n, nanos: 500000000 }

toProtoDuration("-PT1.5S");
// { seconds: -1n, nanos: -500000000 }

toProtoDuration("PT315576000000.000000001S");
// { seconds: 315576000000n, nanos: 1 }
```

## What each bridge accepts

Each bridge accepts only what its target format can actually hold. It returns the
namespace's sentinel otherwise. `toFileTime` on a pre-1601 instant is `0n`, not a negative
tick count, and `toExcelSerial` past 9999-12-31 is `null`, not an out-of-range serial.

PostgreSQL's range is the one that surprises. Its timestamp domain starts at 4713 BC, which
is 40× later than the earliest instant Temporal can represent. An in-range Temporal instant
is therefore not automatically an in-range PostgreSQL one.

Where the target unit is coarser than a nanosecond, the conversion floors toward negative
infinity, so a round trip is exact to that unit:

- 100 ns for `FILETIME` and .NET ticks
- 1 µs for PostgreSQL
- 1 ms for Excel

NTP is the exception in the other direction. Its 2^-32 s unit is finer than a nanosecond,
so `fromNtpTimestamp` rounds to the nearest nanosecond and the round trip is exact.

The data-platform pairs follow the same rule:

- `toParquetInt96` and `fromParquetInt96` hold nanoseconds, so nothing is rounded. `fromParquetInt96` returns `""` for a `nanosOfDay` of a full day or more.
- `toArrowTimestamp` and `toArrowLocalTimestamp` floor below the column's unit and return `0n` when the string is the wrong kind: a wall time for the instant pair, or an instant for the wall-time pair. In nanoseconds they also return `0n` outside int64; check first with `isValidInt64NanosecondInstant` or `isValidInt64NanosecondDateTime`.
- `fromArrowTimestamp` and `fromArrowLocalTimestamp` return `""` when the value is past the supported range.
- `toProtoTimestamp` returns `null` outside 0001-01-01 to 9999-12-31, and for a leap second.
- `toProtoDuration` returns `null` for years, months and weeks, which have no fixed length. A day is exactly 24 hours.
- `fromProtoTimestamp` and `fromProtoDuration` return `""` for a missing member or a `nanos` the type does not allow, such as `{ seconds: 0n, nanos: -1 }` for a `Timestamp`.

## See also

- [Nanosecond Precision](/gmt/guides/core-date-operations/nanosecond-precision/): the `bigint` instant these bridges are built on
- [The Standards](/gmt/guides/concepts/standards/): where each of these epochs comes from
- [`toNtpTimestamp` reference](/gmt/reference/precision/convert/toNtpTimestamp/)
- [`fromNtpTimestamp` reference](/gmt/reference/precision/convert/fromNtpTimestamp/)
- [`toFileTime` reference](/gmt/reference/precision/convert/toFileTime/)
- [`fromFileTime` reference](/gmt/reference/precision/convert/fromFileTime/)
- [`toDotNetTicks` reference](/gmt/reference/precision/convert/toDotNetTicks/)
- [`fromDotNetTicks` reference](/gmt/reference/precision/convert/fromDotNetTicks/)
- [`toExcelSerial` reference](/gmt/reference/precision/convert/toExcelSerial/)
- [`fromExcelSerial` reference](/gmt/reference/precision/convert/fromExcelSerial/)
- [`toPgMicroseconds` reference](/gmt/reference/precision/convert/toPgMicroseconds/)
- [`fromPgMicroseconds` reference](/gmt/reference/precision/convert/fromPgMicroseconds/)
- [`toParquetInt96` reference](/gmt/reference/precision/convert/toParquetInt96/)
- [`fromParquetInt96` reference](/gmt/reference/precision/convert/fromParquetInt96/)
- [`toArrowTimestamp` reference](/gmt/reference/precision/convert/toArrowTimestamp/)
- [`fromArrowTimestamp` reference](/gmt/reference/precision/convert/fromArrowTimestamp/)
- [`fromArrowLocalTimestamp` reference](/gmt/reference/precision/convert/fromArrowLocalTimestamp/)
- [`toProtoTimestamp` reference](/gmt/reference/precision/convert/toProtoTimestamp/)
- [`fromProtoTimestamp` reference](/gmt/reference/precision/convert/fromProtoTimestamp/)
- [`toProtoDuration` reference](/gmt/reference/precision/convert/toProtoDuration/)
- [`fromProtoDuration` reference](/gmt/reference/precision/convert/fromProtoDuration/)
- [Time-Ordered Identifiers](../time-ordered-identifiers/): the instant inside a UUID, ULID, ObjectId, Snowflake or KSUID
