os.uefi: add time to epoch conversion

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

View File

@ -131,6 +131,30 @@ pub const Time = extern struct {
/// Time is to be interpreted as local time
pub const unspecified_timezone: i16 = 0x7ff;
fn daysInYear(year: u16, maxMonth: u4) u32 {
const leapYear: std.time.epoch.YearLeapKind = if (std.time.epoch.isLeapYear(year)) .leap else .not_leap;
var days: u32 = 0;
var month: u4 = 0;
while (month < maxMonth) : (month += 1) {
days += std.time.epoch.getDaysInMonth(leapYear, @enumFromInt(month + 1));
}
return days;
}
pub fn toEpoch(self: std.os.uefi.Time) u64 {
var year: u16 = 0;
var days: u32 = 0;
while (year < (self.year - 1971)) : (year += 1) {
days += daysInYear(year + 1970, 12);
}
days += daysInYear(self.year, @as(u4, @intCast(self.month)) - 1) + self.day;
const hours = self.hour + (days * 24);
const minutes = self.minute + (hours * 60);
return self.second + (minutes * 60);
}
};
/// Capabilities of the clock device