From cd8f76db61f4a1ab1a8a8a4d29b2f3e863b59854 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 26 May 2023 12:19:32 +0200 Subject: taler-util,wallet-core: implement TalerPreciseTimestamp Fixes #7703 --- packages/taler-util/src/time.ts | 58 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) (limited to 'packages/taler-util/src/time.ts') diff --git a/packages/taler-util/src/time.ts b/packages/taler-util/src/time.ts index 6ada13e25..697bde5c0 100644 --- a/packages/taler-util/src/time.ts +++ b/packages/taler-util/src/time.ts @@ -25,8 +25,9 @@ import { Codec, renderContext, Context } from "./codec.js"; declare const flavor_AbsoluteTime: unique symbol; declare const flavor_TalerProtocolTimestamp: unique symbol; -declare const flavor_TalerWalletDbTimestamp: unique symbol; +declare const flavor_TalerPreciseTimestamp: unique symbol; +// FIXME: Make this opaque! export interface AbsoluteTime { /** * Timestamp in milliseconds. @@ -45,7 +46,7 @@ export interface TalerProtocolTimestamp { readonly _flavor?: typeof flavor_TalerProtocolTimestamp; } -export interface TalerWalletDbTimestamp { +export interface TalerPreciseTimestamp { /** * Seconds (as integer) since epoch. */ @@ -56,12 +57,32 @@ export interface TalerWalletDbTimestamp { */ readonly off_us?: number; - readonly _flavor?: typeof flavor_TalerWalletDbTimestamp; + readonly _flavor?: typeof flavor_TalerPreciseTimestamp; +} + +export namespace TalerPreciseTimestamp { + export function now(): TalerPreciseTimestamp { + const absNow = AbsoluteTime.now(); + return AbsoluteTime.toPreciseTimestamp(absNow); + } + + export function round(t: TalerPreciseTimestamp): TalerProtocolTimestamp { + return { + t_s: t.t_s, + } + } + + export function fromSeconds(s: number): TalerPreciseTimestamp { + return { + t_s: Math.floor(s), + off_us: (s - Math.floor(s)) / 1000 / 1000, + }; + } } export namespace TalerProtocolTimestamp { export function now(): TalerProtocolTimestamp { - return AbsoluteTime.toTimestamp(AbsoluteTime.now()); + return AbsoluteTime.toProtocolTimestamp(AbsoluteTime.now()); } export function zero(): TalerProtocolTimestamp { @@ -81,6 +102,7 @@ export namespace TalerProtocolTimestamp { t_s: s, }; } + export function min( t1: TalerProtocolTimestamp, t2: TalerProtocolTimestamp, @@ -309,7 +331,7 @@ export namespace AbsoluteTime { return cmp(t, now()) <= 0; } - export function fromTimestamp(t: TalerProtocolTimestamp): AbsoluteTime { + export function fromProtocolTimestamp(t: TalerProtocolTimestamp): AbsoluteTime { if (t.t_s === "never") { return { t_ms: "never" }; } @@ -318,7 +340,31 @@ export namespace AbsoluteTime { }; } - export function toTimestamp(at: AbsoluteTime): TalerProtocolTimestamp { + export function fromPreciseTimestamp(t: TalerPreciseTimestamp): AbsoluteTime { + if (t.t_s === "never") { + return { t_ms: "never" }; + } + const offsetUs = t.off_us ?? 0; + return { + t_ms: t.t_s * 1000 + offsetUs / 1000, + }; + } + + export function toPreciseTimestamp(at: AbsoluteTime): TalerPreciseTimestamp { + if (at.t_ms == "never") { + return { + t_s: "never", + }; + } + const t_s = Math.floor(at.t_ms / 1000); + const off_us = Math.floor(1000 * (at.t_ms - t_s * 1000)); + return { + t_s, + off_us, + } + } + + export function toProtocolTimestamp(at: AbsoluteTime): TalerProtocolTimestamp { if (at.t_ms === "never") { return { t_s: "never" }; } -- cgit v1.2.3