本文整理汇总了Python中arrow.parser方法的典型用法代码示例。如果您正苦于以下问题:Python arrow.parser方法的具体用法?Python arrow.parser怎么用?Python arrow.parser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arrow
的用法示例。
在下文中一共展示了arrow.parser方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: grok_datetime
# 需要导入模块: import arrow [as 别名]
# 或者: from arrow import parser [as 别名]
def grok_datetime(dstring):
more_formats = ['YYYYMMDDTHHmmss', 'YYYYMMDDTHHmmssZ', 'YYYYMMDD']
parser = DateTimeParser()
parser.SEPARATORS += ['']
# Try to parse datetime string in regular ISO 8601 format
try:
return parser.parse_iso(dstring)
# Fall back to parse datetime string in additional convenience formats
except arrow.parser.ParserError as ex:
for format in more_formats:
try:
return parser.parse(dstring, format)
except arrow.parser.ParserError as ex:
pass
# Fall back to attempt to parse as relative datetime expression, e.g. "now-10m"
return get_timedelta(dstring)
示例2: iso8601_to_dt
# 需要导入模块: import arrow [as 别名]
# 或者: from arrow import parser [as 别名]
def iso8601_to_dt(iso8601):
"""Given an ISO8601 string as returned by Device Cloud, convert to a datetime object"""
# We could just use arrow.get() but that is more permissive than we actually want.
# Internal (but still public) to arrow is the actual parser where we can be
# a bit more specific
parser = DateTimeParser()
try:
arrow_dt = arrow.Arrow.fromdatetime(parser.parse_iso(iso8601))
return arrow_dt.to('utc').datetime
except ParserError as pe:
raise ValueError("Provided was not a valid ISO8601 string: %r" % pe)
示例3: strict_parse_args
# 需要导入模块: import arrow [as 别名]
# 或者: from arrow import parser [as 别名]
def strict_parse_args(parser, raw_args):
"""
Wrapper around parser.parse_args that raises a ValueError if unexpected
arguments are present.
"""
args = parser.parse_args()
unexpected_params = (set(raw_args) - {allowed_arg.name for allowed_arg in
parser.args})
if unexpected_params:
raise InputError('Unexpected query parameters {}'.format(
unexpected_params))
return args