Skip to content

Commit

Permalink
Merge pull request #106 from ryanss/parse-python-logger
Browse files Browse the repository at this point in the history
Allow comma separator for fractional seconds in parser. (Fixes #28 and lp:974463)
  • Loading branch information
pganssle committed Aug 24, 2015
2 parents 8ebaa2c + bc69c3f commit ca8e98b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dateutil/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import string
import time
import collections
import re
from io import StringIO
from calendar import monthrange, isleap

Expand All @@ -59,6 +60,9 @@


class _timelex(object):
# Fractional seconds are sometimes split by a comma
_split_decimal = re.compile("([\.,])")

def __init__(self, instream):
if isinstance(instream, binary_type):
instream = instream.decode()
Expand Down Expand Up @@ -146,7 +150,7 @@ def get_token(self):
# numbers until we find something that doesn't fit.
if nextchar in numchars:
token += nextchar
elif nextchar == '.':
elif nextchar == '.' or (nextchar == ',' and len(token) >= 2):
token += nextchar
state = '0.'
else:
Expand Down Expand Up @@ -177,14 +181,16 @@ def get_token(self):
break # emit token

if (state in ('a.', '0.') and (seenletters or token.count('.') > 1 or
token[-1] == '.')):
l = token.split('.')
token[-1] in '.,')):
l = self._split_decimal.split(token)
token = l[0]
for tok in l[1:]:
self.tokenstack.append('.')
if tok:
self.tokenstack.append(tok)

if state == '0.' and token.count('.') == 0:
token = token.replace(',', '.')

return token

def __iter__(self):
Expand Down
4 changes: 4 additions & 0 deletions dateutil/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4875,6 +4875,10 @@ def testISOStrippedFormatStrip5(self):
self.assertEqual(parse("20030925"),
datetime(2003, 9, 25))

def testPythonLoggerFormat(self):
self.assertEqual(parse("2003-09-25 10:49:41,502"),
datetime(2003, 9, 25, 10, 49, 41, 502000))

def testNoSeparator1(self):
self.assertEqual(parse("199709020908"),
datetime(1997, 9, 2, 9, 8))
Expand Down

0 comments on commit ca8e98b

Please sign in to comment.