[Datetime-SIG] Making tm_gmtoff and tm_zone available on all platforms

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Sep 29 03:21:51 CEST 2015

Most UNIX platforms extend struct tm to include tm_gmtoff and tm_zone
fields that contain the current UTC offset in seconds and the zone

Python has been making these fields available as attributes of
time.struct_time [1] since version 3.3, but only on platforms that support
them in the C library.

>>> import time
>>> t = time.localtime()
>>> t.tm_gmtoff
>>> t.tm_zone

I propose that we make these attributes available on all platforms by
computing their values when they are not available in struct tm.

The tm_gmtoff value is easy to compute by comparing localtime() to gmtime():

>>> u = time.gmtime(time.mktime(t))
>>> from calendar import timegm
>>> timegm(t) - timegm(u)

and tm_zone can be computed by calling strftime() with a '%Z' directive.

>>> time.strftime('%Z', t)

What does the group think?

[1]: https://docs.python.org/3/library/time.html#time.struct_time
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20150928/017c3d69/attachment.html>

More information about the Datetime-SIG mailing list