]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Update the interval's string format function for space.
authorAnt Zucaro <azucaro@gmail.com>
Sat, 25 Nov 2017 21:25:54 +0000 (16:25 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Sat, 25 Nov 2017 21:25:54 +0000 (16:25 -0500)
xonstat/models/main.py
xonstat/templates/main_index.mako

index 2862a25549714749ad37f7d1f6908a042062b047..3eb5e536f951b17eb73bbbb40218a191a92e3977 100644 (file)
@@ -45,20 +45,35 @@ class ActiveServer(object):
         self.server_name = server_name
         self.play_time = play_time
 
-    def play_time_str(self):
-        hour = 3600
-        day = hour * 24
-
+    def play_time_str(self, max_segments=3):
         if not self.play_time:
             return "0m"
 
-        total_seconds = self.play_time.total_seconds()
-        if total_seconds >= day:
-            return "{}d".format(round(float(total_seconds) / day, 1))
-        elif day > total_seconds >= hour:
-            return "{}h".format(round(float(total_seconds) / hour, 1))
-        else:
-            return "{}m".format(round(float(total_seconds) / 60, 1))
+        days, seconds = divmod(self.play_time.total_seconds(), 60*60*24)
+        hours, seconds = divmod(seconds, 60*60)
+        mins, seconds = divmod(seconds, 60)
+
+        parts = []
+        if days > 0 and len(parts) < max_segments:
+            parts.append("{}d".format(int(days)))
+
+        if hours > 0 and len(parts) < max_segments:
+            if len(parts) > 0:
+                prefix = ", "
+            else:
+                prefix = ""
+
+            parts.append("{}{}h".format(prefix, int(hours)))
+
+        if mins > 0 and len(parts) < max_segments:
+            if len(parts) > 0:
+                prefix = ", "
+            else:
+                prefix = ""
+
+            parts.append("{}{}m".format(prefix, int(mins)))
+
+        return "".join(parts)
 
     def __repr__(self):
         return "<ActiveServer({0.sort_order}, {0.server_id})>".format(self)
index ad77d8e39ad541d9d8c7e9bf109e8240af1a7e22..73fd1049ba5cc9f015b789227c971ae897ff6299 100644 (file)
         <tr>
           <td>${ts.sort_order}</td>
           <td class="no-stretch"><a href="${request.route_url('server_info', id=ts.server_id)}" title="Go to the server info page for ${ts.server_name}">${ts.server_name}</a></td>
-          <td>${ts.play_time_str()}</td>
+          <td>${ts.play_time_str(max_segments=2)}</td>
         </tr>
       % endfor
       </tbody>