Python源码示例:arrow.parser()
示例1
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
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
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