diff --git a/src/main.zig b/src/main.zig index a1cd595..832f44c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -228,6 +228,16 @@ test "new timezone 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 { year: Year, month: Month, @@ -283,6 +293,12 @@ pub const DateTime = struct { if (self.getOffset()) |ofs| seconds -= ofs.asSeconds(); 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" { @@ -336,3 +352,16 @@ test "to timestamp negative offset" { }; 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); +}