commit 79b5756e08b87a0d2e10ee74093e4040180688c0
parent c1b17e86d9a84e419accdb27e7221658727bc2b1
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 14 Mar 2021 12:57:45 -0400
time: add time::time, time::now
Diffstat:
2 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/time/+linux.ha b/time/+linux.ha
@@ -5,6 +5,16 @@ fn duration_to_timespec(n: duration, ts: *rt::timespec) void = {
ts.tv_nsec = n % SECOND;
};
+fn time_to_timespec(t: time, ts: *rt::timespec) void = {
+ ts.tv_sec = t.sec;
+ ts.tv_nsec = t.nsec;
+};
+
+fn timespec_to_time(ts: rt::timespec) time = time {
+ sec = ts.tv_sec,
+ nsec = ts.tv_nsec,
+};
+
// Yields the process to the kernel and returns after the requested duration.
export fn sleep(n: duration) void = {
let in = rt::timespec { ... };
@@ -24,3 +34,31 @@ export fn sleep(n: duration) void = {
};
};
};
+
+export type clock = enum {
+ // The current wall-clock time. This may jump forwards or backwards in
+ // time to account for leap seconds, NTP adjustments, etc.
+ REALTIME = 0,
+
+ // The current monotonic time. This clock measures from some undefined
+ // epoch and is not affected by leap seconds, NTP adjustments, and
+ // changes to the system time: it always increases by one second per
+ // second.
+ MONOTONIC = 1,
+
+ // Measures CPU time consumed by the calling process.
+ PROCESS_CPU = 2,
+
+ // Time since the system was booted. Increases monotonically and,
+ // unlike [MONOTONIC], continues to tick while the system is suspended.
+ BOOT = 7,
+};
+
+// Returns the current time for a given clock.
+export fn now(clock: clock) time = {
+ let tp = rt::timespec { ... };
+ return match (rt::clock_gettime(clock, &tp)) {
+ void => timespec_to_time(tp),
+ err: rt::errno => abort("Unexpected error from clock_gettime"),
+ };
+};
diff --git a/time/types.ha b/time/types.ha
@@ -8,3 +8,10 @@ export def MILLISECOND: duration = 1000 * MICROSECOND;
export def SECOND: duration = 1000 * MILLISECOND;
export def MINUTE: duration = 60 * SECOND;
export def HOUR: duration = 60 * MINUTE;
+
+// TEMPORARY; MAY CHANGE
+//
+// Represents a specific instant in time as seconds (+nanoseconds) since a given
+// epoch. For [clock::REALTIME], the epoch is the Unix epoch: January 1st, 1970,
+// at midnight, UTC.
+export type time = struct { sec: i64, nsec: i64 };