Add getWeekday function

This commit is contained in:
Nathan Fisher 2023-06-13 02:06:19 -04:00
parent 0fe32ca124
commit 496970583d

View File

@ -228,6 +228,16 @@ test "new timezone utc" {
try testing.expectEqual(@as(TimeZoneTag, tz1), .utc); try testing.expectEqual(@as(TimeZoneTag, tz1), .utc);
} }
pub const WeekDay = enum(u3) {
thursday = 0,
friday,
saturday,
sunday,
monday,
tuesday,
wednesday,
};
pub const DateTime = struct { pub const DateTime = struct {
year: Year, year: Year,
month: Month, month: Month,
@ -283,6 +293,12 @@ pub const DateTime = struct {
if (self.getOffset()) |ofs| seconds -= ofs.asSeconds(); if (self.getOffset()) |ofs| seconds -= ofs.asSeconds();
return seconds; return seconds;
} }
pub fn weekday(self: Self) WeekDay {
const ts = self.toTimestamp();
const days = @divTrunc(ts, SECONDS_PER_DAY);
return @intToEnum(WeekDay, @rem(days, 7));
}
}; };
test "get year" { test "get year" {
@ -336,3 +352,16 @@ test "to timestamp negative offset" {
}; };
try testing.expectEqual(dt.toTimestamp(), 1686633682); try testing.expectEqual(dt.toTimestamp(), 1686633682);
} }
test "get weekday" {
const dt = DateTime{
.year = Year.new(2023),
.month = .june,
.day = 13,
.hour = 6,
.minute = 21,
.second = 22,
.tz = .utc,
};
try testing.expectEqual(dt.weekday(), .tuesday);
}