Search code examples
pythondatetimestrptime

Python - strptime format with unknown behavior


I have a time format:

Sat Jan 17 04:33:06 +0000 2015

that I can't match to a strptime format. The closest I can find: basic datetime types is %c "Locale’s appropriate date and time representation." However, this doesn't exactly match.

I'm going for:

time_vec = [datetime.strptime(str(x),'%c') for x in data['time']]

any help would be appreciated.


Solution

  • >>> from dateutil import parser
    >>> s = "Sat Jan 17 04:33:06 +0000 2015"
    >>> dt = parser.parse(s)
    >>> dt
    datetime.datetime(2015, 1, 17, 4, 33, 6, tzinfo=tzutc())
    

    This is using the dateutil package to parse your string and return a datetime object.


    Side note: The bug you reference indicates that the problem is resolved in Python 3.2. My assumption is that you are not using Python 3, thus have the error.