time.strftime()

jsaul jsaul at gmx.de
Mon Mar 10 10:03:48 EST 2003


* Mikael Schönenberg [2003-03-10 12:51]:
> On Mon, 10 Mar 2003, jsaul wrote:
> > >>> time.strftime("%a, %d %b %Y %H:%M:%S %z", time.localtime())
> > 'Mon, 10 Mar 2003 10:26:51 +0000'
>
> It seems likely that what you've overlooked is this:
>
> "The use of %Z is now deprecated, but the %z escape that expands to the
>  preferred hour/minute offset is not supported by all ANSI C libraries."
>
> From: http://python.org/doc/current/lib/module-time.html#foot18650

OIC. In fact, "%z" appears to be an extension of the GNU-libc. But
it is also specified by IEEE Std 1003.1-2001 aka POSIX.1-2001.
Anyway, since I am on a GNU-libc system, I would expect either a
proper conversion or no conversion at all. So it appears to be
interpreted and it actually *is*, see below...

> Not what you were hoping for, I'm sure, but probably the cause of your
> problem. As to the solution... I can't think of any, apart from 'do it
> yourself' :-(

I did, but I found it frustrating that it appeared not to be
possible to do it all in one call to strftime(). Somewhat
"unpythonic".

*But* I was too hasty and while I relied on the apparently
somewhat incomplete information provided by help(time), I should
have read the more exhaustime html-docs. It turns out that I can
invoke time.strftime() without a second argument; the current
local time is used then. And this results in the correct output:

>>> time.strftime("%z")
'+0100'

Wow. On the other hand:

>>> time.strftime("%z", time.localtime())
'+0000'

Thus it actually could not have worked the way I tried it, because
the tuple returned by time.localtime() contains no information
about the time zone, therefore obviously no proper output from
"%z" was possible.

The way this is described in the html-docs is a bit misleading,
IMHO: "If tuple is not provided, the current time as returned by
localtime() is used." But both cases are *not* equivalent, as the
tuple lacks time zone information.

Cheers, jsaul
-- 
Palo pa'que aprenda que aquí sí hay honor!
[Rubén Blades]




More information about the Python-list mailing list