authorgravatar for jdknezek@gmail.comJonathan Knezek <jdknezek@gmail.com> 2021-03-07 07:00:15-06:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-03-07 15:00:15+02:00
log0447a2c041a4be843251396e668e074186aa49a2
tree6e0973394e85ef60889f4cad601b085aac6921b8
parent72664df4911f5d5bddead48acde6275f1d5f2a5e
signature Signed by PGP key 4AEE18F83AFDEB23

Implement fmtDuration using Formatter (#8137)

* Implement fmtDuration using Formatter Deprecate Duration, duration * fmtDuration: Fixed ns remainder * fmtDuration: Fixed visibility; removed [Dd]uration

1 files changed, 11 insertions(+), 22 deletions(-)

lib/std/fmt.zig+11-22
...@@ -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}
12261226
1227/// Formats a number of nanoseconds according to its magnitude:1227fn 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
1231pub 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 }
12721268
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}
12771273
1278test "formatDuration" {1274/// Return a Formatter for number of nanoseconds according to its magnitude:
1275/// [#y][#w][#d][#h][#m]#[.###][n|u|m]s
1276pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
1277 return .{ .data = ns };
1278}
1279
1280test "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}
13091312
1310/// Wraps a `u64` to format with `formatDuration`.
1311const 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`.
1320pub fn duration(ns: u64) Duration {
1321 return Duration{ .ns = ns };
1322}
1323
1324pub const ParseIntError = error{1313pub const ParseIntError = error{
1325 /// The result cannot fit in the type specified1314 /// The result cannot fit in the type specified
1326 Overflow,1315 Overflow,