datetime.strptime w/ non-UTC and non-local TZs?

Artur Ergashev artur at ergashev.com
Mon Aug 15 21:10:22 CEST 2011


I was hoping somebody give me some clarity on how datetime.strptime is
supposed to work, I'm thinking this is a bug, but wanted to see if the
community has any ideas before I submit a bug to the python tracker.
For reference I am in the CDT timezone...

>>> from datetime import datetime
>>> date_utc = "Sun Jul 24 02:54:11 UTC 2011"
>>> date_local = "Sun Jul 24 02:54:11 CDT 2011"
>>> date_other = "Sun Jul 24 02:54:11 PDT 2011"
>>> print datetime.strptime(date_utc, '%a %b %d %H:%M:%S %Z %Y')
2011-07-24 02:54:11
>>> print datetime.strptime(date_local, '%a %b %d %H:%M:%S %Z %Y')
2011-07-24 02:54:11
>>> print datetime.strptime(date_other, '%a %b %d %H:%M:%S %Z %Y')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'Sun Jul 24 02:54:11 PDT 2011' does not match
format '%a %b %d %H:%M:%S %Z %Y'

The format is correct, and it can parse UTC (as mentioned in the
documentation), and it can parse CDT (which is my current time zone),
but using another timezone causes it to fail. Trying to parse the
failing timezone on a computer set on that timezone works correctly.
The documentation seems to indicate that parsing non-UTC TZs isn't
guaranteed to work, but in that case the exception is non-clear
(perhaps there's no elegant solution here).



More information about the Python-list mailing list