library / How to use Solana / HS0062
Slot to timestamp conversion
Convert a Solana slot number to a UTC time with getBlockTime, see how neighbouring slots share one second, and estimate a time for a slot that has none from measured slot time. One read-only TypeScript file.
The call
getBlockTime takes one slot and answers the Unix time of that slot's block in whole seconds, for example 1795372801. Multiply by 1000 and hand it to new Date to get a JavaScript date. The call takes no commitment, so the only choice is which slot to ask about. The file sends it to the RPC endpoint at https://services.carbium.sh with the key in the x-api-key header.
The time has a resolution of one second, and a slot is much shorter. Since the Alpenglow switch on mainnet (2026-10-21) a slot lasts about 200 ms, so about five consecutive slots carry the same number. Two slots with equal times are normal, and the run makes that visible by grouping the six newest slots by second.
| Output line | What it shows |
|---|---|
| slot, t=seconds, ISO time | One row per slot: the argument slot, its five predecessors, then three older offsets of 50, 500 and 5000 slots. |
| second N: K slots | The run of the six newest slots that share one second, with the first and last slot of the run. |
| slot time | Milliseconds per slot, taken from the last five performance samples. |
| estimate | A time computed for a slot that has none, next to the real value when the slot has one. |
Slots with no time
Some slots have no block, and a slot without a block has no time. A leader that never produced its block leaves a skipped slot, and that slot stays empty for good. A slot older than the node keeps has the opposite problem: the block existed, but this node no longer holds it. In both cases the call usually comes back as an error, and kit throws it. The file also treats a null result as no time.
The file starts from the finalized head unless you pass a slot. A finalized slot has a block, so the first row of a default run always carries a time. A slot you type yourself may print an error on its row, and the other rows still print, because every block time call settles on its own and a rejection stays with its slot.
The code
Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24, which strips the types on load. The slot is an optional argument, as in node main.ts 380000000.
// HS0062 · Slot to timestamp conversion// https://carbium.sh/library/howsolana/slot-to-timestamp-conversion// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createDefaultRpcTransport, createSolanaRpcFromTransport,} from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1);}const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));const gather = <T>(jobs: Promise<T>[]) => Promise.allSettled(jobs);const cause = (r: PromiseRejectedResult) => (r.reason instanceof Error ? r.reason.message : String(r.reason));const stamp = (sec: number) => new Date(sec * 1000).toISOString();const head = await rpc.getSlot({ commitment: "finalized" }).send();const target = process.argv[2] ? BigInt(process.argv[2]) : head;console.log(`finalized head ${head}, converting ${target}`);const slots = [0n, 1n, 2n, 3n, 4n, 5n, 50n, 500n, 5000n].map((d) => target - d);const [times, samples] = await Promise.all([ gather(slots.map((s) => rpc.getBlockTime(s).send())), gather([rpc.getRecentPerformanceSamples(5).send()]),]);const timeOf = new Map<bigint, number>();times.forEach((r, i) => { const slot = slots[i]; if (r.status === "rejected") return void console.log(`slot ${slot} none (${cause(r)})`); if (r.value == null) return void console.log(`slot ${slot} none (null)`); timeOf.set(slot, Number(r.value)); console.log(`slot ${slot} t=${r.value} ${stamp(Number(r.value))}`);});const bySecond = slots.slice(0, 6).reduce((m, s) => { const t = timeOf.get(s); if (t !== undefined) m.set(t, [...(m.get(t) ?? []), s]); return m;}, new Map<number, bigint[]>());bySecond.forEach((g, t) => console.log(`second ${t}: ${g.length} slots, ${g[g.length - 1]}..${g[0]}`));let msPerSlot = 0;const sampled = samples[0];if (sampled.status === "rejected") { console.log(`performance samples failed: ${cause(sampled)}`);} else { const n = sampled.value.reduce((a, s) => a + Number(s.numSlots), 0); const sec = sampled.value.reduce((a, s) => a + Number(s.samplePeriodSecs), 0); msPerSlot = (1000 * sec) / n; console.log(`slot time ${msPerSlot.toFixed(0)} ms from ${n} slots`);}function estimate(slot: bigint): string { let anchor: bigint | undefined; let gap = 0n; for (const k of timeOf.keys()) { const d = k > slot ? k - slot : slot - k; if (k !== slot && (anchor === undefined || d < gap)) [anchor, gap] = [k, d]; } if (anchor === undefined || !(msPerSlot > 0)) return "no estimate"; const at = Math.round(timeOf.get(anchor)! + (Number(slot - anchor) * msPerSlot) / 1000); const real = timeOf.get(slot); return `${stamp(at)} from ${anchor}` + (real === undefined ? "" : ` (real ${stamp(real)})`);}for (const s of new Set([...slots.filter((s) => !timeOf.has(s)), target - 5000n])) { console.log(`estimate ${s}: ${estimate(s)}`);}
run: node main.ts
The head read passes commitment: "finalized" on the call. Left out, kit 8.6.0 would send confirmed, while the server's own default is finalized, so the file writes the level down. Kit returns slots and times as bigint, which is why the argument goes through BigInt and the times through Number before date arithmetic.
The nine block time calls and the samples call go out together through Promise.allSettled, so a failure on one slot prints on its own row. A failure of the samples call only removes the estimates.
Estimating a time
When a slot has no answer, two inputs give a usable guess. The first is the nearest slot that does have a time. The second is the current slot time, which getRecentPerformanceSamples reports as numSlots slots in samplePeriodSecs seconds per sample. The file adds up five samples and divides, then computes the guess as the neighbour's time plus the slot distance multiplied by the measured milliseconds.
Measure the slot time on every run. The cluster moved from a slower slot to about 200 ms in October, and a constant written into code last month would now be wrong by a wide margin. The samples cover the last few minutes, so they describe today's rate. They say little about a slot from months ago, where the rate in between is unknown.
The last line of a default run shows the cost of distance. It estimates the slot 5000 behind the target from the nearest other slot that has a time, then prints the real time beside it. The gap grows with the distance, so keep the neighbour close and keep the neighbour's slot number in whatever you store.
Precision and ordering
A time cannot put two events in order when they fall in the same second, and with about five slots a second that happens constantly. The slot number can: it only grows along one chain. Store the slot next to the time, sort on the slot, and show the time. The Slack slot-lag alerts entry keeps its readings keyed by slot for the same reason.
| Need | Use |
|---|---|
| Show when something happened | The block time, to the second. |
| Order two events | The slot number. |
| Date a skipped slot | The estimate, labelled as one. |
| Compare two moments in seconds | The difference of two block times, with an error of up to a second on each end. |
The slot you read at the head also depends on the commitment you ask for. Solana commitment levels covers how the three levels compare.
Where it ends
The time is a whole second. Anything finer is a computed guess, and the estimate carries an error that grows with its distance from a known slot.
The block time is the time of the block, which is later than the moment a user signed and often later than the moment a wallet sent. The blockTime field that getTransaction returns for a signature is the same value for that transaction's slot.
How far back getBlockTime answers depends on what the node keeps, and the file does not promise a range. A slot below the node's floor prints an error. Test the slot you need before you build on it, and plan for the estimate or an archive source when it fails.
The file converts slots that exist or have existed. A slot in the future has no time yet, and projecting one takes the newest known time plus a measured slot time, with accuracy that falls as the distance grows.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| The file exits with Set CARBIUM_KEY to your Carbium API key. | Export the key in the shell that runs the file. It is read from the environment so it never sits in source or in a log. |
| The run stops at the head read with 401 UNKNOWN_KEY before any row prints. | The door does not know the key. Attach the whole key, with no missing characters, and check that CARBIUM_RPC_URL is unset or points at https://services.carbium.sh. |
| SyntaxError: Cannot convert ... to a BigInt before any row prints. | The slot argument must be digits only. Remove commas and spaces inside the number, as in 380000000. |
| A row prints a block-not-available style error instead of a time. | The slot was skipped or lies below what the node keeps. Try the slot before or after it, or read the estimate line, and keep the failed slot in your data with no time. |
| The run prints performance samples failed and no estimates. | The samples call failed on its own. Run again: the block times do not depend on it, and the estimates return once the samples answer. |
| Two events with the same time came out in the wrong order after sorting. | Sort on the slot number and use the time for display. Equal seconds hold several slots, so a sort on time alone leaves their order undefined. |
Questions
Is the block time the moment my transaction was sent?
No. It is the time of the block that holds the transaction. The blockTime field on a getTransaction reply gives the same value as getBlockTime for that slot.
Why are several slots on one timestamp?
The value is an integer count of seconds and a slot lasts about 200 ms, so about five blocks round to the same number. The run groups the six newest slots to show it.
Can I convert a slot that has not happened yet?
Only as an estimate. Take the newest known time, add the slot distance times the measured slot time from getRecentPerformanceSamples, and treat the result as a guess that gets worse with distance.
Can I ask for a block time at confirmed or finalized?
getBlockTime takes the slot only. The commitment enters where you choose the slot, as in the file's getSlot call at finalized.