From: Ant Zucaro Date: Sat, 25 Nov 2017 21:25:54 +0000 (-0500) Subject: Update the interval's string format function for space. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=ea9415fa94e5ae720c36162302e4d56775f19733;p=xonotic%2Fxonstat.git Update the interval's string format function for space. --- diff --git a/xonstat/models/main.py b/xonstat/models/main.py index 2862a25..3eb5e53 100644 --- a/xonstat/models/main.py +++ b/xonstat/models/main.py @@ -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 "".format(self) diff --git a/xonstat/templates/main_index.mako b/xonstat/templates/main_index.mako index ad77d8e..73fd104 100644 --- a/xonstat/templates/main_index.mako +++ b/xonstat/templates/main_index.mako @@ -111,7 +111,7 @@ ${ts.sort_order} ${ts.server_name} - ${ts.play_time_str()} + ${ts.play_time_str(max_segments=2)} % endfor