diff --git a/earwigbot/commands/lag.py b/earwigbot/commands/lag.py index 34376c0..5b084cb 100644 --- a/earwigbot/commands/lag.py +++ b/earwigbot/commands/lag.py @@ -34,7 +34,8 @@ class Lag(Command): return msg = "\x0302{0}\x0F: Toolserver replag is {1} seconds; database maxlag is {2} seconds." - msg = msg.format(site.name, site.get_replag(), site.get_maxlag()) + replag, maxlag = site.get_replag(), site.get_maxlag() + msg = msg.format(site.name, self.format(replag), self.format(maxlag)) self.reply(data, msg) def get_site(self, data): @@ -74,3 +75,15 @@ class Lag(Command): self.reply(data, msg.format(project, lang)) return return site + + def format(self, seconds): + parts = [("year", 31536000), ("day", 86400), ("hour", 3600), + ("minute", 60), ("second", 1)] + msg = [] + for name, size in parts: + num = seconds / size + seconds -= num * size + if num: + chunk = "{0} {1}".format(num, name if num == 1 else name + "s") + msg.append(chunk) + return ", ".join(msg) if msg else "0 seconds" diff --git a/earwigbot/commands/registration.py b/earwigbot/commands/registration.py index 05eff7c..8180281 100644 --- a/earwigbot/commands/registration.py +++ b/earwigbot/commands/registration.py @@ -60,17 +60,13 @@ class Registration(Command): self.reply(data, msg.format(name, date, gender, age)) def get_diff(self, t1, t2): - parts = {"years": 31536000, "days": 86400, "hours": 3600, - "minutes": 60, "seconds": 1} + parts = [("year", 31536000), ("day", 86400), ("hour", 3600), + ("minute", 60), ("second", 1)] msg = [] - - order = sorted(parts.items(), key=lambda x: x[1], reverse=True) - for key, value in order: - num = 0 - while t2 - t1 > value: - t1 += value - num += 1 - if num or (not num and msg): - msg.append(" ".join((str(num), key))) - - return ", ".join(msg) + for name, size in parts: + num = (t2 - t1) / size + t1 += num * size + if num: + chunk = "{0} {1}".format(num, name if num == 1 else name + "s") + msg.append(chunk) + return ", ".join(msg) if msg else "0 seconds"