diff --git a/Documentation/dev/dev-json.rst b/Documentation/dev/dev-json.rst index 498381058..b8fd23136 100644 --- a/Documentation/dev/dev-json.rst +++ b/Documentation/dev/dev-json.rst @@ -14,6 +14,7 @@ Supported types: * size: ``size-or-none`` - same as *size* but for 0 it's *none* * UUID: ``uuid`` - if all zeros then *null* (native json), or properly formatted UUID string * date + time: ``date-time`` - timestamp formatted as *YYYY-MM-DD HH:MM:SS TIMEZONE* +* duration: ``duration`` - difference of two timestamps, like *D days HH:MM:SS* (days not show of 0) Commands that support json output --------------------------------- diff --git a/common/format-output.c b/common/format-output.c index 3b333ed5e..555a75232 100644 --- a/common/format-output.c +++ b/common/format-output.c @@ -380,6 +380,17 @@ void fmt_print(struct format_ctx *fctx, const char* key, ...) } else { putchar('-'); } + } else if (strcmp(row->fmt, "duration") == 0) { + const u64 seconds = va_arg(args, u64); + unsigned int days = seconds / (24 * 60 * 60); + unsigned int hours = (seconds % (24 * 60 * 60)) / (60 * 60); + unsigned int minutes = (seconds % (60 * 60)) / 60; + unsigned int sec = seconds % 60; + + if (days > 0) + printf("%u days %02u:%02u:%02u", days, hours, minutes, sec); + else + printf("%02u:%02u:%02u", hours, minutes, sec); } else if (strcmp(row->fmt, "list") == 0) { } else if (strcmp(row->fmt, "map") == 0) { } else if (strcmp(row->fmt, "qgroupid") == 0) {