Skip to content

Commit

Permalink
Fixed an incorrect formatted last played
Browse files Browse the repository at this point in the history
The incorrect behaviour occured when the .days field of the timedelta
was negative. The old code incorrectly formatted that as '-1dXXh'.

Also fixed a bug that has been in the code since it joined the codebase,
this one was caused by using delta.total_seconds as a value, and not
calling it like a method (it is one). This caused the first conditional to
always fail.
  • Loading branch information
Wesley Bitter committed Oct 4, 2014
1 parent 5b86a22 commit 475b71c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions bot/hanyuu_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,18 +462,24 @@ def lucky(server, nick, channel, text, hostmask):

def search(server, nick, channel, text, hostmask):
def format_date(dt):
minute = 60
hour = minute * 60
day = hour * 24
month = day * 30

if dt is None:
return 'Never'
else:
dt = datetime.now() - dt

if dt.total_seconds < 86400:
return '{h}h{m}m'.format(h=dt.total_seconds / 3600,
m=(dt.total_seconds % 3600) / 60)
elif dt.days > 30:
return '{m}m{d}d'.format(m=dt.days / 30, d=dt.days % 30)
delta = datetime.utcnow() - dt

seconds = delta.total_seconds()
if seconds < day:
return '{h:.0f}h{m:.0f}m'.format(h=seconds / hour,
m=(seconds % hour) / minute)
elif seconds > month:
return '{m:.0f}m{d:.0f}d'.format(m=seconds / month, d=(seconds % month) % day)
else:
return '{d}d{h}h'.format(d=dt.days, h=dt.seconds / 3600)
return '{d:.0f}d{h:.0f}h'.format(d=seconds / day, h=(seconds % day) / hour)

match = re.match(
r"^(?P<mode>[.!@])s(earch)?\s(?P<query>.*)", text, re.I | re.U)
Expand Down

0 comments on commit 475b71c

Please sign in to comment.