Commit 23adf09579

Tristan Ross <tristan.ross@midstall.com>
2023-12-20 08:43:10
time: add uefi support
1 parent a86cd91
Changed files (1)
lib
lib/std/time.zig
@@ -114,6 +114,13 @@ pub fn nanoTimestamp() i128 {
         return ns;
     }
 
+    if (builtin.os.tag == .uefi) {
+        var value: std.os.uefi.Time = undefined;
+        const status = std.os.uefi.system_table.runtime_services.getTime(&value, null);
+        assert(status == .Success);
+        return @as(i128, @intCast(value.toEpoch())) * ms_per_s;
+    }
+
     var ts: os.timespec = undefined;
     os.clock_gettime(os.CLOCK.REALTIME, &ts) catch |err| switch (err) {
         error.UnsupportedClock, error.Unexpected => return 0, // "Precision of timing depends on hardware and OS".
@@ -176,7 +183,7 @@ pub const Instant = struct {
     // true if we should use clock_gettime()
     const is_posix = switch (builtin.os.tag) {
         .wasi => builtin.link_libc,
-        .windows => false,
+        .windows, .uefi => false,
         else => true,
     };
 
@@ -197,6 +204,13 @@ pub const Instant = struct {
             return Instant{ .timestamp = ns };
         }
 
+        if (builtin.os.tag == .uefi) {
+            var value: std.os.uefi.Time = undefined;
+            const status = std.os.uefi.system_table.runtime_services.getTime(&value, null);
+            if (status != .Success) return error.Unsupported;
+            return Instant{ .timestamp = value.toEpoch() };
+        }
+
         // On darwin, use UPTIME_RAW instead of MONOTONIC as it ticks while suspended.
         // On linux, use BOOTTIME instead of MONOTONIC as it ticks while suspended.
         // On freebsd derivatives, use MONOTONIC_FAST as currently there's no precision tradeoff.