diff mbox series

btrfs-progs: add duration format to fmt_print

Message ID 20250211232918.153958-1-racz.zoli@gmail.com (mailing list archive)
State New
Headers show
Series btrfs-progs: add duration format to fmt_print | expand

Commit Message

Racz Zoltan Feb. 11, 2025, 11:29 p.m. UTC
Added "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"

---
 common/format-output.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

David Sterba Feb. 13, 2025, 4:27 a.m. UTC | #1
On Wed, Feb 12, 2025 at 01:29:18AM +0200, Racz Zoltan wrote:
> Added "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"

Makes sense to split it like that. I remember seeing the day formatted
as "1d" so this can be simplifed like that and it remains parseable.
I can adjust that or keep it as you sent it, this is more about how
it's expected to be than how we'd prefer it.  This is supposed to be in
the json format so some real examples would help to decide. Thanks.
David Sterba Feb. 17, 2025, 4:37 p.m. UTC | #2
On Wed, Feb 12, 2025 at 01:29:18AM +0200, Racz Zoltan wrote:
> Added "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"

Added to devel, thanks.
diff mbox series

Patch

diff --git a/common/format-output.c b/common/format-output.c
index 3b333ed5..146f23a3 100644
--- a/common/format-output.c
+++ b/common/format-output.c
@@ -380,6 +380,19 @@  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 / 86400;
+		unsigned int hours = (seconds % 86400) / 3600;
+		unsigned int minutes = (seconds % 3600) / 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) {