From b0333316af23e417b7b44aee3df85893bd60c4ea Mon Sep 17 00:00:00 2001 From: Pablo Alessandro Santos Hugen Date: Fri, 31 Oct 2025 19:49:33 -0300 Subject: [PATCH] refactor: Change Deprecated timestamp function in zig master Nuked here: https://github.com/ziglang/zig/pull/25592/files#diff-85f1b115657b16ddf163856e226c4c94ce6c42d64845781dd19e1614c8603a60L25 --- DiffMatchPatch.zig | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/DiffMatchPatch.zig b/DiffMatchPatch.zig index 0ff3dbd..44e0175 100644 --- a/DiffMatchPatch.zig +++ b/DiffMatchPatch.zig @@ -1,8 +1,11 @@ const DiffMatchPatch = @This(); const std = @import("std"); +const builtin = @import("builtin"); +const time = std.time; const testing = std.testing; const Allocator = std.mem.Allocator; +const Io = std.Io; /// DMP with default configuration options pub const default: DiffMatchPatch = .{}; @@ -17,7 +20,7 @@ pub const Diff = struct { operation: Operation, text: []const u8, - pub fn format(value: Diff, _: anytype, _: anytype, writer: anytype) !void { + pub fn format(value: Diff, writer: *Io.Writer) Io.Writer.Error!void { try writer.print("({s}, \"{s}\")", .{ switch (value.operation) { .equal => "=", @@ -69,7 +72,7 @@ patch_delete_threshold: f32 = 0.5, /// Chunk size for context length. patch_margin: u16 = 4, -pub const DiffError = error{OutOfMemory}; +pub const DiffError = error{ OutOfMemory, SystemClockUnavailable }; /// Find the differences between two texts. The return value /// must be freed with `deinitDiffList(allocator, &diffs)`. @@ -89,13 +92,27 @@ pub fn diff( /// a faster slightly less optimal diff. check_lines: bool, ) DiffError!DiffList { - const deadline = if (dmp.diff_timeout == 0) - std.math.maxInt(u64) - else - @as(u64, @intCast(std.time.milliTimestamp())) + dmp.diff_timeout; + const deadline = + if (dmp.diff_timeout == 0) + std.math.maxInt(u64) + else + try timestampMilli() + dmp.diff_timeout; + return dmp.diffInternal(allocator, before, after, check_lines, deadline); } +fn timestampMilli() DiffError!u64 { + const instant = time.Instant.now() catch return DiffError.SystemClockUnavailable; + const timestamp = instant.since(.{ .timestamp = switch (builtin.os.tag) { + .windows, .uefi, .wasi => 0, + else => .{ + .sec = 0, + .nsec = 0, + }, + } }); + return @as(u64, @intCast(timestamp / time.ns_per_ms)); +} + const DiffList = std.ArrayListUnmanaged(Diff); /// Deinit an `std.ArrayListUnmanaged(Diff)` and the allocated slices of @@ -536,7 +553,7 @@ fn diffBisect( var d: isize = 0; while (d < max_d) : (d += 1) { // Bail out if deadline is reached. - if (@as(u64, @intCast(std.time.milliTimestamp())) > deadline) { + if (try timestampMilli() > deadline) { break; } @@ -2497,12 +2514,12 @@ test diff { .diff_timeout = 100, // 100ms }; - const start_time = std.time.milliTimestamp(); + const start_time = try timestampMilli(); { var time_diff = try with_timout.diff(allocator, a, b, false); defer deinitDiffList(allocator, &time_diff); } - const end_time = std.time.milliTimestamp(); + const end_time = try timestampMilli(); // Test that we took at least the timeout period. try testing.expect(with_timout.diff_timeout <= end_time - start_time); // diff: Timeout min.