summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/std-formats.c
diff options
context:
space:
mode:
authorNathan Skrzypczak <nathan.skrzypczak@gmail.com>2022-01-24 17:10:41 +0100
committerDamjan Marion <dmarion@me.com>2022-01-30 15:23:09 +0000
commit6de58f5fd0c9d47a65e24d3617e465b9fa8d8872 (patch)
tree23faefcf17170676d278affcda4df84ee49ba8d4 /src/vppinfra/std-formats.c
parentc454e8993d18670f76b03dca780213860c2e19a2 (diff)
cnat: maglev fixes & improvements
This fixes the maglev logic which previously included a wrong simplication. It moves the maglev logic to its own file, and adds a test function in the debug cli. Type: improvement Change-Id: I2790ae2a26fc1c5739ff02f41d436bfcafd5b380 Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
Diffstat (limited to 'src/vppinfra/std-formats.c')
-rw-r--r--src/vppinfra/std-formats.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/vppinfra/std-formats.c b/src/vppinfra/std-formats.c
index 421cb3dda99..99ea0c1a713 100644
--- a/src/vppinfra/std-formats.c
+++ b/src/vppinfra/std-formats.c
@@ -135,6 +135,52 @@ format_white_space (u8 * s, va_list * va)
}
u8 *
+format_duration (u8 *s, va_list *args)
+{
+ f64 t = va_arg (*args, f64);
+ s = format (s, "");
+
+ const f64 seconds_per_minute = 60;
+ const f64 seconds_per_hour = 60 * seconds_per_minute;
+ const f64 seconds_per_day = 24 * seconds_per_hour;
+ uword days, hours, minutes, secs, msecs, usecs;
+
+ days = t / seconds_per_day;
+ t -= days * seconds_per_day;
+
+ hours = t / seconds_per_hour;
+ t -= hours * seconds_per_hour;
+
+ minutes = t / seconds_per_minute;
+ t -= minutes * seconds_per_minute;
+
+ secs = t;
+ t -= secs;
+
+ msecs = 1e3 * t;
+
+ usecs = 1e6 * t;
+ usecs = usecs % 1000;
+
+ if (t == 0.)
+ s = format (s, "0");
+ if (days)
+ s = format (s, "%ddays ", days);
+ if (hours)
+ s = format (s, "%dh ", hours);
+ if (minutes)
+ s = format (s, "%dmin ", minutes);
+ if (secs)
+ s = format (s, "%ds ", secs);
+ if (msecs)
+ s = format (s, "%dms ", msecs);
+ if (usecs)
+ s = format (s, "%dus", usecs);
+
+ return (s);
+}
+
+u8 *
format_time_interval (u8 * s, va_list * args)
{
u8 *fmt = va_arg (*args, u8 *);