Python strptime() and timezones?

后端 未结 5 1220
栀梦
栀梦 2020-11-22 11:58

I have a CSV dumpfile from a Blackberry IPD backup, created using IPDDump. The date/time strings in here look something like this (where EST is an Australian ti

5条回答
  •  梦谈多话
    2020-11-22 12:50

    I recommend using python-dateutil. Its parser has been able to parse every date format I've thrown at it so far.

    >>> from dateutil import parser
    >>> parser.parse("Tue Jun 22 07:46:22 EST 2010")
    datetime.datetime(2010, 6, 22, 7, 46, 22, tzinfo=tzlocal())
    >>> parser.parse("Fri, 11 Nov 2011 03:18:09 -0400")
    datetime.datetime(2011, 11, 11, 3, 18, 9, tzinfo=tzoffset(None, -14400))
    >>> parser.parse("Sun")
    datetime.datetime(2011, 12, 18, 0, 0)
    >>> parser.parse("10-11-08")
    datetime.datetime(2008, 10, 11, 0, 0)
    

    and so on. No dealing with strptime() format nonsense... just throw a date at it and it Does The Right Thing.

    Update: Oops. I missed in your original question that you mentioned that you used dateutil, sorry about that. But I hope this answer is still useful to other people who stumble across this question when they have date parsing questions and see the utility of that module.

提交回复
热议问题