diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-07 18:42:18 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-07 18:42:18 +0100 |
commit | 165486a11268ab3d8009506916cd22d233cd248b (patch) | |
tree | b90db78eafd4eb12b84ea2e8f4dc0353a315ab7f /src/util | |
parent | d634626d7f3a179613a86eedb2f1c7a917ce65ba (diff) |
auto-refund
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/helpers.ts | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/src/util/helpers.ts b/src/util/helpers.ts index eb8a1c7b2..3831e84af 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -24,7 +24,7 @@ import { AmountJson } from "./amounts"; import * as Amounts from "./amounts"; -import { Timestamp } from "../walletTypes"; +import { Timestamp, Duration } from "../walletTypes"; /** * Show an amount in a form suitable for the user. @@ -152,25 +152,37 @@ export function extractTalerStampOrThrow(stamp: string): Timestamp { } /** - * Check if a timestamp is in the right format. + * Extract a duration from a Taler duration string. */ -export function timestampCheck(stamp: string): boolean { - return getTalerStampSec(stamp) !== null; +export function extractTalerDuration(duration: string): Duration | undefined { + const m = duration.match(/\/?Delay\(([0-9]*)\)\/?/); + if (!m || !m[1]) { + return undefined; + } + return { + d_ms: parseInt(m[1], 10) * 1000, + }; } - /** - * Get a JavaScript Date object from a Taler date string. - * Returns null if input is not in the right format. + * Extract a duration from a Taler duration string. */ -export function getTalerStampDate(stamp: string): Date | null { - const sec = getTalerStampSec(stamp); - if (sec == null) { - return null; +export function extractTalerDurationOrThrow(duration: string): Duration { + const r = extractTalerDuration(duration); + if (!r) { + throw Error("invalid duration"); } - return new Date(sec * 1000); + return r; +} + +/** + * Check if a timestamp is in the right format. + */ +export function timestampCheck(stamp: string): boolean { + return getTalerStampSec(stamp) !== null; } + /** * Compute the hash function of a JSON object. */ |