瀏覽代碼

Format time deltas more nicely.

tags/v0.1^2
Ben Kurtovic 12 年之前
父節點
當前提交
700562c2a0
共有 2 個檔案被更改,包括 23 行新增14 行删除
  1. +14
    -1
      earwigbot/commands/lag.py
  2. +9
    -13
      earwigbot/commands/registration.py

+ 14
- 1
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"

+ 9
- 13
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"

Loading…
取消
儲存