gnunet-svn
[Top][All Lists]
Advanced

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

[taler-taler-ios] 20/29: RelativeTime


From: gnunet
Subject: [taler-taler-ios] 20/29: RelativeTime
Date: Tue, 28 May 2024 09:06:03 +0200

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

marc-stibane pushed a commit to branch master
in repository taler-ios.

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

    RelativeTime
---
 taler-swift/Sources/taler-swift/Time.swift | 45 ++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/taler-swift/Sources/taler-swift/Time.swift 
b/taler-swift/Sources/taler-swift/Time.swift
index 98f0e27..084ad09 100644
--- 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)

-- 
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]