mktime takes localtime, or we skew the clocks

The following snippet shows that to create a proper timestamp, we should
feed mktime() with localtime() data rather than gmtime(), as it is locale
aware.

>>> from time import mktime, gmtime, localtime
>>> bad    = gmtime(mktime(gmtime()))
>>> good   = gmtime(mktime(localtime()))
>>> judge  = gmtime()
>>> if bad == good: "You're using UTC. Demonstration is impossible."
... elif bad == judge: "I can be wrong"
... elif good == judge: "but this shows I'm right"
...
"but this shows I'm right"

The above code checks whether gmtime() == gmtime(timestamp) from
mktime(). Obviously we don't get the same 'now' if we feed the bad value
to mktime.
testing/mmn/xrange_replaces_range_in_python3
Mikael Nordfeldth 2013-07-26 01:02:46 +02:00
parent f80f216625
commit 9ff03b5be7
1 changed files with 4 additions and 4 deletions

View File

@ -23,7 +23,7 @@ Extra utilities for python-gnupg.
from __future__ import absolute_import from __future__ import absolute_import
from datetime import datetime from datetime import datetime
from socket import gethostname from socket import gethostname
from time import gmtime from time import localtime
from time import mktime from time import mktime
import codecs import codecs
@ -330,7 +330,7 @@ def _make_passphrase(length=None, save=False, file=None):
if save: if save:
ruid, euid, suid = os.getresuid() ruid, euid, suid = os.getresuid()
gid = os.getgid() gid = os.getgid()
now = mktime(gmtime()) now = mktime(localtime())
if not file: if not file:
filename = str('passphrase-%s-%s' % uid, now) filename = str('passphrase-%s-%s' % uid, now)
@ -387,8 +387,8 @@ def _threaded_copy_data(instream, outstream):
return copy_thread return copy_thread
def _utc_epoch(): def _utc_epoch():
"""Get the seconds since epoch for UTC.""" """Get the seconds since epoch."""
return int(mktime(gmtime())) return int(mktime(localtime()))
def _which(executable, flags=os.X_OK): def _which(executable, flags=os.X_OK):
"""Borrowed from Twisted's :mod:twisted.python.proutils . """Borrowed from Twisted's :mod:twisted.python.proutils .