# Type Conversion

Industry: Core

GMT provides conversion functions between plain, zoned, UTC, Unix, and calendar
types. The mistakes here cover unit mismatches, annotation handling, and calendar
semantics.

## Mistakes

### Using Date.getTime() for conversion

Severity: high.

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:
```js
const ms = new Date("2024-03-15").getTime(); // 1710460800000 — milliseconds
const unix = ms; // used as seconds — 1000× too large
```

Right:
```js
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, named
```

### Mixing epoch seconds and milliseconds

Severity: high.

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:
```js
const ts = getUnixNow({ epochUnit: "seconds" }); // 1710504645 — seconds
const asMs = ts * 1000; // manual conversion — error-prone
```

Right:
```js
import { getUnixNow } from "@northguild/gmt/unix";

getUnixNow(); // 1710504645000 — milliseconds (the default)
getUnixNow({ epochUnit: "seconds" }); // 1710504645 — seconds
// ask for the unit the consumer expects
```

### Not handling conversion errors

Severity: medium.

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:
```js
const result = convertUtcToUnix(badInput); // null
```

Right:
```js
import { convertUtcToUnix } from "@northguild/gmt/utc";

const result = convertUtcToUnix(input);
if (result === null) {
  // input was invalid
}
```

### Reading a calendar year out of the string

Severity: medium.

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:
```js
const jy = convertDateToCalendar("2024-03-15", "japanese").slice(0, 4); // "2024" — the ISO year, not the era year
```

Right:
```js
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 6
```

### Using the calendar ids from before 1.16.0

Severity: medium.

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:
```js
convertDateToCalendar("2024-10-03", "taiwan"); // "" — not a calendar id
```

Right:
```js
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

Severity: high.

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:
```js
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 bits
```

Right:
```js
import { fromUuidTimestamp } from "@northguild/gmt/precision/identifiers";

fromUuidTimestamp("919108f7-52d1-4320-9bac-f847db4148a8"); // "" — version 4 is random
fromUuidTimestamp("017f22e2-79b0-7cc3-98c4-dc0c0c07398f"); // "2022-02-22T19:22:22Z"
```

### Lower-casing a KSUID

Severity: medium.

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:
```js
const normalised = "0ujtsYcgvSTl8PAuAdqWYSMnLOv".toLowerCase(); // "0ujtsycgvstl8pauadqwysmnlov" — another KSUID
```

Right:
```js
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 later
```

### Writing negative nanos into a protobuf Timestamp

Severity: medium.

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:
```js
const ts = { seconds: 0n, nanos: -1 }; // one nanosecond before 1970 — invalid
```

Right:
```js
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

- [Conversion guide](/gmt/guides/conversion/)
- [`convertUtcToUnix` reference](/gmt/reference/utc/convert/convertUtcToUnix/)
- [`convertUnixToUtc` reference](/gmt/reference/unix/convert/convertUnixToUtc/)
- [`convertDateToCalendar` reference](/gmt/reference/plain/convert/convertDateToCalendar/)
- [`convertZonedToCalendar` reference](/gmt/reference/zoned/convert/convertZonedToCalendar/)
- [`getUnixNow` reference](/gmt/reference/unix/get/getUnixNow/)
- [Time-Ordered Identifiers](/gmt/guides/conversion/time-ordered-identifiers/)
- [Foreign Epoch Bridges](/gmt/guides/conversion/foreign-epoch-bridges/)
