diff options
author | Florian Dold <florian.dold@gmail.com> | 2020-09-02 14:44:36 +0530 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2020-09-02 14:44:40 +0530 |
commit | 8a3ac7f08b114360118bf58a38983401107a62cf (patch) | |
tree | ecace89f8b01818f1d521760d987ff02d5cdd33c /packages/taler-wallet-core/src/util | |
parent | 8d0081b62248f0663e3b4e1ba5246b454f5823db (diff) | |
download | wallet-core-8a3ac7f08b114360118bf58a38983401107a62cf.tar.xz |
schedule exchange updating
Diffstat (limited to 'packages/taler-wallet-core/src/util')
-rw-r--r-- | packages/taler-wallet-core/src/util/http.ts | 19 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/util/time.ts | 32 |
2 files changed, 46 insertions, 5 deletions
diff --git a/packages/taler-wallet-core/src/util/http.ts b/packages/taler-wallet-core/src/util/http.ts index 0977b429e..e050efe61 100644 --- a/packages/taler-wallet-core/src/util/http.ts +++ b/packages/taler-wallet-core/src/util/http.ts @@ -26,7 +26,7 @@ import { Codec } from "./codec"; import { OperationFailedError, makeErrorDetails } from "../operations/errors"; import { TalerErrorCode } from "../TalerErrorCode"; import { Logger } from "./logging"; -import { Duration, Timestamp, getTimestampNow } from "./time"; +import { Duration, Timestamp, getTimestampNow, timestampAddDuration, timestampMin, timestampMax } from "./time"; const logger = new Logger("http.ts"); @@ -257,15 +257,24 @@ export async function readSuccessResponseTextOrThrow<T>( /** * Get the timestamp at which the response's content is considered expired. */ -export function getExpiryTimestamp(httpResponse: HttpResponse): Timestamp { +export function getExpiryTimestamp( + httpResponse: HttpResponse, + opt: { minDuration?: Duration }, +): Timestamp { const expiryDateMs = new Date( httpResponse.headers.get("expiry") ?? "", ).getTime(); + let t: Timestamp; if (Number.isNaN(expiryDateMs)) { - return getTimestampNow(); + t = getTimestampNow(); } else { - return { + t = { t_ms: expiryDateMs, - } + }; + } + if (opt.minDuration) { + const t2 = timestampAddDuration(getTimestampNow(), opt.minDuration); + return timestampMax(t, t2); } + return t; } diff --git a/packages/taler-wallet-core/src/util/time.ts b/packages/taler-wallet-core/src/util/time.ts index ff4c1885b..1641924a1 100644 --- a/packages/taler-wallet-core/src/util/time.ts +++ b/packages/taler-wallet-core/src/util/time.ts @@ -76,6 +76,38 @@ export function timestampMin(t1: Timestamp, t2: Timestamp): Timestamp { return { t_ms: Math.min(t1.t_ms, t2.t_ms) }; } +export function timestampMax(t1: Timestamp, t2: Timestamp): Timestamp { + if (t1.t_ms === "never") { + return { t_ms: "never" }; + } + if (t2.t_ms === "never") { + return { t_ms: "never" }; + } + return { t_ms: Math.max(t1.t_ms, t2.t_ms) }; +} + +const SECONDS = 1000 +const MINUTES = SECONDS * 60; +const HOURS = MINUTES * 60; + +export function durationFromSpec(spec: { + seconds?: number, + hours?: number, + minutes?: number, +}): Duration { + let d_ms = 0; + if (spec.seconds) { + d_ms += spec.seconds * SECONDS; + } + if (spec.minutes) { + d_ms += spec.minutes * MINUTES; + } + if (spec.hours) { + d_ms += spec.hours * HOURS; + } + return { d_ms }; +} + /** * Truncate a timestamp so that that it represents a multiple * of seconds. The timestamp is always rounded down. |