problem with strptime and time zone

Chris Rebert clp2 at rebertia.com
Tue Aug 24 23:23:31 CEST 2010


On Tue, Aug 24, 2010 at 1:45 PM, m_ahlenius <ahleniusm at gmail.com> wrote:
> Hi,
>
> perhaps I missed this posted already somewhere.
>
> I am got a program which reads time stings from some devices which
> are  providing the time zones.  I have to take this into account when
> doing some epoch time calculations.
>
> When I run the following code with the time zone string set to 'GMT'
> it works ok.
>
> This works:
>
> myStrA = 'Sun Aug 22 19:03:06 GMT'
>
> gTimeA = strptime( myStrA, '%a %b %d %H:%M:%S %Z')
>
> print "gTimeA = ",gTimeA
>
> ---
<snip>
>
> whereas this fails:
> myStrA = 'Sun Aug 22 19:03:06 PDT'
> gTimeA = strptime( myStrA, '%a %b %d %H:%M:%S %Z')
> print "gTimeA = ",gTimeA
>
>
> ValueError: time data 'Sun Aug 22 19:03:06 PDT' does not match format
> '%a %b %d %H:%M:%S %Z'
>
> But when its set to 'PDT' it fails.
>
> Any ideas?

Read The Fine Manual.

Quoth http://docs.python.org/library/time.html#time.strptime :
"""
time.strptime(string[, format])
[...]
Support for the %Z directive is based on the values contained in
time.tzname and whether time.daylight is true. Because of this, it is
platform-specific except for recognizing UTC and GMT which are always
known (and are considered to be non-daylight savings timezones).
"""

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list