time: add uefi support

This commit is contained in:
Tristan Ross 2023-12-19 23:43:10 -08:00
parent a86cd91389
commit 23adf09579
No known key found for this signature in database
GPG Key ID: B09C422035669AF8

View File

@ -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.