-
-
Save sentenza/9049239 to your computer and use it in GitHub Desktop.
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 |
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
###################################################################### | |
# LIST ALL EUROPE TIMEZONES | |
###################################################################### | |
from pytz import all_timezones | |
print "Number of timezones found: %d" % len(all_timezones) | |
print "List of all the Europe timezones:\n" | |
zones_counter = 0 | |
for zone in all_timezones: | |
if 'Europe' in zone: | |
zones_counter++ | |
print zone | |
print "Number of Europe timezones: %d" % zones_counter | |
###################################################################### | |
# TIMEZONES CONVERSION | |
###################################################################### | |
from datetime import datetime | |
from pytz import timezone | |
fmt = "%Y-%m-%d %H:%M:%S %Z%z" | |
# Current time in UTC | |
now_utc = datetime.now(timezone('UTC')) | |
print now_utc.strftime(fmt) | |
# Convert to Europe/Rome time zone | |
now_berlin = now_pacific.astimezone(timezone('Europe/Rome')) | |
print now_berlin.strftime(fmt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Formatting datetime
Via: http://stackoverflow.com/a/13891070/1977778
Formatting and timezone localization
Original post @stackoverflow
datetime.tzinfo is an interface