本文整理汇总了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)