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
signaturebadge-question-mark 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
12241224 return fbs.pos;
12251225}
12261226
1227/// Formats a number of nanoseconds according to its magnitude:
1228///
1229/// - #ns
1230/// - [#y][#w][#d][#h][#m]#[.###][u|m]s
1231pub fn formatDuration(ns: u64, writer: anytype) !void {
1227fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
12321228 var ns_remaining = ns;
12331229 inline for (.{
12341230 .{ .ns = 365 * std.time.ns_per_day, .sep = 'y' },
......@@ -1270,12 +1266,18 @@ pub fn formatDuration(ns: u64, writer: anytype) !void {
12701266 }
12711267 }
12721268
1273 try formatInt(ns, 10, false, .{}, writer);
1269 try formatInt(ns_remaining, 10, false, .{}, writer);
12741270 try writer.writeAll("ns");
12751271 return;
12761272}
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" {
12791281 var buf: [24]u8 = undefined;
12801282 inline for (.{
12811283 .{ .s = "0ns", .d = 0 },
......@@ -1301,26 +1303,13 @@ test "formatDuration" {
13011303 .{ .s = "1y1h999.999us", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms - 1 },
13021304 .{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms },
13031305 .{ .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 },
13041307 }) |tc| {
1305 const slice = try bufPrint(&buf, "{}", .{duration(tc.d)});
1308 const slice = try bufPrint(&buf, "{}", .{fmtDuration(tc.d)});
13061309 std.testing.expectEqualStrings(tc.s, slice);
13071310 }
13081311}
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
13241313pub const ParseIntError = error{
13251314 /// The result cannot fit in the type specified
13261315 Overflow,