Skip to content

Instantly share code, notes, and snippets.

@ju-popov
Last active April 7, 2021 16:09
Show Gist options
  • Save ju-popov/5488075 to your computer and use it in GitHub Desktop.
Save ju-popov/5488075 to your computer and use it in GitHub Desktop.
Python DateTime / Timestamp Convertion
######################################################################
# 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
@zed
Copy link

zed commented Nov 17, 2013

datetime.now(pytz.utc) is ok. Here's another option:

# CURRENT AWARE DATETIME IN UTC TIMEZONE
utc_dt = datetime.utcnow().replace(tzinfo=pytz.utc) 

.fromtimestamp() might break for timestamps from the past (when local timezone might have had different utc offset). To avoid ambiguity during DST transitions, don't use .localize() unless you know is_dst parameter:

# TIMESTAMP TO LOCAL AWARE DATETIME
utc_dt = datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc) 
local_dt = utc_dt.astimezone(get_localzone())

You can drop .normalize() when converting from utc timezone (despite the example to the contrary from the pytz docs):

# TIMESTAMP TO CHICAGO AWARE DATETIME
chicago_dt = utc_dt.astimezone(chicago_tz)

timegm-based solution might silently by mistake accept a naive datetime object. Here's an explicit way to get timestamp as a float:

# AWARE DATETIME TO TIMESTAMP
timestamp = (chicago_dt - datetime(1970, 1,1, tzinfo=pytz.utc)).total_seconds()

@ju-popov
Copy link
Author

Thank you zed 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment