Convert timestamps with offset to datetime obj using strptime

前端 未结 4 577

I am trying to convert time-stamps of the format \"2012-07-24T23:14:29-07:00\" to datetime objects in python using strptime method. The problem is with the time offset at t

4条回答
  •  长发绾君心
    2020-11-22 04:42

    ValueError: 'z' is a bad directive in format...

    (note: I have to stick to python 2.7 in my case)

    I have had a similar problem parsing commit dates from the output of git log --date=iso8601 which actually isn't the ISO8601 format (hence the addition of --date=iso8601-strict in a later version).

    Since I am using django I can leverage the utilities there.

    https://github.com/django/django/blob/master/django/utils/dateparse.py

    >>> from django.utils.dateparse import parse_datetime
    >>> parse_datetime('2013-07-23T15:10:59.342107+01:00')
    datetime.datetime(2013, 7, 23, 15, 10, 59, 342107, tzinfo=+0100)
    

    Instead of strptime you could use your own regular expression.

提交回复
热议问题