taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

commit 10f6373f868ad25be573090bfa5c695099c25039
parent a6dff37c0dead8f79bcaaaff677f74e6f79bf48d
Author: Marc Stibane <marc@taler.net>
Date:   Fri, 24 May 2024 08:11:38 +0200

RelativeTime

Diffstat:
Mtaler-swift/Sources/taler-swift/Time.swift | 45+++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+), 0 deletions(-)

diff --git a/taler-swift/Sources/taler-swift/Time.swift b/taler-swift/Sources/taler-swift/Time.swift @@ -16,6 +16,51 @@ enum TimestampError: Error { case invalidUInt64Value } +public enum RelativeTime: Codable, Hashable, Sendable { + // Duration in microseconds or "forever" + // to represent an infinite duration. Numeric + // values are capped at 2^53 - 1 inclusive. + case microseconds(UInt64) + case forever + + enum CodingKeys: String, CodingKey { + case d_us = "d_us" + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + do { + self = RelativeTime.microseconds(try container.decode(UInt64.self, forKey: .d_us)) + } catch { // rethrows or never + let stringValue = try container.decode(String.self, forKey: .d_us) + if stringValue == "forever" { + self = RelativeTime.forever + } else { + throw TimestampError.invalidStringRepresentation + } + } + } + public func hash(into hasher: inout Hasher) { + switch self { + case .microseconds(let d_us): + hasher.combine(d_us) + case .forever: + hasher.combine("forever") + } + } + public func encode(to encoder: Encoder) throws { + var value = encoder.container(keyedBy: CodingKeys.self) + switch self { + case .microseconds(let d_us): + try value.encode(d_us, forKey: .d_us) + case .forever: + try value.encode("forever", forKey: .d_us) + } + } +} + + + /// A point in time, represented by milliseconds from January 1, 1970.. public enum Timestamp: Codable, Hashable, Sendable { case milliseconds(UInt64)