Skip to content

Commit

Permalink
btrfs-progs: add duration format to fmt_print
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
raczzoli authored and kdave committed Feb 17, 2025
1 parent e94bfcc commit 103338d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions Documentation/dev/dev-json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 timesamps, lik *D days HH:MM:SS* (days not show of 0)

Check failure on line 17 in Documentation/dev/dev-json.rst

View workflow job for this annotation

GitHub Actions / Check for spelling errors

timesamps ==> timestamps

Check failure on line 17 in Documentation/dev/dev-json.rst

View workflow job for this annotation

GitHub Actions / Check for spelling errors

lik ==> like, lick, link

Commands that support json output
---------------------------------
Expand Down
11 changes: 11 additions & 0 deletions common/format-output.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 103338d

Please sign in to comment.