]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Merge mmsss and mmssss code into a new function called clockedtime_tostring
authorterencehill <piuntn@gmail.com>
Sun, 6 Feb 2022 14:29:29 +0000 (15:29 +0100)
committerterencehill <piuntn@gmail.com>
Sun, 6 Feb 2022 14:29:29 +0000 (15:29 +0100)
qcsrc/lib/string.qh

index cf724d051c3ae1bc57b0d02a6df0566d65fc3286..e62edec9b4ce6e78feed9f5dc5f4cb72799e0e0f 100644 (file)
@@ -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