taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit d32df801ef9f61552094d9392c97442f9f4f097c
parent e1aae7f0f0f0c30d91f1f2ea3bcefeb1e4eb7cf3
Author: Sebastian <sebasjm@gmail.com>
Date:   Wed, 15 Jan 2025 11:40:31 -0300

tospec

Diffstat:
Mpackages/taler-util/src/time.ts | 52++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/packages/taler-util/src/time.ts b/packages/taler-util/src/time.ts @@ -298,6 +298,34 @@ export namespace Duration { return { d_ms }; } + export function toSpec({ d_ms }: Duration): + | { + seconds: number; + minutes: number; + hours: number; + days: number; + } + | undefined { + if (d_ms === "forever") return undefined; + let time = d_ms; + const millis = d_ms % SECONDS; + time -= millis; + const s = time % MINUTES; + time -= s; + const m = time % HOURS; + time -= m; + const h = time % DAYS; + time -= h; + const d = time; + + return { + seconds: s / SECONDS, + minutes: m / MINUTES, + hours: h / HOURS, + days: d / DAYS, + }; + } + export function getForever(): Duration { return { d_ms: "forever" }; } @@ -605,7 +633,9 @@ export function durationAdd(d1: Duration, d2: Duration): Duration { export const codecForAbsoluteTime: Codec<AbsoluteTime> = { decode(x: any, c?: Context): AbsoluteTime { if (x === undefined) { - throw Error(`got undefined and expected absolute time at ${renderContext(c)}`); + throw Error( + `got undefined and expected absolute time at ${renderContext(c)}`, + ); } const t_ms = x.t_ms; if (typeof t_ms === "string") { @@ -623,7 +653,9 @@ export const codecForTimestamp: Codec<TalerProtocolTimestamp> = { decode(x: any, c?: Context): TalerProtocolTimestamp { // Compatibility, should be removed soon. if (x === undefined) { - throw Error(`got undefined and expected timestamp at ${renderContext(c)}`); + throw Error( + `got undefined and expected timestamp at ${renderContext(c)}`, + ); } const t_ms = x.t_ms; if (typeof t_ms === "string") { @@ -676,3 +708,19 @@ export const codecForDuration: Codec<TalerProtocolDuration> = { throw Error(`expected duration at ${renderContext(c)}`); }, }; + +export const codecForDurationMs: Codec<Duration> = { + decode(x: any, c?: Context): Duration { + const d_ms = x.d_ms; + if (typeof d_ms === "string") { + if (d_ms === "forever") { + return { d_ms: "forever" }; + } + throw Error(`expected duration at ${renderContext(c)}`); + } + if (typeof d_ms === "number") { + return { d_ms }; + } + throw Error(`expected duration at ${renderContext(c)}`); + }, +};