[Python-ideas] Happy leap second

Alexander Belopolsky alexander.belopolsky at gmail.com
Sat Jun 30 16:23:45 CEST 2012

Even though many have hoped that the authorities would stop fiddling
with our clocks, today a leap second will be inserted in UTC.
Systems using Olson/IANA timezone database have a way to deal with
this without adjusting their clocks, but few systems are configured
that way:

$ TZ=right/UTC date -d @1341100824
Sat Jun 30 23:59:60 UTC 2012

(1341100824 is the number of seconds since epoch including the leap seconds.)

Python's time module works fine with the "right" timezones:

>>> import time
>>> print(time.strftime('%T', time.localtime(1341100824)))

but the datetime module clips the leap second down to the previous second:

>>> from datetime import datetime
>>> from datetime import datetime
>>> print(datetime.fromtimestamp(1341100824).strftime('%T'))
>>> print datetime.fromtimestamp(1341100823).strftime('%T')

BDFL has been resisting adding support for leap seconds to the
datetime module [1], but as the clocks become more accurate and
synchronization requirements become stricter, we may want to revisit
this issue.

[1] http://mail.python.org/pipermail/python-ideas/2010-June/007307.html

More information about the Python-ideas mailing list