feat: version 2 beta

This commit is contained in:
2025-04-25 22:39:47 +00:00
commit 1e58359905
75 changed files with 6899 additions and 0 deletions

40
libraries/time.ts Normal file
View File

@@ -0,0 +1,40 @@
import { HLC } from "./hlc.ts";
import { Timestamp } from "./timestamp.ts";
const clock = new HLC();
/**
* Get a date object from given event meta timestamp.
*
* @param timestamp - Event meta timestamp.
*/
export function getDate(timestamp: string): Date {
return new Date(getUnixTimestamp(timestamp));
}
/**
* Get logical timestamp based on current time.
*/
export function getLogicalTimestamp(): string {
const ts = clock.now().toJSON();
return `${ts.time}-${String(ts.logical).padStart(5, "0")}`;
}
/**
* Get timestamp instance from provided logical timestamp.
*
* @param ts - Logical timestamp to convert.
*/
export function getTimestamp(ts: string): Timestamp {
const [time, logical] = ts.split("-");
return new Timestamp(time, Number(logical));
}
/**
* Get unix timestamp value from provided logical timestamp.
*
* @param ts - Logical timestamp to convert.
*/
export function getUnixTimestamp(ts: string): number {
return getTimestamp(ts).time;
}