Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
val1, val2 = val.split(':')
val1 = int(val1)
val2 = int(val2)
except ValueError:
# `val1` or `val2` cannot be converted to int
raise ValueError("Cannot parse %r as a time duration." % val)
raise ValueError("Duration '%s' is ambiguous:"
" use '%dm %ds' for %d minutes and %d seconds,"
" or '%dh %dm' for %d hours and %d minutes."
% (val,
val1, val2, val1, val2,
val1, val2, val1, val2))
else:
# strings like `30 seconds` can be parsed by `_Quantity._new_from_string`;
# see http://blogs.gnome.org/jamesh/2005/06/23/overriding-class-methods-in-python/
return super(Duration, cls)._new_from_string(val)
r'((?P[0-9]+) \s* h((ou)?rs?))? \s*'
r'((?P[0-9]+) \s* m(in(ute)?s?))? \s*'
r'((?P[0-9]+) \s* s(ecs?))?'
# 3. everything else is forwarded to `_Quantity._new_from_string`
r')',
re.X|re.I)
_TIMESPEC_VALS = [
# regexp group name
# | unit time lapse as seconds
# | | corresponding `Duration` unit
# | | |
('days1', 24*60*60, Duration.day),
('hours1', 60*60, Duration.hour),
('minutes1', 60, Duration.minute),
('secs1', 1, Duration.s),
('days2', 24*60*60, Duration.day),
('hours2', 60*60, Duration.hour),
('minutes2', 60, Duration.minute),
('secs2', 1, Duration.s),
]
## aliases for common units
B = Memory.B
byte = B
bytes = B
kB = Memory.kB
MB = Memory.MB
GB = Memory.GB
TB = Memory.TB
TB = Memory.TB
KiB = Memory.KiB
MiB = Memory.MiB
GiB = Memory.GiB
TiB = Memory.TiB
s = Duration.s
secs = s
seconds = s
m = Duration.m
mins = m
minutes = m
h = Duration.h
hrs = h
hours = h
d = Duration.d
days = d
## main: run tests
if "__main__" == __name__:
import doctest
doctest.testmod(name="quantity",
optionflags=doctest.NORMALIZE_WHITESPACE)
r'|'
# 2. allow `4days 3hrs`, `1 hour 4 minutes` and abbreviations `1d 2h 4m`
r'((?P[0-9]+) \s* d(ays?))? \s*'
r'((?P[0-9]+) \s* h((ou)?rs?))? \s*'
r'((?P[0-9]+) \s* m(in(ute)?s?))? \s*'
r'((?P[0-9]+) \s* s(ecs?))?'
# 3. everything else is forwarded to `_Quantity._new_from_string`
r')',
re.X|re.I)
_TIMESPEC_VALS = [
# regexp group name
# | unit time lapse as seconds
# | | corresponding `Duration` unit
# | | |
('days1', 24*60*60, Duration.day),
('hours1', 60*60, Duration.hour),
('minutes1', 60, Duration.minute),
('secs1', 1, Duration.s),
('days2', 24*60*60, Duration.day),
('hours2', 60*60, Duration.hour),
('minutes2', 60, Duration.minute),
('secs2', 1, Duration.s),
]
## aliases for common units
B = Memory.B
byte = B
bytes = B
kB = Memory.kB
GiB = Memory.GiB
TiB = Memory.TiB
s = Duration.s
secs = s
seconds = s
m = Duration.m
mins = m
minutes = m
h = Duration.h
hrs = h
hours = h
d = Duration.d
days = d
## main: run tests
if "__main__" == __name__:
import doctest
doctest.testmod(name="quantity",
optionflags=doctest.NORMALIZE_WHITESPACE)
kB = Memory.kB
MB = Memory.MB
GB = Memory.GB
TB = Memory.TB
KiB = Memory.KiB
MiB = Memory.MiB
GiB = Memory.GiB
TiB = Memory.TiB
s = Duration.s
secs = s
seconds = s
m = Duration.m
mins = m
minutes = m
h = Duration.h
hrs = h
hours = h
d = Duration.d
days = d
## main: run tests
if "__main__" == __name__:
import doctest
doctest.testmod(name="quantity",
# 2. allow `4days 3hrs`, `1 hour 4 minutes` and abbreviations `1d 2h 4m`
r'((?P[0-9]+) \s* d(ays?))? \s*'
r'((?P[0-9]+) \s* h((ou)?rs?))? \s*'
r'((?P[0-9]+) \s* m(in(ute)?s?))? \s*'
r'((?P[0-9]+) \s* s(ecs?))?'
# 3. everything else is forwarded to `_Quantity._new_from_string`
r')',
re.X|re.I)
_TIMESPEC_VALS = [
# regexp group name
# | unit time lapse as seconds
# | | corresponding `Duration` unit
# | | |
('days1', 24*60*60, Duration.day),
('hours1', 60*60, Duration.hour),
('minutes1', 60, Duration.minute),
('secs1', 1, Duration.s),
('days2', 24*60*60, Duration.day),
('hours2', 60*60, Duration.hour),
('minutes2', 60, Duration.minute),
('secs2', 1, Duration.s),
]
## aliases for common units
B = Memory.B
byte = B
bytes = B
kB = Memory.kB
MB = Memory.MB
def to_timedelta(duration):
"""
Convert a duration into a Python `datetime.timedelta` object.
This is useful to operate on Python's `datetime.time` and
`datetime.date` objects, which can be added or subtracted to
`datetime.timedelta`.
"""
return datetime.timedelta(seconds=duration.amount(Duration.s))