From 4198779b5bf137cd362eb5571a1d68df268d093f Mon Sep 17 00:00:00 2001 From: Dan Peschman Date: Sat, 17 Jan 2015 00:41:37 +0000 Subject: [PATCH] python3: support datetime produced by datetime.isoformat like '2015-01-17T00:15:29.515' --- src/main/resources/python3/swagger.mustache | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/resources/python3/swagger.mustache b/src/main/resources/python3/swagger.mustache index 4986b8548dfb..3b00a14dc95b 100644 --- a/src/main/resources/python3/swagger.mustache +++ b/src/main/resources/python3/swagger.mustache @@ -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)