Type Conversion
GMT provides conversion functions between plain, zoned, UTC, Unix, and calendar types. The mistakes here cover unit mismatches, annotation handling, and calendar semantics.
Mistakes
Section titled “Mistakes”Using Date.getTime() for conversion
Date.getTime() returns epoch milliseconds whatever the consumer expects. Handing that number to an API that expects Unix seconds introduces a 1000× unit error. GMT's conversions name the unit: milliseconds by default, seconds when you pass { epochUnit: 'seconds' }.
Wrong
const ms = new Date("2024-03-15").getTime(); // 1710460800000 — millisecondsconst unix = ms; // used as seconds — 1000× too largeRight
import { convertUtcToUnix } from "@northguild/gmt/utc";
convertUtcToUnix("2024-03-15T00:00:00Z"); // 1710460800000 — milliseconds (the default)convertUtcToUnix("2024-03-15T00:00:00Z", { epochUnit: "seconds" }); // 1710460800 — seconds, namedMixing epoch seconds and milliseconds
getUnixNow() returns milliseconds; getUnixNow({ epochUnit: 'seconds' }) returns seconds. Passing seconds to a function expecting milliseconds (or vice versa) produces timestamps that are 1000× off.
Wrong
const ts = getUnixNow({ epochUnit: "seconds" }); // 1710504645 — secondsconst asMs = ts * 1000; // manual conversion — error-proneRight
import { getUnixNow } from "@northguild/gmt/unix";
getUnixNow(); // 1710504645000 — milliseconds (the default)getUnixNow({ epochUnit: "seconds" }); // 1710504645 — seconds// ask for the unit the consumer expectsNot handling conversion errors
Conversion functions return a sentinel for invalid input rather than throwing — an empty string from a function that returns a string, null from one that returns a number, such as convertUtcToUnix. A failed conversion is silent — check the result.
Wrong
const result = convertUtcToUnix(badInput); // nullRight
import { convertUtcToUnix } from "@northguild/gmt/utc";
const result = convertUtcToUnix(input);if (result === null) {// input was invalid}Reading a calendar year out of the string
convertDateToCalendar and convertZonedToCalendar write the RFC 9557 form: ISO digits, then a [u-ca=...] annotation. The digits are the ISO date in every calendar, so slicing the string gives the ISO year, never the Hebrew year or the Japanese era year. Read calendar fields from Temporal.
Wrong
const jy = convertDateToCalendar("2024-03-15", "japanese").slice(0, 4); // "2024" — the ISO year, not the era yearRight
import { convertDateToCalendar, Temporal } from "@northguild/gmt";
const jp = convertDateToCalendar("2024-03-15", "japanese"); // "2024-03-15[u-ca=japanese]"Temporal.PlainDate.from(jp).eraYear; // 6 — Reiwa 6Using the calendar ids from before 1.16.0
GMT's calendar ids are the canonical BCP 47 / Temporal ids. gregorian, taiwan and islamic-tabular are not calendar ids and return the sentinel; use iso8601 (or gregory), roc and islamic-tbla. An alias Temporal accepts, such as ethiopic-amete-alem, is canonicalized to ethioaa.
Wrong
convertDateToCalendar("2024-10-03", "taiwan"); // "" — not a calendar idRight
import { convertDateToCalendar } from "@northguild/gmt";
convertDateToCalendar("2024-10-03", "roc"); // "2024-10-03[u-ca=roc]"convertDateToCalendar("2024-10-03[u-ca=roc]", "iso8601"); // "2024-10-03"Reading a time out of a UUID v4
A version 4 UUID is random and holds no time. Code that slices the first hex digits and treats them as a timestamp gets a number that looks real and means nothing. fromUuidTimestamp returns an empty string for v4, and for every other version that carries no timestamp.
Wrong
const ms = parseInt("919108f7-52d1-4320-9bac-f847db4148a8".replace(/-/g, "").slice(0, 12), 16);new Date(ms).toISOString(); // "7041-11-06T22:31:51.761Z" — a date from random bitsRight
import { fromUuidTimestamp } from "@northguild/gmt/precision/identifiers";
fromUuidTimestamp("919108f7-52d1-4320-9bac-f847db4148a8"); // "" — version 4 is randomfromUuidTimestamp("017f22e2-79b0-7cc3-98c4-dc0c0c07398f"); // "2022-02-22T19:22:22Z"Lower-casing a KSUID
UUIDs and ULIDs ignore case, so normalising every ID to lower case looks harmless. A KSUID is base62, where a and A are different digits, so lower-casing writes a different KSUID that names a different second.
Wrong
const normalised = "0ujtsYcgvSTl8PAuAdqWYSMnLOv".toLowerCase(); // "0ujtsycgvstl8pauadqwysmnlov" — another KSUIDRight
import { fromKsuid } from "@northguild/gmt/precision/identifiers";
fromKsuid("0ujtsYcgvSTl8PAuAdqWYSMnLOv"); // "2017-10-10T04:00:47Z"fromKsuid("0ujtsycgvstl8pauadqwysmnlov"); // "2017-10-10T04:00:51Z" — a different ID, four seconds laterWriting negative nanos into a protobuf Timestamp
A Timestamp's nanos count forward from its seconds, so an instant before 1970 has negative seconds and non-negative nanos. { seconds: 0n, nanos: -1 } is invalid. toProtoTimestamp writes the valid form, and fromProtoTimestamp returns an empty string for the invalid one.
Wrong
const ts = { seconds: 0n, nanos: -1 }; // one nanosecond before 1970 — invalidRight
import { toProtoTimestamp, fromProtoTimestamp } from "@northguild/gmt/precision/convert";
toProtoTimestamp("1969-12-31T23:59:59.999999999Z"); // { seconds: -1n, nanos: 999999999 }fromProtoTimestamp({ seconds: 0n, nanos: -1 }); // ""See also
Section titled “See also”© 2026 NorthGuild · Licences
Code examples: MIT-0. Packages: MIT. Third-party data and components: their own licences, credited where they appear. Everything else: © 2026 NorthGuild, all rights reserved.