Add now method and test

This commit is contained in:
Nathan Fisher 2023-06-13 15:41:48 -04:00
parent 20afb68910
commit 536228c9ee

View File

@ -395,6 +395,10 @@ pub const DateTime = struct {
} }
} }
pub fn now() Self {
return Self.fromTimestamp(std.time.timestamp());
}
pub fn weekday(self: Self) WeekDay { pub fn weekday(self: Self) WeekDay {
const ts = self.toTimestamp(); const ts = self.toTimestamp();
const days = @divTrunc(ts, SECONDS_PER_DAY); const days = @divTrunc(ts, SECONDS_PER_DAY);
@ -471,6 +475,18 @@ test "conversions" {
debug.print("Passed\n", .{}); debug.print("Passed\n", .{});
} }
test "now" {
const ts = std.time.timestamp();
const dt = DateTime.now();
try testing.expect(ts <= dt.toTimestamp());
// Make sure they're identical at least to the tens place, since it's possible
// for them to vary by a second or more depending on system resources
const a = @divTrunc(ts, 10);
const b = @divTrunc(dt.toTimestamp(), 10);
try testing.expectEqual(a, b);
debug.print("Passed\n", .{});
}
test "get weekday" { test "get weekday" {
const dt = DateTime{ const dt = DateTime{
.year = Year.new(2023), .year = Year.new(2023),