added FuzzyDict class, gives a more flexible matching of the time units.
This commit is contained in:
parent
a3266bc1de
commit
30c78107af
|
@ -79,14 +79,27 @@ def fromTime(x):
|
||||||
return cPickle.dumps(datetime.datetime(*time.gmtime(x)[:6], **{'tzinfo': pytz.timezone("UTC")}))
|
return cPickle.dumps(datetime.datetime(*time.gmtime(x)[:6], **{'tzinfo': pytz.timezone("UTC")}))
|
||||||
|
|
||||||
|
|
||||||
timeUnits = {
|
class FuzzyDict(dict):
|
||||||
'm': 60, 'minutes': 60, 'minute': 60, 'min': 60,
|
def __getitem__(self, k):
|
||||||
'h': 3600, 'hours': 3600, 'hour': 3600,
|
try:
|
||||||
'd': 86400, 'days': 86400, 'day': 86400,
|
return dict.__getitem__(self, k)
|
||||||
'w': 604800, 'weeks': 604800, 'week': 604800,
|
except KeyError:
|
||||||
'M': 2592000, 'months': 2592000, 'month': 2592000,
|
# ok, lets find the closest match
|
||||||
'y': 31536000, 'years': 31536000, 'year': 31536000,
|
n = len(k)
|
||||||
}
|
keys = [ s for s in self if s[:n] == k ]
|
||||||
|
if len(keys) != 1:
|
||||||
|
# ambiguous
|
||||||
|
raise
|
||||||
|
return dict.__getitem__(self, keys[0])
|
||||||
|
|
||||||
|
timeUnits = FuzzyDict({
|
||||||
|
'minutes': 60, 'm': 60,
|
||||||
|
'hours' : 3600, 'M': 2592000,
|
||||||
|
'days' : 86400,
|
||||||
|
'weeks' : 604800,
|
||||||
|
'months' : 2592000,
|
||||||
|
'years' : 31536000,
|
||||||
|
})
|
||||||
|
|
||||||
def readTimeDelta(s):
|
def readTimeDelta(s):
|
||||||
"""convert a string like "2 days" or "1h2d3w" into seconds"""
|
"""convert a string like "2 days" or "1h2d3w" into seconds"""
|
||||||
|
|
Loading…
Reference in New Issue