From c45ebbfbd283240dde2a68e5fca16ecd0b85eff8 Mon Sep 17 00:00:00 2001 From: terencehill Date: Sun, 6 Feb 2022 15:29:29 +0100 Subject: [PATCH] Merge mmsss and mmssss code into a new function called clockedtime_tostring --- qcsrc/lib/string.qh | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index cf724d051..e62edec9b 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -137,6 +137,26 @@ string seconds_tostring(float seconds) return sprintf("%d:%02d", minutes, seconds); } +/// \param[in] tm integer clocked time in tenths or hundredths, CANNOT be negative +/// \param[in] hundredths if true append hundredths too, otherwise only tenths +/// \return clocked time as "m:ss.t" or "m:ss.th" string (rounded) +ERASEABLE +string clockedtime_tostring(int tm, bool hundredths) +{ + if (tm < 0) + return strcat("0:00:0", hundredths ? "0" : ""); + int acc = hundredths ? 6000 : 600; + int seconds = floor(tm + 0.5); + int minutes = floor(seconds / acc); + seconds -= minutes * acc; + // NOTE: the start digit of s is a placeholder and won't be displayed + string s = ftos(acc * 10 + seconds); + return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, hundredths ? 2 : 1)); +} + +#define mmsss(tm) clockedtime_tostring(tm, false) +#define mmssss(tm) clockedtime_tostring(tm, true) + ERASEABLE string format_time(float seconds) { @@ -151,26 +171,6 @@ string format_time(float seconds) else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds); } -ERASEABLE -string mmsss(float tenths) -{ - tenths = floor(tenths + 0.5); - float minutes = floor(tenths / 600); - tenths -= minutes * 600; - string s = ftos(1000 + tenths); - return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1)); -} - -ERASEABLE -string mmssss(float hundredths) -{ - hundredths = floor(hundredths + 0.5); - float minutes = floor(hundredths / 6000); - hundredths -= minutes * 6000; - string s = ftos(10000 + hundredths); - return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2)); -} - int ColorTranslateMode; ERASEABLE -- 2.39.2