Skip to content

Commit 9ff1979

Browse files
committed
fix issue related to ISO-8601 datetime string
- #83 - it seems the isoformat provided by python has ‘microsecond’ field.
1 parent 6fefdce commit 9ff1979

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

pyswagger/tests/test_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ def test_iso8601_convert_from_string(self):
1717
self.assertEqual(utils.from_iso8601('2007-04-05T12:30:00-02:00'), datetime(2007, 4, 5, 14, 30, tzinfo=utils.FixedTZ(0, 0)))
1818
self.assertEqual(utils.from_iso8601('2007-04-05T00:00:00'), datetime(2007, 4, 5, 0, 0, 0))
1919
self.assertEqual(utils.from_iso8601('2007-04-05T00:00:00Z'), datetime(2007, 4, 5, 0, 0, 0, tzinfo=utils.FixedTZ(0, 0)))
20-
20+
# microsecond
21+
self.assertEqual(utils.from_iso8601('2007-04-05T14:30:24.1'), datetime(2007, 4, 5, 14, 30, 24, 100000))
22+
self.assertEqual(utils.from_iso8601('2007-04-05T14:30:24.11'), datetime(2007, 4, 5, 14, 30, 24, 110000))
23+
self.assertEqual(utils.from_iso8601('2007-04-05T14:30:24.111'), datetime(2007, 4, 5, 14, 30, 24, 111000))
24+
self.assertEqual(utils.from_iso8601('2007-04-05T14:30:24.1111'), datetime(2007, 4, 5, 14, 30, 24, 111100))
25+
self.assertEqual(utils.from_iso8601('2007-04-05T14:30:24.11111'), datetime(2007, 4, 5, 14, 30, 24, 111110))
26+
self.assertEqual(utils.from_iso8601('2007-04-05T14:30:24.111111'), datetime(2007, 4, 5, 14, 30, 24, 111111))
2127

2228
def test_json_pointer(self):
2329
""" json pointer io function """

pyswagger/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def dst(self, dt):
119119
_iso8601_fmt = re.compile(''.join([
120120
'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', # YYYY-MM-DD
121121
'T', # T
122-
'(?P<hour>\d{2}):(?P<minute>\d{2})(:(?P<second>\d{1,2}))?', # hh:mm:ss
122+
'(?P<hour>\d{2}):(?P<minute>\d{2})(:(?P<second>\d{1,2})(\.(?P<microsecond>\d{1,6}))?)?', # hh:mm:ss.ms
123123
'(?P<tz>Z|[+-]\d{2}:\d{2})?' # Z or +/-hh:mm
124124
]))
125125
_iso8601_fmt_date = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})') # YYYY-MM-DD
@@ -155,6 +155,10 @@ def _default_none(key):
155155
hour = _default_none('hour')
156156
minute = _default_none('minute')
157157
second = _default_none('second')
158+
microsecond = g.get('microsecond', None)
159+
if microsecond is not None:
160+
# append zero when not matching 6 digits
161+
microsecond = int(microsecond + '0' * (6 - len(microsecond)))
158162
tz_s = g.get('tz')
159163

160164
if not (year and month and day):
@@ -186,6 +190,7 @@ def _default_none(key):
186190
hour=hour or 0,
187191
minute=minute or 0,
188192
second=second or 0,
193+
microsecond=microsecond or 0,
189194
tzinfo=tz
190195
)
191196

0 commit comments

Comments
 (0)