Last active
December 30, 2015 10:49
-
-
Save moskytw/7818553 to your computer and use it in GitHub Desktop.
It explains the relation between timestamp, timetuple and datetime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 '## time() -> now timestamp' | |
print timestamp | |
print '## localtime(timestamp) -> local timetuple' | |
print local_timetuple | |
print '## gmtime(timestamp) -> UTC timetuple' | |
print utc_timetuple | |
print '# From timetuple to timestamp' | |
print '## mktime(local_timetuple) -> timestamp' | |
print mktime(local_timetuple) | |
print '## timegm(utc_timetuple) -> timestamp' | |
print timegm(utc_timetuple) | |
print '# From naive datetime to timestamp' | |
print 'A datetime without tzinfo is a "naive" datetime.' | |
print '## datetime.fromtimestamp(timestamp) -> naive datetime' | |
print naive_datetime | |
print '## .timetuple() -> local timetuple' | |
print naive_datetime.timetuple() | |
print '## mktime(.timetuple()) -> timestamp' | |
print mktime(naive_datetime.timetuple()) | |
print '## .utctimetuple() -> still local timetuple, because it is a "naive" datetime.' | |
print naive_datetime.utctimetuple() | |
print '## timegm(.utctimetuple()) -> *wrong* timestamp, because .utctimetuple of a naive datetime returns a local timetuple.' | |
print timegm(naive_datetime.utctimetuple()) | |
if aware_datetime: | |
print '# From aware datetime to timestamp' | |
print '## datetime.fromtimestamp(timestamp, tzlocal())' | |
print aware_datetime | |
print '## .timetuple() -> local timetuple' | |
print aware_datetime.timetuple() | |
print '## mktime(.timetuple()) -> timestamp' | |
print mktime(aware_datetime.timetuple()) | |
print '## .utctimetuple() -> UTC timetuple' | |
print aware_datetime.utctimetuple() | |
print '## timegm(.utctimetuple()) -> timestamp' | |
print timegm(aware_datetime.utctimetuple()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment