import { useExecutionMode } from '@lib/use-execution-mode'; import { useReplayScheduler } from './replay'; export type Unit = 'ns' | 'us' | 'ms' | 's'; export type Timestamp = bigint & { readonly __unit: Unit }; const baseMs = Date.now(); const baseHr = process.hrtime.bigint(); export function now(): Timestamp<'ns'> { const offset = process.hrtime.bigint() - baseHr; return (BigInt(baseMs) * 1_000_000n + offset) as Timestamp<'ns'>; } export const ns = (value: bigint | number): Timestamp<'ns'> => BigInt(value) as Timestamp<'ns'>; export const us = (value: bigint | number): Timestamp<'us'> => BigInt(value) as Timestamp<'us'>; export const ms = (value: bigint | number): Timestamp<'ms'> => BigInt(value) as Timestamp<'ms'>; export const s = (value: bigint | number): Timestamp<'s'> => BigInt(value) as Timestamp<'s'>; const factors = { ns: 1n, us: 1_000n, ms: 1_000_000n, s: 1_000_000_000n } as const; export function convert( value: Timestamp, from: F, to: T ): Timestamp { const ns = BigInt(value) * factors[from]; return BigInt(ns / factors[to]) as Timestamp; } export function add(a: Timestamp, b: Timestamp): Timestamp { return (a + b) as Timestamp; } export function useTimestamp(): { timestamp: Timestamp<'ns'> } { const { isReplay } = useExecutionMode(); if (isReplay) { return { timestamp: useReplayScheduler().timestamp() }; } return { timestamp: now() }; }