Allow a short representation of time elapsed.

This commit is contained in:
Jeremy Fincher 2004-12-08 07:26:19 +00:00
parent c948a257fa
commit fc2751d04d
2 changed files with 30 additions and 9 deletions

View File

@ -273,6 +273,19 @@ for (name, s) in registry._cache.iteritems():
### ###
registerGroup(supybot, 'reply') 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', registerGlobalValue(supybot.reply, 'maximumLength',
registry.Integer(512*256, """Determines the absolute maximum length of the 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 bot's reply -- no reply will be passed through the bot with a length

View File

@ -118,13 +118,18 @@ def abbrev(strings, d=None):
del d[key] del d[key]
return d return d
def timeElapsed(elapsed, leadingZeroes=False, years=True, weeks=True, def timeElapsed(elapsed, short=False, leadingZeroes=False, years=True,
days=True, hours=True, minutes=True, seconds=True): weeks=True, days=True, hours=True, minutes=True, seconds=True):
"""Given <elapsed> seconds, returns a string with an English description of """Given <elapsed> seconds, returns a string with an English description of
how much time as passed. leadingZeroes determines whether 0 days, 0 hours, how much time as passed. leadingZeroes determines whether 0 days, 0 hours,
etc. will be printed; the others determine what larger time periods should etc. will be printed; the others determine what larger time periods should
be used. be used.
""" """
def format(s, i):
if short:
return '%s%s' % (i, s[0])
else:
return nItems(s, i)
elapsed = int(elapsed) elapsed = int(elapsed)
assert years or weeks or days or \ assert years or weeks or days or \
hours or minutes or seconds, 'One flag must be True' hours or minutes or seconds, 'One flag must be True'
@ -134,33 +139,36 @@ def timeElapsed(elapsed, leadingZeroes=False, years=True, weeks=True,
if leadingZeroes or yrs: if leadingZeroes or yrs:
if yrs: if yrs:
leadingZeroes = True leadingZeroes = True
ret.append(nItems('year', yrs)) ret.append(format('year', yrs))
if weeks: if weeks:
wks, elapsed = elapsed // 604800, elapsed % 604800 wks, elapsed = elapsed // 604800, elapsed % 604800
if leadingZeroes or wks: if leadingZeroes or wks:
if wks: if wks:
leadingZeroes = True leadingZeroes = True
ret.append(nItems('week', wks)) ret.append(format('week', wks))
if days: if days:
ds, elapsed = elapsed // 86400, elapsed % 86400 ds, elapsed = elapsed // 86400, elapsed % 86400
if leadingZeroes or ds: if leadingZeroes or ds:
if ds: if ds:
leadingZeroes = True leadingZeroes = True
ret.append(nItems('day', ds)) ret.append(format('day', ds))
if hours: if hours:
hrs, elapsed = elapsed // 3600, elapsed % 3600 hrs, elapsed = elapsed // 3600, elapsed % 3600
if leadingZeroes or hrs: if leadingZeroes or hrs:
if hrs: if hrs:
leadingZeroes = True leadingZeroes = True
ret.append(nItems('hour', hrs)) ret.append(format('hour', hrs))
if minutes or seconds: if minutes or seconds:
mins, secs = elapsed // 60, elapsed % 60 mins, secs = elapsed // 60, elapsed % 60
if leadingZeroes or mins: if leadingZeroes or mins:
ret.append(nItems('minute', mins)) ret.append(format('minute', mins))
if seconds: if seconds:
ret.append(nItems('second', secs)) ret.append(format('second', secs))
if len(ret) == 0: if len(ret) == 0:
raise ValueError, 'Time difference not great enough to be noted.' raise ValueError, 'Time difference not great enough to be noted.'
if short:
return ' '.join(ret)
else:
return commaAndify(ret) return commaAndify(ret)
def distance(s, t): def distance(s, t):