authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-17 20:55:38-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:26-07:00
log09af68de80da4319d552a9eefc3f3e86a4c64caf
treed64150997fa4bcc8b2cc909a6f7bb648a8094d7a
parent051c7af49ad3e3d4575fd63c68cf96eff144bed7

fix duration formatting


1 files changed, 6 insertions(+), 3 deletions(-)

lib/std/io/BufferedWriter.zig+6-3
......@@ -888,6 +888,7 @@ pub fn printInt(
888888 'X' => return printIntOptions(bw, int_value, 16, .upper, options),
889889 'o' => return printIntOptions(bw, int_value, 8, .lower, options),
890890 'B' => return printByteSize(bw, int_value, .decimal, options),
891 'D' => return printDuration(bw, int_value, options),
891892 else => invalidFmtError(fmt, value),
892893 },
893894 2 => {
......@@ -1248,7 +1249,9 @@ pub fn printDurationUnsigned(bw: *BufferedWriter, ns: u64) anyerror!void {
12481249 if (frac > 0) {
12491250 // Write up to 3 decimal places
12501251 var decimal_buf = [_]u8{ '.', 0, 0, 0 };
1251 assert(printInt(decimal_buf[1..], frac, 10, .lower, .{ .fill = '0', .width = 3 }) == 3);
1252 var inner: BufferedWriter = undefined;
1253 inner.initFixed(decimal_buf[1..]);
1254 inner.printIntOptions(frac, 10, .lower, .{ .fill = '0', .width = 3 }) catch unreachable;
12521255 var end: usize = 4;
12531256 while (end > 1) : (end -= 1) {
12541257 if (decimal_buf[end - 1] != '0') break;
......@@ -1272,8 +1275,8 @@ pub fn printDuration(bw: *BufferedWriter, nanoseconds: anytype, options: std.fmt
12721275 var sub_bw: BufferedWriter = undefined;
12731276 sub_bw.initFixed(&buf);
12741277 switch (@typeInfo(@TypeOf(nanoseconds)).int.signedness) {
1275 .signed => sub_bw.printDurationSigned(nanoseconds, options) catch unreachable,
1276 .unsigned => sub_bw.printDurationUnsigned(nanoseconds, options) catch unreachable,
1278 .signed => sub_bw.printDurationSigned(nanoseconds) catch unreachable,
1279 .unsigned => sub_bw.printDurationUnsigned(nanoseconds) catch unreachable,
12771280 }
12781281 return alignBufferOptions(bw, sub_bw.getWritten(), options);
12791282}