problem with strptime and time zone

M

m_ahlenius

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


---

But when its set to 'PDT' it fails.

Any ideas?

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'

thank you,
 
A

Alex Willmer

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'

Support for the %Z directive is based on the values contained in
tzname and whether 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).

http://docs.python.org/library/time.html

Dateutil has it's own timezone database, so should work reliably
http://labix.org/python-dateutil
 
C

Chris Rebert

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

---
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
 
M

m_ahlenius

Support for the %Z directive is based on the values contained in
tzname and whether 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).

http://docs.python.org/library/time.html

Dateutil has it's own timezone database, so should work reliablyhttp://labix.org/python-dateutil


Thanks much, I missed the directive settings.
 
L

Lawrence D'Oliveiro

In message
Alex said:
Dateutil has it's own timezone database ...

I hate code which doesn’t just use /usr/share/zoneinfo. How many places do
you need to patch every time somebody changes their daylight-saving rules?
 
A

Alex Willmer

In message


I hate code which doesn’t just use /usr/share/zoneinfo. How many places do
you need to patch every time somebody changes their daylight-saving rules?

From reading http://labix.org/python-dateutil can read timezone
information from several platforms, including /usr/share/zoneinfo. I
don't know whether one chooses the source explicitly, or if it is
detected with fall back to the internal database.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top