Skip to content

Instantly share code, notes, and snippets.

@nzjrs nzjrs/timestamp.py forked from ju-popov/timestamp.py
Last active Dec 26, 2015

Embed
What would you like to do?
##
## http://emilics.com/blog/article/python_time.html
## http://www.saltycrane.com/blog/2008/11/python-datetime-time-conversions/
######################################################################
# CURRENT AWARE LOCAL DATETIME
######################################################################
from datetime import datetime
from tzlocal import get_localzone
local_tz = get_localzone()
local_dt = datetime.now(local_tz)
print local_dt
######################################################################
# CURRENT AWARE DATETIME IN UTC TIMEZONE
######################################################################
import pytz
from datetime import datetime
utc_tz = pytz.timezone('UTC')
utc_dt = datetime.now(utc_tz)
print utc_dt
######################################################################
# CURRENT AWARE DATETIME IN CHICAGO TIMEZONE
######################################################################
import pytz
from datetime import datetime
chicago_tz = pytz.timezone('America/Chicago')
chicago_dt = datetime.now(chicago_tz)
print chicago_dt
######################################################################
# CURRENT TIMESTAMP
######################################################################
from time import time
timestamp = int(time())
print timestamp
######################################################################
# TIMESTAMP TO LOCAL AWARE DATETIME
######################################################################
from datetime import datetime
from tzlocal import get_localzone
timestamp = 1367319130
local_tz = get_localzone()
local_dt = local_tz.localize(datetime.fromtimestamp(timestamp))
print local_dt
######################################################################
# TIMESTAMP TO UTC AWARE DATETIME
######################################################################
import pytz
from datetime import datetime
timestamp = 1367319130
utc_tz = pytz.timezone('UTC')
utc_dt = utc_tz.localize(datetime.utcfromtimestamp(timestamp))
print utc_dt
######################################################################
# TIMESTAMP TO CHICAGO AWARE DATETIME
######################################################################
import pytz
from datetime import datetime
timestamp = 1367319130
utc_tz = pytz.timezone('UTC')
utc_dt = utc_tz.localize(datetime.utcfromtimestamp(timestamp))
chicago_tz = pytz.timezone('America/Chicago')
chicago_dt = chicago_tz.normalize(utc_dt.astimezone(chicago_tz))
print chicago_dt
######################################################################
# AWARE DATETIME TO TIMESTAMP
######################################################################
import pytz
import calendar
from datetime import datetime
chicago_tz = pytz.timezone('America/Chicago')
chicago_dt = chicago_tz.localize(datetime(2013, 4, 30, 5, 52, 10))
timestamp = calendar.timegm(chicago_dt.utctimetuple())
print timestamp
######################################################################
# AWARE DATETIME TIMEZONE CONVERTION
######################################################################
import pytz
from datetime import datetime
chicago_tz = pytz.timezone('America/Chicago')
chicago_dt = chicago_tz.localize(datetime(2013, 4, 30, 5, 52, 10))
sofia_tz = pytz.timezone('Europe/Sofia')
sofia_dt = sofia_tz.normalize(chicago_dt.astimezone(sofia_tz))
print sofia_dt
######################################################################
# CONVERSIONS TO STRINGS
######################################################################
# datetime object to string
from datetime import datetime
import time
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
print date_str
# time tuple to string
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print date_str
######################################################################
# CONVERSIONS TO DATETIME OBJECTS
######################################################################
# time tuple to datetime object
from datetime import datetime
import time
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
dt_obj = datetime(*time_tuple[0:6])
print repr(dt_obj)
# date string to datetime object
date_str = "2008-11-10 17:53:59"
dt_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(dt_obj)
# timestamp to datetime object in local time
timestamp = 1226527167.595983
dt_obj = datetime.fromtimestamp(timestamp)
print repr(dt_obj)
# timestamp to datetime object in UTC
timestamp = 1226527167.595983
dt_obj = datetime.utcfromtimestamp(timestamp)
print repr(dt_obj)
######################################################################
# CONVERSIONS TO TIME TUPLES
######################################################################
# datetime object to time tuple
from datetime import datetime
import time
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
time_tuple = dt_obj.timetuple()
print repr(time_tuple)
# string to time tuple
date_str = "2008-11-10 17:53:59"
time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(time_tuple)
# timestamp to time tuple in UTC
timestamp = 1226527167.595983
time_tuple = time.gmtime(timestamp)
print repr(time_tuple)
# timestamp to time tuple in local time
timestamp = 1226527167.595983
time_tuple = time.localtime(timestamp)
print repr(time_tuple)
######################################################################
# CONVERSIONS TO TIMESTAMPS
######################################################################
# time tuple in local time to timestamp
from datetime import datetime
import time
time_tuple = (2008, 11, 12, 13, 59, 27, 2, 317, 0)
timestamp = time.mktime(time_tuple)
print repr(timestamp)
# time tuple in utc time to timestamp
time_tuple_utc = (2008, 11, 12, 13, 59, 27, 2, 317, 0)
timestamp_utc = calendar.timegm(time_tuple_utc)
print repr(timestamp_utc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.