Skip to content

Commit

Permalink
utils/str.py: Use already existing function utils.gen.timeElapsed ins…
Browse files Browse the repository at this point in the history
…tead of redundant one.
  • Loading branch information
progval committed Aug 11, 2013
1 parent 8b4b45e commit 1e050d7
Showing 1 changed file with 2 additions and 37 deletions.
39 changes: 2 additions & 37 deletions src/utils/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,42 +417,6 @@ def timestamp(t):
t = time.time()
return time.ctime(t)

# adapted from
# https://github.com/liudmil-mitev/experiments/blob/master/time/humanize_time.py
def timedelta(amount, units='second'):
'''
Divide `amount` in time periods.
Useful for making time intervals more human readable.
>>> humanize_time(173, "hour")
[(1, 'week'), (5, 'hours')]
>>> humanize_time(17313, "second")
[(4, 'hours'), (48, 'minutes'), (33, 'seconds')]
>>> humanize_time(90, "week")
[(1, 'year'), (10, 'months'), (2, 'weeks')]
>>> humanize_time(42, "month")
[(3, 'years'), (6, 'months')]
>>> humanize_time(500, "day")
[(1, 'year'), (5, 'months'), (3, 'weeks'), (3, 'days')]
'''
INTERVALS = [1, 60, 3600, 86400, 604800, 2419200, 29030400]
NAMES = (_('second'), _('minute'), _('hour'), _('day'),
_('week'), _('month'), _('year'))

result = []

unit = NAMES.index(units)
# Convert to seconds
amount = amount * INTERVALS[unit]

for i in range(len(NAMES)-1, -1, -1):
a = int(amount // INTERVALS[i])
if a > 0:
result.append(nItems(a, NAMES[i]))
amount -= a * INTERVALS[i]

return ' '.join(result)

_formatRe = re.compile('%((?:\d+)?\.\d+f|[bfhiLnpqrsStTuv%])')
def format(s, *args, **kwargs):
"""w00t.
Expand Down Expand Up @@ -543,7 +507,8 @@ def sub(match):
elif char == 't':
return timestamp(args.pop())
elif char == 'T':
return timedelta(args.pop())
from gen import timeElapsed
return timeElapsed(args.pop())
elif char == 'u':
import supybot.conf as conf
return conf.supybot.reply.format.url() % args.pop()
Expand Down

0 comments on commit 1e050d7

Please sign in to comment.