Add now
method and test
This commit is contained in:
parent
20afb68910
commit
536228c9ee
16
src/main.zig
16
src/main.zig
@ -395,6 +395,10 @@ pub const DateTime = struct {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn now() Self {
|
||||
return Self.fromTimestamp(std.time.timestamp());
|
||||
}
|
||||
|
||||
pub fn weekday(self: Self) WeekDay {
|
||||
const ts = self.toTimestamp();
|
||||
const days = @divTrunc(ts, SECONDS_PER_DAY);
|
||||
@ -471,6 +475,18 @@ test "conversions" {
|
||||
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" {
|
||||
const dt = DateTime{
|
||||
.year = Year.new(2023),
|
||||
|
Loading…
Reference in New Issue
Block a user