mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 19:22:45 +01:00
utils/str.py: Use already existing function utils.gen.timeElapsed instead of redundant one.
This commit is contained in:
parent
8b4b45ecf0
commit
1e050d7fa8
@ -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.
|
||||
@ -543,7 +507,8 @@ def format(s, *args, **kwargs):
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user