TimeWrapper docs

This commit is contained in:
enkore 2013-10-17 23:24:51 +02:00
parent eb661a0ebb
commit 130f103d07

View File

@ -306,6 +306,23 @@ def formatp(string, **kwargs):
class TimeWrapper: class TimeWrapper:
"""
A wrapper that implements __format__ and __bool__ for time differences and time spans.
:param seconds: seconds (numeric)
:param default_format: the default format to be used if no explicit format_spec is passed to __format__
Format string syntax:
* %h, %m and %s are the hours, minutes and seconds without leading zeros (i.e. 0 to 59 for minutes and seconds)
* %H, %M and %S are padded with a leading zero to two digits, i.e. 00 to 59
* %l and %L produce hours non-padded and padded but only if hours is not zero. If the hours are zero it produces an empty string.
* %% produces a literal %
* %E (only valid on beginning of the string) if the time is null, don't format anything but rather produce an empty string. If the time is non-null it is removed from the string.
The formatted string is stripped, i.e. spaces on both ends of the result are removed
"""
class TimeTemplate(string.Template): class TimeTemplate(string.Template):
delimiter = "%" delimiter = "%"
idpattern = r"[a-zA-Z]" idpattern = r"[a-zA-Z]"
@ -315,9 +332,13 @@ class TimeWrapper:
self.default_format = default_format self.default_format = default_format
def __bool__(self): def __bool__(self):
""":returns: `bool(seconds)`, i.e. False if seconds == 0 and True otherwise
"""
return bool(self.seconds) return bool(self.seconds)
def __format__(self, format_spec): def __format__(self, format_spec):
"""Formats the time span given the format_spec (or the default_format).
"""
format_spec = format_spec or self.default_format format_spec = format_spec or self.default_format
h = self.seconds // 3600 h = self.seconds // 3600
m, s = divmod(self.seconds % 3600, 60) m, s = divmod(self.seconds % 3600, 60)