[techtalk] python problem

Malcolm Tredinnick malcolmt at smart.net.au
Wed Dec 22 11:43:45 EST 1999


On Tue, Dec 21, 1999 at 05:05:59AM -0800, Neil ''Fred'' Picciotto wrote:
> > consider the following simple python script, test.py:
> > 
> > ------
> > #!/usr/bin/python
> > 
> > import time
> > 
> > format = '%a %b %d %H:%M:%S %Z %Y'
> > timetuple1 = time.localtime(time.time())
> > timestring = time.strftime( format , timetuple1 )
> > timetuple2 = time.strptime( timestring , format )
> > ------
> 
> well, i've now discovered that if i remove the time zone ('%Z') from the
> format string, it doesn't have this problem.  so the workaround for the
> actual script i'm writing is to remove the time zone from the string i
> want to parse, and just use the format string '%a %b %d %H:%M:%S %Y'.
> 
> so i guess it is a bug in the interpreter...?

The problem does indeed exist (which I was not willing to believe initially,
since it just seemed so silly), but it is not Python's fault! It is a problem
in the Linux C library (assuming the man pages can be trusted). The two
functions strftime() and strptime() are implemented in terms of the C
functions of the corresponding names. Looking at 'man strftime', we see that
it takes a %Z modifier, whereas 'man strptime' takes no such modifier!

So that explains the mystery -- albeit fairly unsatisfactorily. I've taken the
liberty of posting some information about this to the Python mailing list
(which is also the newsgroup comp.lang.python) to see what the story is on
other Unices.

The time module in Python is the source of *so* many questions -- particularly
for those trying to do cross-platform programming and handle different time
zones, etc. Unfortunately, this isn't one of the obvious questions (I hadn't
seen it before and I spend a lot of time reading Python newsgroups, etc).

Let me also recommend comp.lang.python and the Python mailing list to those
who are interested Python programming. It is one of the friendliest help-lists
I have been involved with  (in fact, after first lurking here, my initial
thoughts on this list were "it's just like the Python one"). No question too
small, but some of the real language heavies hang out there, too (including
Python author Guido van Rossum), so any really tricky questions are run into
the ground as well.  More information on this and other Pythonistic things at
www.python.org <end plug>.

Cheers,
Malcolm Tredinnick

--
Who here is telekinetic? Raise my hand ...


************
techtalk at linuxchix.org   http://www.linuxchix.org




More information about the Techtalk mailing list