Parse arbitary date formats in Python (including rfc3164)
Question: PHP has the amazing [strtotime()](http://php.net/strtotime) function which
takes pretty much anything and turns it into a time.

I'm looking for something similar in Python?

As an example of why: I'm parsing syslogs which have the dumbest format ever
(aka [rfc3164](http://www.ietf.org/rfc/rfc3164.txt)) which omits a year and
includes a space-padded day-of-month.

Currently in Python I'm doing this:

    
    
    import datetime
    d='Mar  5 09:10:11' # as an example
    
    # first remove the space, if it exists
    if d[4] == ' ':
       d = d[0:4] + d[5:]
    # append this year (I know, it could be last year around Dec/Jan!)
    d =  str(datetime.datetime.now().year) + ' ' + d
    
    # now we can feed it strptime
    date = datetime.strptime(d, "%Y %b %d %H:%M:%S")
    

This is really ugly.

Is there a better way?


Answer: I think you are looking for the [`dateutils`](http://labix.org/python-
dateutil#head-c0e81a473b647dfa787dc11e8c69557ec2c3ecd2) module:

    
    
    In [12]: d = 'Mar  5 09:10:11'
    
    In [13]: import dateutil
    
    In [14]: dateutil.parser.parse(d)
    Out[14]: datetime.datetime(2015, 3, 5, 9, 10, 11)
    

