From d6632b1dd281149a7219f910e90072a098cc5345 Mon Sep 17 00:00:00 2001 From: squidy239 Date: Thu, 12 Mar 2026 01:34:40 +0100 Subject: [PATCH] std.Io: added missing toMicroseconds and fromMicroseconds functions (#30099) added toMicroseconds and fromMicroseconds functions to std.Io.Duration and toMicroseconds to std.Io.Timestamp Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30099 Reviewed-by: Andrew Kelley Co-authored-by: squidy239 Co-committed-by: squidy239 --- lib/std/Io.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/std/Io.zig b/lib/std/Io.zig index 8d51f649e3e79180860e02c2e4930e975cce7e33..e191e52eb944ff6d93a0a470f8ab04efb5a319ff 100644 --- a/lib/std/Io.zig +++ b/lib/std/Io.zig @@ -925,6 +925,10 @@ pub const Timestamp = struct { return .{ .nanoseconds = x }; } + pub fn toMicroseconds(t: Timestamp) i64 { + return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_us)); + } + pub fn toMilliseconds(t: Timestamp) i64 { return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_ms)); } @@ -964,6 +968,10 @@ pub const Duration = struct { return .{ .nanoseconds = x }; } + pub fn fromMicroseconds(x: i64) Duration { + return .{ .nanoseconds = @as(i96, x) * std.time.ns_per_us }; + } + pub fn fromMilliseconds(x: i64) Duration { return .{ .nanoseconds = @as(i96, x) * std.time.ns_per_ms }; } @@ -972,6 +980,10 @@ pub const Duration = struct { return .{ .nanoseconds = @as(i96, x) * std.time.ns_per_s }; } + pub fn toMicroseconds(d: Duration) i64 { + return @intCast(@divTrunc(d.nanoseconds, std.time.ns_per_us)); + } + pub fn toMilliseconds(d: Duration) i64 { return @intCast(@divTrunc(d.nanoseconds, std.time.ns_per_ms)); } -- 2.54.0