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 <racz.zoli@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Racz Zoltan 2025-02-12 01:29:18 +02:00 committed by David Sterba
parent 0416dafaea
commit ed8b167de9
2 changed files with 12 additions and 0 deletions

View File

@ -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
---------------------------------

View File

@ -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) {