Skip to content

Commit 1480f4a

Browse files
raczzolikdave
authored andcommitted
btrfs-progs: add duration format to fmt_print
Add "duration" format in seconds to fmt_print which will convert the input to one of the following strings: 1. if number of seconds represents more than one day, the output will be for example: "1 days 01:30:00" (left the plural so parsing back the string is easier) 2. if less then a day: "23:30:10" Author: Racz Zoltan <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent a1ea2a3 commit 1480f4a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Documentation/dev/dev-json.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Supported types:
1414
* size: ``size-or-none`` - same as *size* but for 0 it's *none*
1515
* UUID: ``uuid`` - if all zeros then *null* (native json), or properly formatted UUID string
1616
* date + time: ``date-time`` - timestamp formatted as *YYYY-MM-DD HH:MM:SS TIMEZONE*
17+
* duration: ``duration`` - difference of two timesamps, lik *D days HH:MM:SS* (days not show of 0)
1718

1819
Commands that support json output
1920
---------------------------------

common/format-output.c

+11
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ void fmt_print(struct format_ctx *fctx, const char* key, ...)
380380
} else {
381381
putchar('-');
382382
}
383+
} else if (strcmp(row->fmt, "duration") == 0) {
384+
const u64 seconds = va_arg(args, u64);
385+
unsigned int days = seconds / (24 * 60 * 60);
386+
unsigned int hours = (seconds % (24 * 60 * 60)) / (60 * 60);
387+
unsigned int minutes = (seconds % (60 * 60) / 60;
388+
unsigned int sec = seconds % 60;
389+
390+
if (days > 0)
391+
printf("%u days %02u:%02u:%02u", days, hours, minutes, sec);
392+
else
393+
printf("%02u:%02u:%02u", hours, minutes, sec);
383394
} else if (strcmp(row->fmt, "list") == 0) {
384395
} else if (strcmp(row->fmt, "map") == 0) {
385396
} else if (strcmp(row->fmt, "qgroupid") == 0) {

0 commit comments

Comments
 (0)