diff options
Diffstat (limited to 'packages/taler-util/src/time.ts')
-rw-r--r-- | packages/taler-util/src/time.ts | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/packages/taler-util/src/time.ts b/packages/taler-util/src/time.ts index c677d52ae..5702b2947 100644 --- a/packages/taler-util/src/time.ts +++ b/packages/taler-util/src/time.ts @@ -21,7 +21,7 @@ /** * Imports. */ -import { Codec, renderContext, Context } from "./codec.js"; +import { Codec, Context, renderContext } from "./codec.js"; declare const flavor_AbsoluteTime: unique symbol; declare const flavor_TalerProtocolTimestamp: unique symbol; @@ -412,6 +412,10 @@ export namespace AbsoluteTime { return cmp(t, now()) <= 0; } + export function isNever(t: AbsoluteTime): boolean { + return t.t_ms === "never"; + } + export function fromProtocolTimestamp( t: TalerProtocolTimestamp, ): AbsoluteTime { @@ -503,6 +507,23 @@ export namespace AbsoluteTime { return { t_ms: t1.t_ms + d.d_ms, [opaque_AbsoluteTime]: true }; } + /** + * Get the remaining duration until {@param t1}. + * + * If {@param t1} already happened, the remaining duration + * is zero. + */ + export function remaining(t1: AbsoluteTime): Duration { + if (t1.t_ms === "never") { + return Duration.getForever(); + } + const stampNow = now(); + if (stampNow.t_ms === "never") { + throw Error("invariant violated"); + } + return Duration.fromMilliseconds(Math.max(0, t1.t_ms - stampNow.t_ms)); + } + export function subtractDuraction( t1: AbsoluteTime, d: Duration, |