Skip to content

Instantly share code, notes, and snippets.

@sentenza
Forked from ju-popov/timestamp.py
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sentenza/9049239 to your computer and use it in GitHub Desktop.
Save sentenza/9049239 to your computer and use it in GitHub Desktop.
######################################################################
# 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
######################################################################
# 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)
@sentenza
Copy link
Author

Formatting datetime

The time.time() function returns the number of seconds since the epoch as seconds in UTC.

import datetime
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')

Via: http://stackoverflow.com/a/13891070/1977778

Formatting and timezone localization

localize() the date string as a UTC datetime, then use astimezone to convert it to the local timezone.

import pytz, datetime

timeUTC = datetime.datetime(2014, 2, 23, 18, 14, 50, 0)
timezoneLocal = pytz.timezone('Europe/Rome')
utc = pytz.utc
timeLocal = utc.localize(timeUTC).astimezone(timezoneLocal)
print(timeLocal)

localize does not convert datetimes, it interprets the date string as though it were written in that timezone. localize builds a timezone-aware datetime out of a naive datetime (such as timeUTC). astimezone converts timezone-aware datetimes to other timezones.

Original post @stackoverflow

datetime.tzinfo is an interface

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