]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/string.qh
Merge branch 'terencehill/clockedtime_tostring_improvements' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
index 79ba56ebe9738c374b2bec10cf258ef070503a28..c2e77aacc404eda78bc9303a80588fbdb45cc8ca 100644 (file)
@@ -139,23 +139,39 @@ string seconds_tostring(float 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
+/// \param[in] compact if true leading 0s are omitted (except the seconds unit digit)
 /// \return clocked time as "m:ss.t" or "m:ss.th" string (rounded)
 ERASEABLE
-string clockedtime_tostring(int tm, bool hundredths)
+string clockedtime_tostring(int tm, bool hundredths, bool compact)
 {
        if (tm < 0)
-               return strcat("0:00:0", hundredths ? "0" : "");
+       {
+               if (compact)
+                       return strcat("0.0", hundredths ? "0" : "");
+               else
+                       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;
+       tm = floor(tm + 0.5);
+       int minutes = floor(tm / acc);
+       int tm_without_minutes = tm - 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));
+       string s = ftos(acc * 10 + tm_without_minutes);
+       if (!compact || minutes > 0)
+               return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, hundredths ? 2 : 1));
+
+       int ofs = 2, digits = 1;
+       if (tm_without_minutes >= 10 * (hundredths ? 100 : 10))
+       {
+               ofs = 1;
+               digits = 2;
+       }
+       return strcat(substring(s, ofs, digits), ".", substring(s, 3, hundredths ? 2 : 1));
+
 }
 
-#define mmsst(tm) clockedtime_tostring(tm, false)
-#define mmssth(tm) clockedtime_tostring(tm, true)
+#define mmsst(tm, compact) clockedtime_tostring(tm, false, compact)
+#define mmssth(tm, compact) clockedtime_tostring(tm, true, compact)
 
 ERASEABLE
 string format_time(float seconds)