strptime on Unix systems
Justin Sheehy
dworkin at ccs.neu.edu
Wed Dec 22 16:46:27 EST 1999
"Malcolm Tredinnick" <malcolmt at smart.net.au> writes:
> 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())
^
(I assume that you meant `tt', and not `t'...
> 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.)
Really?
I would expect it to break regardless of the value of `format'. You
are passing a tuple as the first argument to time.strptime, which is
expecting a string.
However, there does seem to be a linux problem here.
Under FreeBSD and Solaris, at least, the time module works fine with
the above format string:
Python 1.5.2 (#3, Aug 24 1999, 15:55:20) [GCC 2.7.2.1] on freebsd3
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from time import *
>>> t = time()
>>> format = '%a %b %d %H:%M:%S %Z %Y'
>>> int(t) == (mktime(strptime(strftime(format, localtime(t)),
format)))
1
But under linux:
Python 1.5.2 (#1, Oct 22 1999, 16:54:21) [GCC 2.7.2.3] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from time import *
>>> t = time()
>>> format = '%a %b %d %H:%M:%S %Z %Y'
>>> int(t) == (mktime(strptime(strftime(format, localtime(t)),
format)))
Traceback (innermost last):
File "<stdin>", line 1, in ?
ValueError: format mismatch
Indeed, looking at the manual page for the linux system call
strptime(), it does not seem to accept %Z as a format specifier.
-Justin
More information about the Python-list
mailing list