Search code examples
pythonpython-3.xpython-dateutil

Why does dateutil.parser.parse() get the year wrong on this string?


I'm working on parsing log files, which have variable datetime formats in their timestamps. I am passing this string: Feb 22 08:58:24 router1 to dateutil.parser.parse() to try and extract the timestamp, like so:

>>> dateutil.parser.parse('Feb 22 08:58:24 router1', fuzzy=True)
datetime.datetime(2001, 2, 22, 8, 58, 24)

Which results in the date: February 22nd, 2001

Why is the year parsed as 2001 rather than the current year, 2019?


Solution

  • The parser is ignoring router but not the 1 next to it. This can be checked by passing the fuzzy_with_tokens=True argument. The output results in a tuple with the first item as the datetime object representation of the timestamp and the second item as the ignored strings.

    from dateutil import parser
    print(parser.parse('Feb 22 08:58:24 router1', fuzzy_with_tokens=True))
    #Output:
    (datetime.datetime(2001, 2, 22, 8, 58, 24), (' ', ' router'))