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

Discussion in 'Python' started by Artur Ergashev, Aug 15, 2011.

  1. 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).
    Artur Ergashev, Aug 15, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aGVuaw==?=

    Covert UTC String to UTC Datetime

    =?Utf-8?B?aGVuaw==?=, May 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    18,726
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  2. kongkolvyu
    Replies:
    4
    Views:
    486
    Old Wolf
    Jan 4, 2006
  3. Tuomas Vesterinen

    Problems with datetime.datetime.strptime

    Tuomas Vesterinen, May 9, 2009, in forum: Python
    Replies:
    0
    Views:
    864
    Tuomas Vesterinen
    May 9, 2009
  4. Jonathan Wood
    Replies:
    1
    Views:
    2,195
    Jonathan Wood
    Nov 1, 2009
  5. Justin Voelker
    Replies:
    2
    Views:
    263
    Dr J R Stockton
    Dec 2, 2008
Loading...

Share This Page