gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-typescript-core] 04/08: tospec


From: gnunet
Subject: [taler-typescript-core] 04/08: tospec
Date: Wed, 15 Jan 2025 22:32:50 +0100

This is an automated email from the git hooks/post-receive script.

sebasjm pushed a commit to branch master
in repository taler-typescript-core.

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

    tospec
---
 packages/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
index 95b4911a0..93e34bbaf 100644
--- 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)}`);
+  },
+};

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]