python3: support datetime produced by datetime.isoformat like '2015-01-17T00:15:29.515'

This commit is contained in:
Dan Peschman 2015-01-17 00:41:37 +00:00
parent 711d2c8cf4
commit 4198779b5b

View File

@ -118,12 +118,13 @@ class ApiClient:
for (key, val) in objDict.items()
if key != 'swaggerTypes'}
def _iso8601Format(self, timesep, microsecond, zulu):
def _iso8601Format(self, timesep, microsecond, offset, zulu):
"""Format for parsing a datetime string with given properties.
Args:
timesep -- string separating time from date ('T' or 't')
microsecond -- microsecond portion of time ('.XXX')
offset -- time offset (+/-XX:XX) or None
zulu -- 'Z' or 'z' for UTC, or None for time offset (+/-XX:XX)
Returns:
@ -132,19 +133,21 @@ class ApiClient:
return '%Y-%m-%d{}%H:%M:%S{}{}'.format(
timesep,
'.%f' if microsecond else '',
zulu or '%z')
zulu or ('%z' if offset else ''))
# http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
_iso8601Regex = re.compile(
r'^\d\d\d\d-\d\d-\d\d([Tt])\d\d:\d\d:\d\d(\.\d+)?(([Zz])|(\+|-)\d\d:?\d\d)$')
r'^\d\d\d\d-\d\d-\d\d([Tt])\d\d:\d\d:\d\d(\.\d+)?(([Zz])|(\+|-)\d\d:?\d\d)?$')
def _parseDatetime(self, d):
if d is None:
return None
m = ApiClient._iso8601Regex.match(d)
if not m:
raise Exception('datetime regex match failed "%s"' % d)
timesep, microsecond, offset, zulu, plusminus = m.groups()
format = self._iso8601Format(timesep, microsecond, zulu)
if not zulu:
format = self._iso8601Format(timesep, microsecond, offset, zulu)
if offset and not zulu:
d = d.rsplit(sep=plusminus, maxsplit=1)[0] + offset.replace(':', '')
return datetime.datetime.strptime(d, format)