Add getWeekday
function
This commit is contained in:
parent
0fe32ca124
commit
496970583d
29
src/main.zig
29
src/main.zig
@ -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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user