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