From 536228c9ee300de5d55dc5d12b2f74c88acb7f05 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 13 Jun 2023 15:41:48 -0400 Subject: [PATCH] Add `now` method and test --- src/main.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.zig b/src/main.zig index 41b7138..28326c2 100644 --- a/src/main.zig +++ b/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),