From 9d2edc0e17cc16eaea49dbea379b85ba4f1e610e Mon Sep 17 00:00:00 2001 From: Jay Weisskopf Date: Tue, 3 Jul 2018 15:08:32 -0400 Subject: Accept more than 6 fractional digits in `isoparse` RFC 3339 does not specify a limit to the number of fractional digits that can be provided. In the wild, languages like Go will provide up to 9 digits by default. Since the standard library's `datetime` only supports precision down to microseconds, any extra digits in the fractional component are truncated. Fixes #786 --- dateutil/parser/isoparser.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'dateutil/parser') diff --git a/dateutil/parser/isoparser.py b/dateutil/parser/isoparser.py index 7c755e5..172972b 100644 --- a/dateutil/parser/isoparser.py +++ b/dateutil/parser/isoparser.py @@ -88,7 +88,7 @@ class isoparser(object): - ``hh`` - ``hh:mm`` or ``hhmm`` - ``hh:mm:ss`` or ``hhmmss`` - - ``hh:mm:ss.sss`` or ``hh:mm:ss.ssssss`` (3-6 sub-second digits) + - ``hh:mm:ss.ssssss`` (Up to 6 sub-second digits) Midnight is a special case for `hh`, as the standard supports both 00:00 and 24:00 as a representation. The decimal separator can be @@ -199,10 +199,9 @@ class isoparser(object): return self._parse_tzstr(tzstr, zero_as_utc=zero_as_utc) # Constants - _MICROSECOND_END_REGEX = re.compile(b'[-+Z]+') _DATE_SEP = b'-' _TIME_SEP = b':' - _MICRO_SEPS = b'.,' + _FRACTION_REGEX = re.compile(b'[\\.,]([0-9]+)') def _parse_isodate(self, dt_str): try: @@ -357,16 +356,14 @@ class isoparser(object): pos += 1 if comp == 3: - # Microsecond - if timestr[pos:pos + 1] not in self._MICRO_SEPS: + # Fraction of a second + frac = self._FRACTION_REGEX.match(timestr[pos:]) + if not frac: continue - pos += 1 - us_str = self._MICROSECOND_END_REGEX.split(timestr[pos:pos + 6], - 1)[0] - + us_str = frac.group(1)[:6] # Truncate to microseconds components[comp] = int(us_str) * 10**(6 - len(us_str)) - pos += len(us_str) + pos += len(frac.group()) if pos < len_str: raise ValueError('Unused components in ISO string') -- cgit v1.2.3