mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 19:22:45 +01:00
Allow a short representation of time elapsed.
This commit is contained in:
parent
c948a257fa
commit
fc2751d04d
13
src/conf.py
13
src/conf.py
@ -273,6 +273,19 @@ for (name, s) in registry._cache.iteritems():
|
||||
###
|
||||
registerGroup(supybot, 'reply')
|
||||
|
||||
registerGroup(supybot.reply, 'format')
|
||||
registerGroup(supybot.reply.format, 'time')
|
||||
registerGroup(supybot.reply.format.time, 'elapsed')
|
||||
registerChannelValue(supybot.reply.format.time.elapsed, 'short',
|
||||
registry.Boolean(False, """Determines whether elapsed times will be given
|
||||
as "1 day, 2 hours, 3 minutes, and 15 seconds" or as "1d 2h 3m 15s"."""))
|
||||
|
||||
originalTimeElapsed = utils.timeElapsed
|
||||
def timeElapsed(*args, **kwargs):
|
||||
kwargs['short'] = supybot.reply.format.time.elapsed.short()
|
||||
return originalTimeElapsed(*args, **kwargs)
|
||||
utils.timeElapsed = timeElapsed
|
||||
|
||||
registerGlobalValue(supybot.reply, 'maximumLength',
|
||||
registry.Integer(512*256, """Determines the absolute maximum length of the
|
||||
bot's reply -- no reply will be passed through the bot with a length
|
||||
|
26
src/utils.py
26
src/utils.py
@ -118,13 +118,18 @@ def abbrev(strings, d=None):
|
||||
del d[key]
|
||||
return d
|
||||
|
||||
def timeElapsed(elapsed, leadingZeroes=False, years=True, weeks=True,
|
||||
days=True, hours=True, minutes=True, seconds=True):
|
||||
def timeElapsed(elapsed, short=False, leadingZeroes=False, years=True,
|
||||
weeks=True, days=True, hours=True, minutes=True, seconds=True):
|
||||
"""Given <elapsed> seconds, returns a string with an English description of
|
||||
how much time as passed. leadingZeroes determines whether 0 days, 0 hours,
|
||||
etc. will be printed; the others determine what larger time periods should
|
||||
be used.
|
||||
"""
|
||||
def format(s, i):
|
||||
if short:
|
||||
return '%s%s' % (i, s[0])
|
||||
else:
|
||||
return nItems(s, i)
|
||||
elapsed = int(elapsed)
|
||||
assert years or weeks or days or \
|
||||
hours or minutes or seconds, 'One flag must be True'
|
||||
@ -134,34 +139,37 @@ def timeElapsed(elapsed, leadingZeroes=False, years=True, weeks=True,
|
||||
if leadingZeroes or yrs:
|
||||
if yrs:
|
||||
leadingZeroes = True
|
||||
ret.append(nItems('year', yrs))
|
||||
ret.append(format('year', yrs))
|
||||
if weeks:
|
||||
wks, elapsed = elapsed // 604800, elapsed % 604800
|
||||
if leadingZeroes or wks:
|
||||
if wks:
|
||||
leadingZeroes = True
|
||||
ret.append(nItems('week', wks))
|
||||
ret.append(format('week', wks))
|
||||
if days:
|
||||
ds, elapsed = elapsed // 86400, elapsed % 86400
|
||||
if leadingZeroes or ds:
|
||||
if ds:
|
||||
leadingZeroes = True
|
||||
ret.append(nItems('day', ds))
|
||||
ret.append(format('day', ds))
|
||||
if hours:
|
||||
hrs, elapsed = elapsed // 3600, elapsed % 3600
|
||||
if leadingZeroes or hrs:
|
||||
if hrs:
|
||||
leadingZeroes = True
|
||||
ret.append(nItems('hour', hrs))
|
||||
ret.append(format('hour', hrs))
|
||||
if minutes or seconds:
|
||||
mins, secs = elapsed // 60, elapsed % 60
|
||||
if leadingZeroes or mins:
|
||||
ret.append(nItems('minute', mins))
|
||||
ret.append(format('minute', mins))
|
||||
if seconds:
|
||||
ret.append(nItems('second', secs))
|
||||
ret.append(format('second', secs))
|
||||
if len(ret) == 0:
|
||||
raise ValueError, 'Time difference not great enough to be noted.'
|
||||
return commaAndify(ret)
|
||||
if short:
|
||||
return ' '.join(ret)
|
||||
else:
|
||||
return commaAndify(ret)
|
||||
|
||||
def distance(s, t):
|
||||
"""Returns the levenshtein edit distance between two strings."""
|
||||
|
Loading…
Reference in New Issue
Block a user