| ... | @@ -1224,11 +1224,7 @@ pub fn formatIntBuf(out_buf: []u8, value: anytype, base: u8, uppercase: bool, op | ... | @@ -1224,11 +1224,7 @@ pub fn formatIntBuf(out_buf: []u8, value: anytype, base: u8, uppercase: bool, op |
| 1224 | return fbs.pos; | 1224 | return fbs.pos; |
| 1225 | } | 1225 | } |
| 1226 | | 1226 | |
| 1227 | /// Formats a number of nanoseconds according to its magnitude: | 1227 | fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 1228 | /// | | |
| 1229 | /// - #ns | | |
| 1230 | /// - [#y][#w][#d][#h][#m]#[.###][u|m]s | | |
| 1231 | pub fn formatDuration(ns: u64, writer: anytype) !void { | | |
| 1232 | var ns_remaining = ns; | 1228 | var ns_remaining = ns; |
| 1233 | inline for (.{ | 1229 | inline for (.{ |
| 1234 | .{ .ns = 365 * std.time.ns_per_day, .sep = 'y' }, | 1230 | .{ .ns = 365 * std.time.ns_per_day, .sep = 'y' }, |
| ... | @@ -1270,12 +1266,18 @@ pub fn formatDuration(ns: u64, writer: anytype) !void { | ... | @@ -1270,12 +1266,18 @@ pub fn formatDuration(ns: u64, writer: anytype) !void { |
| 1270 | } | 1266 | } |
| 1271 | } | 1267 | } |
| 1272 | | 1268 | |
| 1273 | try formatInt(ns, 10, false, .{}, writer); | 1269 | try formatInt(ns_remaining, 10, false, .{}, writer); |
| 1274 | try writer.writeAll("ns"); | 1270 | try writer.writeAll("ns"); |
| 1275 | return; | 1271 | return; |
| 1276 | } | 1272 | } |
| 1277 | | 1273 | |
| 1278 | test "formatDuration" { | 1274 | /// Return a Formatter for number of nanoseconds according to its magnitude: |
| | 1275 | /// [#y][#w][#d][#h][#m]#[.###][n|u|m]s |
| | 1276 | pub fn fmtDuration(ns: u64) Formatter(formatDuration) { |
| | 1277 | return .{ .data = ns }; |
| | 1278 | } |
| | 1279 | |
| | 1280 | test "fmtDuration" { |
| 1279 | var buf: [24]u8 = undefined; | 1281 | var buf: [24]u8 = undefined; |
| 1280 | inline for (.{ | 1282 | inline for (.{ |
| 1281 | .{ .s = "0ns", .d = 0 }, | 1283 | .{ .s = "0ns", .d = 0 }, |
| ... | @@ -1301,26 +1303,13 @@ test "formatDuration" { | ... | @@ -1301,26 +1303,13 @@ test "formatDuration" { |
| 1301 | .{ .s = "1y1h999.999us", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms - 1 }, | 1303 | .{ .s = "1y1h999.999us", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms - 1 }, |
| 1302 | .{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms }, | 1304 | .{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms }, |
| 1303 | .{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms + 1 }, | 1305 | .{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms + 1 }, |
| | 1306 | .{ .s = "1y1m999ns", .d = 365 * std.time.ns_per_day + std.time.ns_per_min + 999 }, |
| 1304 | }) |tc| { | 1307 | }) |tc| { |
| 1305 | const slice = try bufPrint(&buf, "{}", .{duration(tc.d)}); | 1308 | const slice = try bufPrint(&buf, "{}", .{fmtDuration(tc.d)}); |
| 1306 | std.testing.expectEqualStrings(tc.s, slice); | 1309 | std.testing.expectEqualStrings(tc.s, slice); |
| 1307 | } | 1310 | } |
| 1308 | } | 1311 | } |
| 1309 | | 1312 | |
| 1310 | /// Wraps a `u64` to format with `formatDuration`. | | |
| 1311 | const Duration = struct { | | |
| 1312 | ns: u64, | | |
| 1313 | | | |
| 1314 | pub fn format(self: Duration, comptime fmt: []const u8, options: FormatOptions, writer: anytype) !void { | | |
| 1315 | return formatDuration(self.ns, writer); | | |
| 1316 | } | | |
| 1317 | }; | | |
| 1318 | | | |
| 1319 | /// Formats a number of nanoseconds according to its magnitude. See `formatDuration`. | | |
| 1320 | pub fn duration(ns: u64) Duration { | | |
| 1321 | return Duration{ .ns = ns }; | | |
| 1322 | } | | |
| 1323 | | | |
| 1324 | pub const ParseIntError = error{ | 1313 | pub const ParseIntError = error{ |
| 1325 | /// The result cannot fit in the type specified | 1314 | /// The result cannot fit in the type specified |
| 1326 | Overflow, | 1315 | Overflow, |