commit 14d21786e315df4e9ebfcd24e51fcaeb8e4c0147
parent fdd654705289f6e6f07709e23cc14310a46b260d
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 14 Mar 2021 13:02:28 -0400
time: add time::compare
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/time/types.ha b/time/types.ha
@@ -15,3 +15,18 @@ export def HOUR: duration = 60 * MINUTE;
// epoch. For [clock::REALTIME], the epoch is the Unix epoch: January 1st, 1970,
// at midnight, UTC.
export type time = struct { sec: i64, nsec: i64 };
+
+// The return value is negative if a < b, zero if a == b, and positive if a > b.
+export fn compare(a: time, b: time) int = {
+ return if (a.sec < b.sec) -1 else if (a.sec > b.sec) 1 else
+ if (a.nsec < b.nsec) -1 else if (a.nsec > b.nsec) 1 else 0;
+};
+
+@test fn compare() void = {
+ let a = now(clock::MONOTONIC);
+ sleep(1 * MILLISECOND);
+ let b = now(clock::MONOTONIC);
+ assert(compare(a, b) < 0);
+ assert(compare(b, a) > 0);
+ assert(compare(a, a) == 0);
+};