Skip to content

Instantly share code, notes, and snippets.

@moskytw
Last active December 30, 2015 10:49
Show Gist options
  • Save moskytw/7818553 to your computer and use it in GitHub Desktop.
Save moskytw/7818553 to your computer and use it in GitHub Desktop.
It explains the relation between timestamp, timetuple and datetime.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
It explains the relation between timestamp, timetuple and datetime.
The principles:
1. If you use datetime without tzinfo (i.e., naive datetime), use mktime with .timetuple to get the timestamp.
2. If you use datetime with tzinfo (i.e., aware datetime), use timegm with .utctimetuple to get the timestamp.
3. Don't mix the naive datetime and aware dateime, except you always use local tzinfo for all aware datetimes (then you should follow the principle #1 or #2).
'''
from time import time, localtime, gmtime, mktime
from calendar import timegm
from datetime import datetime
timestamp = time()
local_timetuple = localtime(timestamp)
utc_timetuple = gmtime(timestamp)
# A datetime without tzinfo is a "naive" datetime.
naive_datetime = datetime.fromtimestamp(timestamp)
aware_datetime = None
try:
from dateutil.tz import tzlocal
except ImportError:
import sys
print >> sys.stderr, 'Warning: Need dateutil to test aware datetime.'
else:
aware_datetime = datetime.fromtimestamp(timestamp, tzlocal())
print '# From timestamp to timetuple'
print
print '## time() -> now timestamp'
print
print timestamp
print
print '## localtime(timestamp) -> local timetuple'
print
print local_timetuple
print
print '## gmtime(timestamp) -> UTC timetuple'
print
print utc_timetuple
print
print
print '# From timetuple to timestamp'
print
print '## mktime(local_timetuple) -> timestamp'
print
print mktime(local_timetuple)
print
print '## timegm(utc_timetuple) -> timestamp'
print
print timegm(utc_timetuple)
print
print
print '# From naive datetime to timestamp'
print
print 'A datetime without tzinfo is a "naive" datetime.'
print
print '## datetime.fromtimestamp(timestamp) -> naive datetime'
print
print naive_datetime
print
print '## .timetuple() -> local timetuple'
print
print naive_datetime.timetuple()
print
print '## mktime(.timetuple()) -> timestamp'
print
print mktime(naive_datetime.timetuple())
print
print '## .utctimetuple() -> still local timetuple, because it is a "naive" datetime.'
print
print naive_datetime.utctimetuple()
print
print '## timegm(.utctimetuple()) -> *wrong* timestamp, because .utctimetuple of a naive datetime returns a local timetuple.'
print
print timegm(naive_datetime.utctimetuple())
print
print
if aware_datetime:
print '# From aware datetime to timestamp'
print
print '## datetime.fromtimestamp(timestamp, tzlocal())'
print
print aware_datetime
print
print '## .timetuple() -> local timetuple'
print
print aware_datetime.timetuple()
print
print '## mktime(.timetuple()) -> timestamp'
print
print mktime(aware_datetime.timetuple())
print
print '## .utctimetuple() -> UTC timetuple'
print
print aware_datetime.utctimetuple()
print
print '## timegm(.utctimetuple()) -> timestamp'
print
print timegm(aware_datetime.utctimetuple())
print
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment