strftime: %Z, timezone, and all that

Carey Evans careye at spamcop.net
Thu Jun 28 06:33:35 EDT 2001


Les Schaffer <schaffer at optonline.net> writes:

> i tried adding a date header to an email as per the python docs, like so:
...
>     return strftime("%a, %d %b %Y %H:%M:%S %Z", localtime())
...
>     Date: Tue, 26 Jun 2001 23:40:46 Eastern Daylight Time
...
> seems like %Z in strftime should either return a +0500 or an EDT, no?

-0500, actually.

I think the Python documentation is incorrect in this case.  K&R only
describes %Z as "time zone name, if any", and the Single UNIX Spec is
just as vague:

    http://www.opengroup.org/onlinepubs/7908799/xsh/strftime.html

In any case, my time zone is one of "New Zealand Standard Time",
"Pacific/Auckland" or "NZST", none of which is listed in section 5.1
of RFC 822.  RFC 822 has been superseded by RFC 2822 now as well, and
the latter deprecates the named time zones.

Here's a function to return the correct numeric time zone for RFC 2822:

    def zone2822(timetuple):
        dst = timetuple[8]
        offs = (time.timezone, time.timezone, time.altzone)[1 + dst]
        return '%+.2d%.2d' % (offs / -3600, abs(offs / 60) % 60)

-- 
	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

	    "Quiet, you'll miss the humorous conclusion."



More information about the Python-list mailing list