From 2da397ecc005befafd585a8af49303113e2fd03f Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Tue, 7 Nov 2017 21:59:03 -0500 Subject: [PATCH] Use an instance method to format the play time string. --- xonstat/models/main.py | 16 ++++++++++++++++ xonstat/templates/main_index.mako | 4 ++-- xonstat/templates/top_servers_index.mako | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/xonstat/models/main.py b/xonstat/models/main.py index 0648623..2862a25 100644 --- a/xonstat/models/main.py +++ b/xonstat/models/main.py @@ -2,6 +2,7 @@ Models related to the main index page. """ +import datetime from xonstat.util import html_colors @@ -44,6 +45,21 @@ class ActiveServer(object): self.server_name = server_name self.play_time = play_time + def play_time_str(self): + hour = 3600 + day = hour * 24 + + 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)) + def __repr__(self): return "".format(self) diff --git a/xonstat/templates/main_index.mako b/xonstat/templates/main_index.mako index 6641d96..75f7854 100644 --- a/xonstat/templates/main_index.mako +++ b/xonstat/templates/main_index.mako @@ -103,7 +103,7 @@ # Server - Games + Play Time @@ -111,7 +111,7 @@ ${ts.sort_order} ${ts.server_name} - ${ts.games} + ${ts.play_time_str()} % endfor diff --git a/xonstat/templates/top_servers_index.mako b/xonstat/templates/top_servers_index.mako index 4adba45..836e36f 100644 --- a/xonstat/templates/top_servers_index.mako +++ b/xonstat/templates/top_servers_index.mako @@ -24,7 +24,7 @@ # Server - Games + Time @@ -32,7 +32,7 @@ ${ts.sort_order} ${ts.server_name} - ${ts.games} + ${ts.play_time_str()} % endfor -- 2.39.2