本文整理匯總了Python中aniso8601.parse_duration方法的典型用法代碼示例。如果您正苦於以下問題:Python aniso8601.parse_duration方法的具體用法?Python aniso8601.parse_duration怎麽用?Python aniso8601.parse_duration使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類aniso8601
的用法示例。
在下文中一共展示了aniso8601.parse_duration方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_parse_duration
# 需要導入模塊: import aniso8601 [as 別名]
# 或者: from aniso8601 import parse_duration [as 別名]
def test_parse_duration(sutime, input_duration, tomorrow, two_pm):
result = sutime.parse(input_duration)
assert len(result) == 3
assert result[0]["type"] == "DATE"
assert parser.parse(result[0]["value"]).date() == tomorrow
assert result[1]["type"] == "TIME"
assert parser.parse(result[1]["value"]).time() == two_pm
assert result[2]["type"] == "DURATION"
assert aniso8601.parse_duration(result[2]["value"]) == timedelta(hours=2)
示例2: get_timedelta
# 需要導入模塊: import aniso8601 [as 別名]
# 或者: from aniso8601 import parse_duration [as 別名]
def get_timedelta(self, iso8601_time_duration_string):
""" A facade method for the iso8601.parse_duration method that reads a string,
containing an iso8601 time duration value, and returns a datetime.timedelta object.
:param iso8601_time_duration_string: a string containing an iso8601 time duration.
:return: datetime.timedelta
"""
time_delta = None
try:
time_delta = parse_duration(iso8601_time_duration_string)
except Exception as ex:
self.logger.error("Time Duration Unparseable: {td}".format(td=iso8601_time_duration_string))
self.logger.error(ex)
finally:
return time_delta or timedelta(seconds=0)
示例3: to_timedelta
# 需要導入模塊: import aniso8601 [as 別名]
# 或者: from aniso8601 import parse_duration [as 別名]
def to_timedelta(amazon_duration):
return aniso8601.parse_duration(amazon_duration)
示例4: make_timedelta
# 需要導入模塊: import aniso8601 [as 別名]
# 或者: from aniso8601 import parse_duration [as 別名]
def make_timedelta(value):
"""Tries to convert the given value to a :class:`datetime.timedelta`.
Strings will be parsed as ISO 8601 durations.
If a number is provided, it will be interpreted as the number of
seconds.
If a `dict` is provided, does `datetime.timedelta(**value)`.
:param value: something to convert
:type value: str | unicode | float | int | datetime.timedelta | dict
:return: the value after conversion
:rtype: datetime.timedelta
"""
if isinstance(value, basestring):
try:
return aniso8601.parse_duration(value)
except Exception as e:
msg = (
"Conversion to datetime.timedelta failed. Could not "
"parse the given string as an ISO 8601 duration: "
"%s\n\n"
"%s" %
(
repr(value),
e.message,
)
)
raise ValueError(msg)
try:
if isinstance(value, datetime.timedelta):
return value
elif isinstance(value, dict):
return datetime.timedelta(**value)
elif isinstance(value, (float, int)):
return datetime.timedelta(seconds=value)
else:
return datetime.timedelta(value)
except Exception as e:
msg = (
"Could not convert the given value of type '%s' to a "
"datetime.timedelta: %s\n\n"
"%s" %
(
value.__class__.__name__,
repr(value),
e.message,
)
)
raise TypeError(msg) if isinstance(e, TypeError) else ValueError(msg)