Merge pull request #23046 from linusg/uefi-time

Three time-related fixes for UEFI
This commit is contained in:
Linus Groh 2025-03-03 10:07:41 +00:00 committed by GitHub
commit 6d29ef0baf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 40 deletions

View File

@ -113,31 +113,39 @@ pub const Time = extern struct {
/// 0 - 59 /// 0 - 59
second: u8, second: u8,
_pad1: u8,
/// 0 - 999999999 /// 0 - 999999999
nanosecond: u32, nanosecond: u32,
/// The time's offset in minutes from UTC. /// The time's offset in minutes from UTC.
/// Allowed values are -1440 to 1440 or unspecified_timezone /// Allowed values are -1440 to 1440 or unspecified_timezone
timezone: i16, timezone: i16,
daylight: packed struct { daylight: packed struct(u8) {
_pad1: u6,
/// If true, the time has been adjusted for daylight savings time. /// If true, the time has been adjusted for daylight savings time.
in_daylight: bool, in_daylight: bool,
/// If true, the time is affected by daylight savings time. /// If true, the time is affected by daylight savings time.
adjust_daylight: bool, adjust_daylight: bool,
_: u6,
}, },
_pad2: u8,
comptime {
std.debug.assert(@sizeOf(Time) == 16);
}
/// Time is to be interpreted as local time /// Time is to be interpreted as local time
pub const unspecified_timezone: i16 = 0x7ff; pub const unspecified_timezone: i16 = 0x7ff;
fn daysInYear(year: u16, maxMonth: u4) u32 { fn daysInYear(year: u16, max_month: u4) u9 {
const leapYear: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap; const leap_year: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;
var days: u32 = 0; var days: u9 = 0;
var month: u4 = 0; var month: u4 = 0;
while (month < maxMonth) : (month += 1) { while (month < max_month) : (month += 1) {
days += std.time.epoch.getDaysInMonth(leapYear, @enumFromInt(month + 1)); days += std.time.epoch.getDaysInMonth(leap_year, @enumFromInt(month + 1));
} }
return days; return days;
} }
@ -151,9 +159,9 @@ pub const Time = extern struct {
} }
days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day; days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;
const hours = self.hour + (days * 24); const hours: u64 = self.hour + (days * 24);
const minutes = self.minute + (hours * 60); const minutes: u64 = self.minute + (hours * 60);
const seconds = self.second + (minutes * std.time.s_per_min); const seconds: u64 = self.second + (minutes * std.time.s_per_min);
return self.nanosecond + (seconds * std.time.ns_per_s); return self.nanosecond + (seconds * std.time.ns_per_s);
} }
}; };

View File

@ -135,7 +135,7 @@ pub const Instant = struct {
const clock_id = switch (builtin.os.tag) { const clock_id = switch (builtin.os.tag) {
.windows => { .windows => {
// QPC on windows doesn't fail on >= XP/2000 and includes time suspended. // QPC on windows doesn't fail on >= XP/2000 and includes time suspended.
return Instant{ .timestamp = windows.QueryPerformanceCounter() }; return .{ .timestamp = windows.QueryPerformanceCounter() };
}, },
.wasi => { .wasi => {
var ns: std.os.wasi.timestamp_t = undefined; var ns: std.os.wasi.timestamp_t = undefined;
@ -147,7 +147,7 @@ pub const Instant = struct {
var value: std.os.uefi.Time = undefined; var value: std.os.uefi.Time = undefined;
const status = std.os.uefi.system_table.runtime_services.getTime(&value, null); const status = std.os.uefi.system_table.runtime_services.getTime(&value, null);
if (status != .success) return error.Unsupported; if (status != .success) return error.Unsupported;
return Instant{ .timestamp = value.toEpoch() }; return .{ .timestamp = value.toEpoch() };
}, },
// On darwin, use UPTIME_RAW instead of MONOTONIC as it ticks while // On darwin, use UPTIME_RAW instead of MONOTONIC as it ticks while
// suspended. // suspended.
@ -185,7 +185,8 @@ pub const Instant = struct {
/// This assumes that the `earlier` Instant represents a moment in time before or equal to `self`. /// This assumes that the `earlier` Instant represents a moment in time before or equal to `self`.
/// This also assumes that the time that has passed between both Instants fits inside a u64 (~585 yrs). /// This also assumes that the time that has passed between both Instants fits inside a u64 (~585 yrs).
pub fn since(self: Instant, earlier: Instant) u64 { pub fn since(self: Instant, earlier: Instant) u64 {
if (builtin.os.tag == .windows) { switch (builtin.os.tag) {
.windows => {
// We don't need to cache QPF as it's internally just a memory read to KUSER_SHARED_DATA // We don't need to cache QPF as it's internally just a memory read to KUSER_SHARED_DATA
// (a read-only page of info updated and mapped by the kernel to all processes): // (a read-only page of info updated and mapped by the kernel to all processes):
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-kuser_shared_data // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-kuser_shared_data
@ -204,17 +205,18 @@ pub const Instant = struct {
const scale = @as(u64, std.time.ns_per_s << 32) / @as(u32, @intCast(qpf)); const scale = @as(u64, std.time.ns_per_s << 32) / @as(u32, @intCast(qpf));
const result = (@as(u96, qpc) * scale) >> 32; const result = (@as(u96, qpc) * scale) >> 32;
return @as(u64, @truncate(result)); return @as(u64, @truncate(result));
} },
.uefi, .wasi => {
// WASI timestamps are directly in nanoseconds // UEFI and WASI timestamps are directly in nanoseconds
if (builtin.os.tag == .wasi) {
return self.timestamp - earlier.timestamp; return self.timestamp - earlier.timestamp;
} },
else => {
// Convert timespec diff to ns // Convert timespec diff to ns
const seconds = @as(u64, @intCast(self.timestamp.sec - earlier.timestamp.sec)); const seconds = @as(u64, @intCast(self.timestamp.sec - earlier.timestamp.sec));
const elapsed = (seconds * ns_per_s) + @as(u32, @intCast(self.timestamp.nsec)); const elapsed = (seconds * ns_per_s) + @as(u32, @intCast(self.timestamp.nsec));
return elapsed - @as(u32, @intCast(earlier.timestamp.nsec)); return elapsed - @as(u32, @intCast(earlier.timestamp.nsec));
},
}
} }
}; };