From e6fb01c6f0c98ca0372e77900343cb56af0b7092 Mon Sep 17 00:00:00 2001 From: agrevster <138630000+agrevster@users.noreply.github.com> Date: Sun, 5 Jul 2026 13:42:17 -0500 Subject: [PATCH] Zig 0.16 support --- build.zig.zon | 4 +-- src/datetime.zig | 78 +++++++++++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index c670e80..ebe2173 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,8 +1,8 @@ .{ .name = .datetime, - .version = "0.8.0", + .version = "0.9.0", .fingerprint = 0x93f3c6cacc579370, // Changing this has security and trust implications. - + .minimum_zig_version = "0.16.0", .paths = .{ "src", "build.zig", diff --git a/src/datetime.zig b/src/datetime.zig index c614031..268e84b 100644 --- a/src/datetime.zig +++ b/src/datetime.zig @@ -308,8 +308,14 @@ pub const Date = struct { } // Returns todays date - pub fn now() Date { - return Date.fromTimestamp(time.milliTimestamp()); + pub fn now(io: std.Io) error{ClockError}!Date { + const clock = std.Io.Clock.real; + const res = clock.resolution(io) catch { + return error.ClockError; + }; + if (res.nanoseconds == 0) return error.ClockError; + + return Date.fromTimestamp(clock.now(io).toMilliseconds()); } // Create a date from the number of seconds since 1 Jan 1970 @@ -429,8 +435,8 @@ pub const Date = struct { return std.fmt.bufPrint(buf, ISO_DATE_FMT, .{ self.year, self.month, self.day }); } - pub fn writeIso(self: Date, writer: anytype) !void { - try std.fmt.format(writer, ISO_DATE_FMT, .{ self.year, self.month, self.day }); + pub fn writeIso(self: Date, writer: *std.Io.Writer) !void { + try writer.print(ISO_DATE_FMT, .{ self.year, self.month, self.day }); } // ------------------------------------------------------------------------ @@ -538,7 +544,7 @@ pub const Date = struct { }; test "date-now" { - _ = Date.now(); + _ = try Date.now(std.testing.io); } test "date-compare" { @@ -732,10 +738,10 @@ test "date-write-iso" { for (date_strs) |date_str| { var buf: [32]u8 = undefined; - var stream = std.io.fixedBufferStream(buf[0..]); var d = try Date.parseIso(date_str); - try d.writeIso(stream.writer()); - try testing.expectEqualStrings(date_str, stream.getWritten()); + var buf_writer = std.Io.Writer.fixed(&buf); + try d.writeIso(&buf_writer); + try testing.expectEqualStrings(date_str, buf_writer.buffered()); } } @@ -843,8 +849,13 @@ pub const Time = struct { // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ - pub fn now() Time { - return Time.fromTimestamp(time.milliTimestamp()); + pub fn now(io: std.Io) error{ClockError}!Time { + const clock = std.Io.Clock.real; + const res = clock.resolution(io) catch { + return error.ClockError; + }; + if (res.nanoseconds == 0) return error.ClockError; + return Time.fromTimestamp(clock.now(io).toMilliseconds()); } // Create a Time struct and validate that all fields are in range @@ -978,12 +989,12 @@ pub const Time = struct { const ISO_HM_FORMAT = "T{d:0>2}:{d:0>2}"; const ISO_HMS_FORMAT = "T{d:0>2}:{d:0>2}:{d:0>2}"; - pub fn writeIsoHM(self: Time, writer: anytype) !void { - try std.fmt.format(writer, ISO_HM_FORMAT, .{ self.hour, self.minute }); + pub fn writeIsoHM(self: Time, writer: *std.Io.Writer) !void { + try writer.print(ISO_HM_FORMAT, .{ self.hour, self.minute }); } - pub fn writeIsoHMS(self: Time, writer: anytype) !void { - try std.fmt.format(writer, ISO_HMS_FORMAT, .{ self.hour, self.minute, self.second }); + pub fn writeIsoHMS(self: Time, writer: *std.Io.Writer) !void { + try writer.print(ISO_HMS_FORMAT, .{ self.hour, self.minute, self.second }); } }; @@ -1001,7 +1012,7 @@ test "time-create" { } test "time-now" { - _ = Time.now(); + _ = try Time.now(std.testing.io); } test "time-from-seconds" { @@ -1061,22 +1072,20 @@ test "time-write-iso-hm" { const t = Time.fromTimestamp(1574908586928); var buf: [6]u8 = undefined; - var fbs = std.io.fixedBufferStream(buf[0..]); - - try t.writeIsoHM(fbs.writer()); + var writer = std.Io.Writer.fixed(&buf); - try testing.expectEqualSlices(u8, "T02:36", fbs.getWritten()); + try t.writeIsoHM(&writer); + try testing.expectEqualSlices(u8, "T02:36", writer.buffered()); } test "time-write-iso-hms" { const t = Time.fromTimestamp(1574908586928); var buf: [9]u8 = undefined; - var fbs = std.io.fixedBufferStream(buf[0..]); + var writer = std.Io.Writer.fixed(&buf); - try t.writeIsoHMS(fbs.writer()); - - try testing.expectEqualSlices(u8, "T02:36:26", fbs.getWritten()); + try t.writeIsoHMS(&writer); + try testing.expectEqualSlices(u8, "T02:36:26", writer.buffered()); } pub const Datetime = struct { @@ -1156,8 +1165,13 @@ pub const Datetime = struct { // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ - pub fn now() Datetime { - return Datetime.fromTimestamp(time.milliTimestamp()); + pub fn now(io: std.Io) error{ClockError}!Datetime { + const clock = std.Io.Clock.real; + const res = clock.resolution(io) catch { + return error.ClockError; + }; + if (res.nanoseconds == 0) return error.ClockError; + return Datetime.fromTimestamp(clock.now(io).toMilliseconds()); } pub fn create(year: u32, month: u32, day: u32, hour: u32, minute: u32, second: u32, nanosecond: u32, zone: ?Timezone) !Datetime { @@ -1505,7 +1519,7 @@ pub const Datetime = struct { }; test "datetime-now" { - _ = Datetime.now(); + _ = try Datetime.now(std.testing.io); } test "datetime-create-timestamp" { @@ -1617,7 +1631,7 @@ test "datetime-compare" { const dt2 = try Datetime.fromDate(2016, 12, 2); try testing.expect(dt2.lt(dt1)); - const dt3 = Datetime.now(); + const dt3 = try Datetime.now(std.testing.io); try testing.expect(dt3.gt(dt2)); const dt4 = try dt3.copy(); @@ -1679,10 +1693,12 @@ test "datetime-parse-modified-since" { } test "file-modified-date" { - var f = try std.fs.cwd().openFile("README.md", .{}); - const stat = try f.stat(); + const io = std.testing.io; + var f = try std.Io.Dir.cwd().openFile(io, "README.md", .{}); + const stat = try f.stat(io); + var buf: [32]u8 = undefined; - const str = try Datetime.formatHttpFromModifiedDate(&buf, stat.mtime); + const str = try Datetime.formatHttpFromModifiedDate(&buf, stat.mtime.nanoseconds); std.log.warn("Modtime: {s}\n", .{str}); } @@ -1695,7 +1711,7 @@ test "readme-example" { assert(next_year.day == 1); // In UTC - const now = Datetime.now(); + const now = try Datetime.now(std.testing.io); const now_str = try now.formatHttp(allocator); defer allocator.free(now_str); std.log.warn("The time is now: {s}\n", .{now_str});