From ae1fb8a2fba5d5c57131116f785cb81d2a141b49 Mon Sep 17 00:00:00 2001 From: "Nurul Huda (Apon)" Date: Wed, 27 May 2026 16:38:39 +0600 Subject: [PATCH] fix: rm std.os usage --- example/src/root.zig | 26 ++++++++++++++------------ src/Now.zig | 30 +++++++----------------------- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/example/src/root.zig b/example/src/root.zig index 440e10f..6f50a7f 100644 --- a/example/src/root.zig +++ b/example/src/root.zig @@ -522,18 +522,20 @@ pub fn run(allocator: std.mem.Allocator, io_optional: ?std.Io) !void { try instant_zdt.toString(allocator, .{}), }); - const now_zdt = try Temporal.Now.zonedDateTimeISO(); - defer now_zdt.deinit(); - std.log.info( - \\Now Coverage - \\ - timeZoneId(): {s} - \\ - zonedDateTimeISO(): {s} - \\ - \\ - , .{ - Temporal.Now.timeZoneId(), - try now_zdt.toString(allocator, .{}), - }); + if (io_optional) |io| { + const now_zdt = try Temporal.Now.zonedDateTimeISO(io); + defer now_zdt.deinit(); + std.log.info( + \\Now Coverage + \\ - timeZoneId(): {s} + \\ - zonedDateTimeISO(): {s} + \\ + \\ + , .{ + Temporal.Now.timeZoneId(), + try now_zdt.toString(allocator, .{}), + }); + } const date_cal = try Temporal.PlainDate.calInit(2024, 2, 2, "iso8601"); const date_from = try Temporal.PlainDate.from("2024-02-02"); diff --git a/src/Now.zig b/src/Now.zig index 91da473..9ad8ec5 100644 --- a/src/Now.zig +++ b/src/Now.zig @@ -87,9 +87,10 @@ pub fn timeZoneId() []const u8 { /// Returns the current date and time as a [`Temporal.ZonedDateTime`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime) object, in the ISO 8601 calendar and the specified time zone. /// /// See: [MDN Temporal.Now.zonedDateTimeISO](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Now/zonedDateTimeISO) -pub fn zonedDateTimeISO() !ZonedDateTime { +pub fn zonedDateTimeISO(io: std.Io) !ZonedDateTime { + const now = try instant(io); const time_zone = try ZonedDateTime.TimeZone.init(timeZoneId()); - return ZonedDateTime.fromEpochNanoseconds(currentEpochNanoseconds(), time_zone); + return ZonedDateTime.fromEpochNanoseconds(now.epochNanoseconds(), time_zone); } const CurrentParts = struct { @@ -134,26 +135,6 @@ fn currentParts(io: std.Io) CurrentParts { }; } -fn currentEpochNanoseconds() i128 { - return switch (builtin.os.tag) { - .windows => blk: { - const epoch_ns = std.time.epoch.windows * std.time.ns_per_s; - break :blk @as(i128, @intCast(std.os.windows.ntdll.RtlGetSystemTimePrecise())) * 100 + epoch_ns; - }, - else => blk: { - var timespec: std.posix.timespec = undefined; - switch (std.posix.errno(std.posix.system.clock_gettime(.REALTIME, ×pec))) { - .SUCCESS => {}, - else => return 0, - } - - const seconds: i128 = @intCast(timespec.sec); - const nanos: i128 = @intCast(timespec.nsec); - break :blk seconds * std.time.ns_per_s + nanos; - }, - }; -} - // ---------- Tests --------------------- test instant { const io = std.testing.io; @@ -190,9 +171,12 @@ test timeZoneId { try std.testing.expectEqualStrings("UTC", tz); } test zonedDateTimeISO { - const zdt = try zonedDateTimeISO(); + const io = std.testing.io; + const zdt = try zonedDateTimeISO(io); defer zdt.deinit(); + std.log.info("{d}", .{zdt.epochNanoseconds()}); + try std.testing.expect(zdt.epochNanoseconds() > 0); const tz = try zdt.timeZoneId(std.testing.allocator); defer std.testing.allocator.free(tz);