Examples of pytz in Python Django 02.11.2014

pytz allows accurate and cross platform timezone calculations using Python 2.4 or higher.

Install

pip install pytz

Create localized time

import pytz
import datetime

kiev_tz = pytz.timezone('Europe/Kiev')
kiev_tz.zone

dt = kiev_tz.localize(datetime.datetime(2014, 11, 2, 15, 25, 0))

Format time

fmt = '%Y-%m-%d %H:%M:%S %Z%z'
dt.strftime(fmt)

The preferred way of dealing with times is to always work in UTC, converting to localtime only when generating output to be read by humans.

utc_dt = datetime.datetime(2014, 11, 2, 15, 25, 0, tzinfo=pytz.utc)
dt = utc_dt.astimezone(kiev_tz)
dt.strftime(fmt)

Get now in locale time

now = datetime.datetime.utcnow().replace(tzinfo = pytz.utc)
now.astimezone(kiev_tz)

Also look at arrow.