]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Allow seconds_tostring to work with a negative number of seconds
authorterencehill <piuntn@gmail.com>
Sun, 6 Feb 2022 11:43:38 +0000 (12:43 +0100)
committerterencehill <piuntn@gmail.com>
Sun, 6 Feb 2022 11:43:38 +0000 (12:43 +0100)
qcsrc/lib/string.qh

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