# truncateNanoseconds

Industry: Core

## Signature

```ts
truncateNanoseconds(nanoseconds: bigint, unit: NanosecondTruncationUnit): bigint
```

Truncate nanoseconds since the Unix epoch to the precision a store can actually hold.

- Floors toward negative infinity, so pre-1970 values truncate the same way post-1970 ones do: `truncateNanoseconds(-1500n, "us")` is `-2000n`, not `-1000n`. Rounding toward zero would make the result jump direction either side of the epoch.
- Call this before writing to a store that cannot hold nanoseconds — PostgreSQL `timestamptz` and MySQL `DATETIME(6)` hold microseconds, `Date`-based APIs hold milliseconds — so the value read back equals the value written.
- Idempotent: truncating an already-truncated value returns it unchanged.
- Returns `0n` on invalid input, including nanoseconds outside the range `Temporal.Instant` can represent. `0n` is also the epoch itself — validate first when the two must be told apart.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `nanoseconds` | `bigint` | nanoseconds since the Unix epoch (bigint) |
| `unit` | `NanosecondTruncationUnit` | target storage precision, "ms" (milliseconds) or "us" (microseconds) |

## Returns

truncated nanoseconds as a bigint, or 0n on invalid input

## Types

### NanosecondTruncationUnit

Storage precision a nanosecond value can be truncated to.

```ts
import type { NanosecondTruncationUnit } from "@northguild/gmt";
```

```ts
"ms" | "us"
```

## Examples

```ts
import { truncateNanoseconds } from "@northguild/gmt/precision/calculate";

truncateNanoseconds(1710072000123456789n, "us") // 1710072000123456000n
```

## Source

[precision/calculate/truncateNanoseconds.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/precision/calculate/truncateNanoseconds.ts)
