strptime on Unix systems

M.-A. Lemburg mal at lemburg.com
Tue Dec 28 04:17:01 EST 1999


Malcolm Tredinnick wrote:
> 
> On a Linux technical support mailing list I am on, somebody recently posted a
> question about Python and although I was able to answer it, the logic behind
> the answer has me stumped:
> 
> The following does *not* work under Linux (at least):
> 
> import time
> format = '%a %b %d %H:%M:%S %Z %Y'
> t = time.localtime(time.time())
> timestring = time.strftime(format, tt)          # Works OK
> timetuple = time.strptime(tt, format)           # Throws ValueError
> 
> The reason for this problem is that strftime and strptime are based on their
> C-library counterparts and according the man pages, while strftime does take a
> %Z modifier in the format string, strptime does NOT understand this modifier.
> (so you can remove the %Z from format and the above snippet is fine.)
> 
> Two questions:
> (1) What is the story on other Unix systems? Is this a general problem (I hope
> not)?
> 
> (2) Is there any logic hidden behind the fact that one direction takes %Z and
> the other does not?

Sure: %Z would effectively have to parse *any* timezone string
or offset in strptime() while the same in strftime() only needs
to know the local timezone string.

For date/time parsing try mxDateTime (see my Python Pages). It
has quite a few parsers and automates much of the process -- note
that it currently doesn't parse literal timezone strings (for the
same reason mentioned above) only numeric ones. See the docs for
details.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                            Get ready to party !
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/






More information about the Python-list mailing list