toPgMicroseconds
Signature
Section titled “Signature”toPgMicroseconds(isoString: string): bigintConvert an ISO 8601 instant string to PostgreSQL’s internal timestamptz value.
- PostgreSQL stores a
timestamp/timestamptzas a signed 64-bit count of microseconds since 2000-01-01T00:00:00Z — not since the Unix epoch. The wire protocol’s binary format carries this same value; only the text format and the client libraries convert to 1970. - Sub-microsecond digits floor away, via
truncateNanoseconds(..., "us"), so the grid stays uniform either side of the epoch.timestamptzhas no finer resolution to write them to. - Instants outside PostgreSQL’s own timestamp range are invalid input. It starts at
-004713-11-24T00:00:00Z(Julian Day 0 — the manual’s “4713 BC”, counted in the Julian calendar), which is 40× later than the earliest instantTemporalcan represent, so an in-rangeTemporalinstant is not automatically an in-range PostgreSQL one. The upper end is the other way round: PostgreSQL stops at 294276 AD, past whatTemporalholds, so the instant range binds there first. - Returns
0non invalid input.0nis also the PostgreSQL epoch itself (2000-01-01) — validate the string first withisValidInstantwhen the two must be told apart.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
isoString | string | ISO 8601 instant string (e.g. “2024-03-10T12:00:00Z”) |
Returns
Section titled “Returns”microseconds since 2000-01-01 as a bigint, or 0n on invalid input
Examples
Section titled “Examples”import { toPgMicroseconds } from "@northguild/gmt/precision/convert";
toPgMicroseconds("2000-01-01T00:00:00Z") // 0n — the PostgreSQL epoch