mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-16 21:08:38 +08:00
22 lines
788 B
TypeScript
22 lines
788 B
TypeScript
/**
|
|
* SpacetimeDB timestamps are { __timestamp_micros_since_unix_epoch__: bigint }.
|
|
* Convert to a JS Date.
|
|
*/
|
|
export function toDate(ts: unknown): Date {
|
|
if (ts instanceof Date) return ts;
|
|
if (typeof ts === "object" && ts !== null && "__timestamp_micros_since_unix_epoch__" in ts) {
|
|
const micros = (ts as Record<string, unknown>).__timestamp_micros_since_unix_epoch__;
|
|
if (typeof micros !== "bigint") {
|
|
throw new TypeError(`Expected __timestamp_micros_since_unix_epoch__ to be bigint, got ${typeof micros}`);
|
|
}
|
|
return new Date(Number(micros / 1000n));
|
|
}
|
|
if (typeof ts === "bigint") {
|
|
return new Date(Number(ts / 1000n));
|
|
}
|
|
if (typeof ts === "number") {
|
|
return new Date(ts);
|
|
}
|
|
throw new TypeError(`Cannot convert ${typeof ts} to Date`);
|
|
}
|