Function fmtDuration [src]
Return a Formatter for number of nanoseconds according to its magnitude:
[#y][#w][#d][#h][#m]#[.###][n|u|m]s
Prototype
pub fn fmtDuration(ns: u64) Formatter(formatDuration)
Parameters
ns: u64
Example
test fmtDuration {
var buf: [24]u8 = undefined;
inline for (.{
.{ .s = "0ns", .d = 0 },
.{ .s = "1ns", .d = 1 },
.{ .s = "999ns", .d = std.time.ns_per_us - 1 },
.{ .s = "1us", .d = std.time.ns_per_us },
.{ .s = "1.45us", .d = 1450 },
.{ .s = "1.5us", .d = 3 * std.time.ns_per_us / 2 },
.{ .s = "14.5us", .d = 14500 },
.{ .s = "145us", .d = 145000 },
.{ .s = "999.999us", .d = std.time.ns_per_ms - 1 },
.{ .s = "1ms", .d = std.time.ns_per_ms + 1 },
.{ .s = "1.5ms", .d = 3 * std.time.ns_per_ms / 2 },
.{ .s = "1.11ms", .d = 1110000 },
.{ .s = "1.111ms", .d = 1111000 },
.{ .s = "1.111ms", .d = 1111100 },
.{ .s = "999.999ms", .d = std.time.ns_per_s - 1 },
.{ .s = "1s", .d = std.time.ns_per_s },
.{ .s = "59.999s", .d = std.time.ns_per_min - 1 },
.{ .s = "1m", .d = std.time.ns_per_min },
.{ .s = "1h", .d = std.time.ns_per_hour },
.{ .s = "1d", .d = std.time.ns_per_day },
.{ .s = "1w", .d = std.time.ns_per_week },
.{ .s = "1y", .d = 365 * std.time.ns_per_day },
.{ .s = "1y52w23h59m59.999s", .d = 730 * std.time.ns_per_day - 1 }, // 365d = 52w1d
.{ .s = "1y1h1.001s", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_s + std.time.ns_per_ms },
.{ .s = "1y1h1s", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_s + 999 * std.time.ns_per_us },
.{ .s = "1y1h999.999us", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms - 1 },
.{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms },
.{ .s = "1y1h1ms", .d = 365 * std.time.ns_per_day + std.time.ns_per_hour + std.time.ns_per_ms + 1 },
.{ .s = "1y1m999ns", .d = 365 * std.time.ns_per_day + std.time.ns_per_min + 999 },
.{ .s = "584y49w23h34m33.709s", .d = math.maxInt(u64) },
}) |tc| {
const slice = try bufPrint(&buf, "{}", .{fmtDuration(tc.d)});
try std.testing.expectEqualStrings(tc.s, slice);
}
inline for (.{
.{ .s = "=======0ns", .f = "{s:=>10}", .d = 0 },
.{ .s = "1ns=======", .f = "{s:=<10}", .d = 1 },
.{ .s = " 999ns ", .f = "{s:^10}", .d = std.time.ns_per_us - 1 },
}) |tc| {
const slice = try bufPrint(&buf, tc.f, .{fmtDuration(tc.d)});
try std.testing.expectEqualStrings(tc.s, slice);
}
}
Source
pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
const data = FormatDurationData{ .ns = ns };
return .{ .data = data };
}