From 6b0d348d3d5d6f64008b0e6d9e19a2e00634fa98 Mon Sep 17 00:00:00 2001 From: terencehill Date: Sun, 6 Feb 2022 12:43:38 +0100 Subject: [PATCH] Allow seconds_tostring to work with a negative number of seconds --- qcsrc/lib/string.qh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index c99497bc7..cf724d051 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -117,12 +117,24 @@ string strftime_s() return strcat(ftos(hundreds_of_seconds), seconds_str); } +/// \param[in] seconds number of seconds, can be negative too +/// \return time as "m:ss" string (floored) ERASEABLE -string seconds_tostring(float sec) +string seconds_tostring(float seconds) { - float minutes = floor(sec / 60); - sec -= minutes * 60; - return sprintf("%d:%02d", minutes, sec); + bool negative = false; + if (seconds < 0) + { + negative = true; + seconds = -seconds; + if (floor(seconds) != seconds) + seconds += 1; // make floor work in the other direction + } + int minutes = floor(seconds / 60); + seconds -= minutes * 60; + if (negative) + return sprintf("-%d:%02d", minutes, seconds); + return sprintf("%d:%02d", minutes, seconds); } ERASEABLE -- 2.39.2