Add new `toTimestampNaive` method on `DateTime` so that weekday calculations can be performed for local time rather than for UTC

This commit is contained in:
Nathan Fisher 2024-01-30 00:30:45 -05:00
parent 8f4f120a3b
commit 10fe374ea7
1 changed files with 7 additions and 2 deletions

View File

@ -47,7 +47,7 @@ pub const DateTime = struct {
};
}
pub fn toTimestamp(self: Self) i64 {
fn toTimestampNaive(self: Self) i64 {
var seconds: i64 = 0;
if (self.year.get() < 1970) {
var year = Year.new(1970);
@ -79,6 +79,11 @@ pub const DateTime = struct {
if (self.second) |s| {
seconds += s;
}
return seconds;
}
pub fn toTimestamp(self: Self) i64 {
var seconds = self.toTimestampNaive();
if (self.getOffset()) |ofs| seconds -= ofs.asSeconds();
return seconds;
}
@ -169,7 +174,7 @@ pub const DateTime = struct {
}
pub fn weekday(self: Self) WeekDay {
const ts = self.toTimestamp();
const ts = self.toTimestampNaive();
const days = @divTrunc(ts, SECONDS_PER_DAY);
return @as(WeekDay, @enumFromInt(@rem(days, 7)));
}