From 7fb08e618f405c75af52641e34ab0be568d60e4d Mon Sep 17 00:00:00 2001 From: Bruno Date: Wed, 11 Aug 2021 00:17:07 +0200 Subject: [PATCH] Added a READABLE time string conversion --- GstToolkit.cpp | 23 ++++++++++++++++++++++- GstToolkit.h | 3 ++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/GstToolkit.cpp b/GstToolkit.cpp index ea753db..46cf868 100644 --- a/GstToolkit.cpp +++ b/GstToolkit.cpp @@ -14,6 +14,8 @@ string GstToolkit::time_to_string(guint64 t, time_string_mode m) return "00:00:00.00"; case TIME_STRING_MINIMAL: return "0.0"; + case TIME_STRING_READABLE: + return "0 second"; default: return "00.00"; } @@ -23,8 +25,27 @@ string GstToolkit::time_to_string(guint64 t, time_string_mode m) guint s = ms / 1000; ostringstream oss; + // READABLE : long format + if (m == TIME_STRING_READABLE) { + int count = 0; + if (s / 3600) { + oss << s / 3600 << " h "; + count++; + } + if ((s % 3600) / 60) { + oss << (s % 3600) / 60 << " min "; + count++; + } + if (count < 2) { + oss << setw(count > 0 ? 2 : 1) << setfill('0') << (s % 3600) % 60; + count++; + } + if (count < 2 ) + oss << '.'<< setw(2) << setfill('0') << (ms % 1000) / 10; + oss << " second"; + } // MINIMAL: keep only the 2 higher values (most significant) - if (m == TIME_STRING_MINIMAL) { + else if (m == TIME_STRING_MINIMAL) { int count = 0; if (s / 3600) { oss << s / 3600 << ':'; diff --git a/GstToolkit.h b/GstToolkit.h index 646b7c9..ec689b0 100644 --- a/GstToolkit.h +++ b/GstToolkit.h @@ -12,7 +12,8 @@ namespace GstToolkit typedef enum { TIME_STRING_FIXED = 0, TIME_STRING_ADJUSTED, - TIME_STRING_MINIMAL + TIME_STRING_MINIMAL, + TIME_STRING_READABLE } time_string_mode; std::string time_to_string(guint64 t, time_string_mode m = TIME_STRING_ADJUSTED);