Last active
April 7, 2021 16:09
-
-
Save ju-popov/5488075 to your computer and use it in GitHub Desktop.
Python DateTime / Timestamp Convertion
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
###################################################################### | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
datetime.now(pytz.utc)
is ok. Here's another option:.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 knowis_dst
parameter:You can drop
.normalize()
when converting from utc timezone (despite the example to the contrary from the pytz docs):timegm
-based solution might silently by mistake accept a naive datetime object. Here's an explicit way to get timestamp as a float: