UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

255 lines (238 loc) • 16.2 kB
--- name: gmt-integration description: > Application integration patterns — stable cache keys, router/query params, table-sort keys, nanosecond timestamps across JSON and storage boundaries, foreign epoch bridges (NTP, FILETIME, .NET ticks, Excel, PostgreSQL), the timestamps inside UUID, ULID, ObjectId, Snowflake and KSUID identifiers, Parquet INT96, Arrow and protobuf timestamps, TAI and the closed IERS leap-second table (taiMinusUtc, leapSecondsBetween, nextLeapSecond, isLeapSecond, toTAI, fromTAI, toTai64n, fromTai64n, toTT, fromTT, toTCG, fromTCG, tcgMinusTt), Julian Date, Modified Julian Date and J2000 seconds (toJulianDate, toModifiedJulianDate, toJ2000Seconds and their from and Parts forms), CCSDS ASCII time codes A and B (parseCcsdsAscii*, formatCcsdsAscii*), and lint package selection (ESLint, Biome, Oxlint). GNSS time is in gmt-gnss. Reads the docs site (llms.txt and each page's .md twin) and the installed package's JSDoc for API details, and the lint-package READMEs for lint setup; this skill is a routing pointer, not an API dump. sources: - "northguild/gmt:apps/dox/src/content/docs/install.mdx" - "northguild/gmt:apps/dox/src/content/docs/guides/integration/app-integratio\ n.mdx" - "northguild/gmt:apps/dox/src/content/docs/guides/integration/linting.mdx" - "northguild/gmt:apps/dox/src/content/docs/guides/core-date-operations/nanos\ econd-precision.mdx" - "northguild/gmt:apps/dox/src/content/docs/guides/conversion/foreign-epoch-b\ ridges.mdx" - "northguild/gmt:packages/gmt/src/zoned/validate/index.ts" - "northguild/gmt:packages/gmt/src/precision/convert/index.ts" - "northguild/gmt:packages/gmt/src/precision/identifiers/index.ts" - "northguild/gmt:apps/dox/src/content/docs/guides/conversion/time-ordered-id\ entifiers.mdx" - "northguild/gmt:packages/gmt/src/precision/format/index.ts" - "northguild/gmt:packages/gmt/src/precision/calculate/index.ts" - "northguild/gmt:packages/gmt/src/precision/validate/index.ts" - "northguild/gmt:packages/gmt/src/timescale/index.ts" metadata: type: composition library: "@northguild/gmt" library_version: "1.23.1" --- # GMT Integration Use this skill when integrating GMT into an application framework or choosing which lint package to install for Date-ban enforcement. ## When to load - The user asks about cache keys, router params, or table-sort keys for dates. - The user wants to validate a timezone id before using it as a cache key. - The user is moving nanosecond timestamps across a JSON wire or into a database. - The user is reading or writing another system's epoch — NTP, Windows `FILETIME`, .NET ticks, an Excel day serial, or PostgreSQL's internal microseconds. - The user wants the time inside a UUID, ULID, MongoDB ObjectId, Snowflake or KSUID, or writes Parquet `INT96`, Arrow or protobuf timestamps. - The user needs TAI, TAI − UTC on a date, or whether a `23:59:60` was a real leap second. - The user is picking an ESLint, Biome, or Oxlint plugin to ban `Date` APIs. - The user has a lint package but not the gmt runtime (or vice versa). ## Core rules 1. **Canonical, stable representations at boundaries.** Use ISO 8601 strings or normalized epoch numbers for cache keys — never `Date` objects or fuzzy strings. Different environments must produce the same key. 2. **Validate before you key.** Always run `isValidTimeZone` on a timezone parameter before using it as a cache key or router param — `"Invalid/Zone"` is a sentinel failure, not a real zone. 3. **Pick one canonical Unix unit.** If you mix epoch seconds and milliseconds, convert explicitly with `convertUtcToUnix(value)` (milliseconds) / `convertUtcToUnix(value, { epochUnit: "seconds" })` rather than dividing/multiplying by hand. An `epochUnit` option must be `"seconds"` or `"milliseconds"` (singular accepted): any other value returns the sentinel instead of being read as milliseconds. `unix/` and `utc/` read zone-dependent fields in UTC unless you pass `timeZone` (`"local"` for the system zone); that holds for the `utc/` readers and converters too, such as `parseHourFromUtc` and `convertUtcToPlainDate`. `convertUtcToZoned` and `convertUnixToZoned` take a required zone and do not accept `"local"`. A blank epoch string is invalid, not `1970`, and `sortUnix`/`minUnix`/`maxUnix` skip values that are not integer epochs in Temporal's range. 4. **Nanoseconds are `bigint`, never `number`.** A `number` is exact only to `2^53 − 1`, which nanoseconds since the epoch passed in April 1970. Use `toNanoseconds` / `fromNanoseconds`, cross a JSON boundary with `formatNanoseconds` / `parseNanoseconds` (`JSON.stringify` throws on a raw `bigint`), and call `truncateNanoseconds(ns, "us" | "ms")` before writing to a store that holds less than nanoseconds — PostgreSQL `timestamptz` holds microseconds — or the read-back value will not equal what was written. 5. **Never hand-roll another system's epoch constant.** `precision/convert/` carries the bridges: `toNtpTimestamp` / `fromNtpTimestamp`, `toFileTime` / `fromFileTime`, `toDotNetTicks` / `fromDotNetTicks`, `toExcelSerial` / `fromExcelSerial`, `toPgMicroseconds` / `fromPgMicroseconds`, and the data-platform bridges `toParquetInt96` / `fromParquetInt96`, `toArrowTimestamp` / `fromArrowTimestamp`, `toArrowLocalTimestamp` / `fromArrowLocalTimestamp`, `toProtoTimestamp` / `fromProtoTimestamp`, `toProtoDuration` / `fromProtoDuration`. An Arrow column without a timezone is wall-clock time, so read it with `fromArrowLocalTimestamp`, which returns a plain date-time and not an instant. An `int64` of nanoseconds ends in 1677 and 2262, so check with `isValidInt64NanosecondInstant` or `isValidInt64NanosecondDateTime` before trusting a nanosecond `0n`. Two traps they exist to close: an NTP timestamp's seconds field wraps every ~136 years and carries no era, so `fromNtpTimestamp` needs the era passed in; and Excel's serial 60 is the phantom 1900-02-29, a date that never existed, so it returns `""` rather than a neighbouring day. Each bridge rejects instants its target format cannot hold instead of returning an out-of-range number. More edges to know: - A `FILETIME` at or above `2^63` is out of range, as Windows' `FileTimeToSystemTime` rules, so `fromFileTime` returns `""`. That includes `0xFFFFFFFFFFFFFFFF`, the "do not modify" marker. - An all-zero NTP timestamp is both the NTP epoch and RFC 5905's "unknown or unsynchronized" marker. Check for `0n` yourself before trusting it as 1900. - .NET ticks name an instant only when `DateTimeKind` is `Utc`. `Local` and `Unspecified` ticks count wall-clock time. - `toNanoseconds` and `isValidInstant` take exactly `Z` or a written offset and nothing after it: a bracketed zone, calendar or other annotation (`2024-06-15T10:00:00Z[Europe/London]`) makes the string invalid. 6. **Leap seconds come from the built-in table, never a literal.** `timescale/` holds the IERS leap-second table as a closed record: 27 insertions, the last on 2016-12-31, and TAI − UTC stays 37 s for every later date, because UTC becomes continuous on 20 May 2027 (CGPM 2026). No function takes a table. ```typescript taiMinusUtc("2024-01-01T00:00:00Z"); // 37 leapSecondsBetween("2000-01-01T00:00:00Z", "2024-01-01T00:00:00Z"); // 5 isLeapSecond("2016-12-31T23:59:60Z"); // true (a table lookup) isLeapSecond("2024-06-15T12:34:60Z"); // false (hasSecond60 is the shape test) toTAI("2026-01-01T00:00:00Z"); // "2026-01-01T00:00:37 TAI" fromTAI("2017-01-01T00:00:36 TAI"); // "2016-12-31T23:59:60Z" toTai64n("1992-06-02T08:06:43Z"); // "@400000002a2b2c2d00000000" fromTai64n("@400000002a2b2c2d00000000"); // "1992-06-02T08:06:43Z" toTT("2026-01-01T00:00:00Z"); // "2026-01-01T00:01:09.184 TT" toTCG("2026-01-01T00:00:00Z"); // "2026-01-01T00:01:10.261661917 TCG" fromTCG("2026-01-01T00:01:10.261661917 TCG"); // "2026-01-01T00:00:00Z" tcgMinusTt("2026-01-01T00:00:00Z"); // 1.077661917 toJulianDateParts("2026-01-01T00:00:00.123456789Z"); // { jd1: 2461041, jd2: 0.5000014288980208 } toJulianDate("2026-01-01T12:00:00Z"); // 2461042 toModifiedJulianDate("2000-01-01T00:00:00Z"); // 51544 toJ2000Seconds("2000-01-01T11:58:55.816Z"); // 0 fromJ2000Seconds(0); // "2000-01-01T11:58:55.816Z" parseCcsdsAsciiA("1988-01-18T17:20:43.123456Z"); // "1988-01-18T17:20:43.123456Z" parseCcsdsAsciiB("2024-366T12:00:00Z"); // "2024-12-31T12:00:00Z" formatCcsdsAsciiA("2024-06-15T12:30:00.123456789+02:00", 6); // "2024-06-15T10:30:00.123456Z" formatCcsdsAsciiB("2016-12-31T23:59:60Z"); // "2016-366T23:59:60Z" ``` - `toTai64n` and `fromTai64n` read and write the TAI64N labels of qmail, daemontools and s6 logs. A label counts TAI seconds, so reading one as Unix seconds is 37 s out today. `fromTai64n` takes 16, 24 or 32 hex digits, with or without `@`, and drops TAI64NA attoseconds. Labels daemontools writes on a UTC clock read 27 s early: `fromTai64n` does not correct that. - `toTT` / `fromTT` and `toTCG` / `fromTCG` convert to and from Terrestrial Time (TAI + 32.184 s) and Geocentric Coordinate Time, which runs ahead of TT at a fixed rate. `tcgMinusTt` returns the gap in seconds, or `null`. All floor to the nanosecond and return `""` before 1972. - `toJulianDateParts` / `fromJulianDateParts` carry a Julian Date as two doubles, `jd1` a whole number of days and `jd2` the fraction, and keep the nanosecond. `toJulianDate` and `toModifiedJulianDate` return one double, which is rounded: about 20 µs at today's JD, and the MJD keeps more digits than the JD. Each `to*` has a `from*` that returns the UTC instant. They take an optional scale (`"UTC"`, `"TAI"`, `"TT"`, `"TCG"`). The default is `"UTC"`, except `toJ2000Seconds` and `fromJ2000Seconds`, which default to `"TT"`, the scale J2000.0 is defined in. A JD day starts at noon and an MJD day at midnight. On UTC every day counts as one day, a leap-second day included. Any other scale, such as `"TDB"`, returns the sentinel. - `parseCcsdsAsciiA` / `formatCcsdsAsciiA` and `parseCcsdsAsciiB` / `formatCcsdsAsciiB` read and write the CCSDS ASCII time codes (CCSDS 301.0-B-4). Code A names the day by calendar date (`1988-01-18T17:20:43.123456Z`), Code B by day of year (`1988-018T17:20:43.123456Z`). The code is UTC: `Z` is optional on input and always written. A parser needs the seconds, keeps the fraction digits as sent (up to nine, never rounded) and returns the instant as ISO 8601. A formatter takes `fractionalSecondDigits` (`"auto"`, or 0 to 9; extra digits are cut, never rounded). Second 60 is read and written only at an inserted leap second. All four return `""` on input that does not fit. - Before 1972-01-01 TAI − UTC was fractional, so every function returns its sentinel there. - `timescale/`, `gnss/` and `finance/` accept a second 60 only on a listed insertion day (for `gnss/`, see the `gmt-gnss` skill; for `finance/`, the `gmt-finance` skill), and may return `…T23:59:60Z`. Every other GMT function rejects `:60`, so do not pass that result on. - A smeared clock never reads `23:59:60` and is up to a second off UTC around an insertion. GMT cannot detect smearing. 7. **Lint packages are toolchain-specific, not mutually exclusive.** Recommend only the one matching the project's existing linter; do not force all three. 8. **Read an ID's time with `precision/identifiers`.** `fromUuidTimestamp` (UUID v1, v6 and v7; any other version returns `""`), `fromUlid`, `fromObjectId`, `fromKsuid` and `fromSnowflake`. A Snowflake needs its layout passed explicitly, `fromSnowflake(id, { epochMs, shift })`, because the same ID read with another system's epoch gives a plausible wrong date. Never pass a 64-bit ID as a `number`; use a `bigint` or a string. `uuidV7TimestampPrefix` and `ulidTimestampPrefix` give the prefix a range scan starts from. GMT decodes IDs and generates none. 9. **Read the docs site and the lint README.** This skill is a routing pointer. For full integration guidance, read these: - `https://northguild.dev/gmt/llms.txt` is the map of every docs page. - Each page has a `.md` twin that holds its text, for example `https://northguild.dev/gmt/guides/integration/app-integration.md`. - The JSDoc ships in the installed package: `node_modules/@northguild/gmt/dist/**/*.d.ts`. - For lint setup, read the matching `@northguild/gmt-eslint` / `gmt-oxlint` / `gmt-biome` README. ## Lint package selection | Current lint stack | Recommend | | ------------------ | ----------------------------------------------------- | | ESLint | `@northguild/gmt-eslint` | | Biome | `@northguild/gmt-biome` | | Oxlint | `@northguild/gmt-oxlint` | | No linter yet | Add one of the above; install `@northguild/gmt` first | These can be used individually or together. If the project has a lint package but no `@northguild/gmt`, suggest adding gmt to support safe Date-to-Temporal refactors. Lint findings should point toward existing gmt methods first; only suggest a new Temporal custom method (and an issue) when no gmt helper covers the pattern. ## Integration guidance - **Query/cache keys**: `['events', { start: '2024-02-29', end: '2024-03-01' }]` — ISO strings, validated. - **Router params**: validate with `isValidDate` / `isValidTimeZone` before use. - **Table sorting**: use ISO strings as sort keys for consistent ordering. - **State management**: canonicalized strings/numbers at boundaries only. - **High-precision timestamps**: keep `bigint` nanoseconds internally, send the decimal string from `formatNanoseconds` over the wire, and truncate to the target column's precision before persisting. - **Foreign epochs at the boundary**: convert on the way in and on the way out with the `precision/convert/` bridges, and keep ISO strings or `bigint` nanoseconds everywhere in between — never carry another system's serial through your own code. - **Identifiers and data-platform columns**: decode the time out of an ID with `precision/identifiers`, and bridge Parquet `INT96`, Arrow and protobuf values with the `precision/convert/` bridges. - **Atomic time at the boundary**: convert a TAI label to a UTC instant on the way in (`fromTAI`, or `fromTai64n` for a log label), and back on the way out. GNSS time scales and weeks are in the `gmt-gnss` skill. ## Key functions - **Leap seconds and TAI** (`@northguild/gmt/timescale`): `taiMinusUtc`, `leapSecondsBetween`, `nextLeapSecond`, `isLeapSecond`, `toTAI`, `fromTAI`, `toTai64n`, `fromTai64n`, `toTT`, `fromTT`, `toTCG`, `fromTCG`, `tcgMinusTt`, `toJulianDateParts`, `fromJulianDateParts`, `toJulianDate`, `fromJulianDate`, `toModifiedJulianDate`, `fromModifiedJulianDate`, `toJ2000Seconds`, `fromJ2000Seconds` - **Identifiers** (`@northguild/gmt/precision/identifiers`): `fromUuidTimestamp`, `uuidV7TimestampPrefix`, `fromUlid`, `ulidTimestampPrefix`, `fromObjectId`, `fromSnowflake`, `fromKsuid` ## References - [Docs map for agents (llms.txt)](https://northguild.dev/gmt/llms.txt) - [Install](https://northguild.dev/gmt/install/) - [App integration guide](https://northguild.dev/gmt/guides/integration/app-integration/) - [Nanosecond precision guide](https://northguild.dev/gmt/guides/core-date-operations/nanosecond-precision/) - [Foreign epoch bridges guide](https://northguild.dev/gmt/guides/conversion/foreign-epoch-bridges/) - [Time-ordered identifiers guide](https://northguild.dev/gmt/guides/conversion/time-ordered-identifiers/) - [timescale reference](https://northguild.dev/gmt/reference/timescale/) - [Lint Package Selection guide](/guides/integration/linting/) - [Core Rules](/core-rules/)