Skip to content

Instantly share code, notes, and snippets.

@Snegovikufa
Created February 20, 2013 03:58
Show Gist options
  • Save Snegovikufa/4992757 to your computer and use it in GitHub Desktop.
Save Snegovikufa/4992757 to your computer and use it in GitHub Desktop.
How to operate with datetime and timezones
# -*- coding: utf-8 -*-
from pytz import timezone
from datetime import datetime, timedelta
tz1 = timezone("Asia/Yekaterinburg")
tz2 = timezone("America/New_York")
dst1 = datetime(2013, 3, 9, tzinfo=tz2)
dst2 = datetime(2013, 3, 10, tzinfo=tz2)
dst3 = datetime(2013, 3, 11, tzinfo=tz2)
dst4 = datetime(2013, 11, 2, tzinfo=tz2)
dst5 = datetime(2013, 11, 3, tzinfo=tz2)
dst6 = datetime(2013, 11, 4, tzinfo=tz2)
dst7 = datetime(2012, 3, 10, tzinfo=tz2)
dst8 = datetime(2012, 3, 11, tzinfo=tz2)
dst9 = datetime(2012, 3, 12, tzinfo=tz2)
dst10 = datetime(2012, 11, 3, tzinfo=tz2)
dst11 = datetime(2012, 11, 4, tzinfo=tz2)
dst12 = datetime(2012, 11, 5, tzinfo=tz2)
dsts = [dst1, dst2, dst3, dst4, dst5, dst6,
dst7, dst8, dst9, dst10, dst11, dst12]
for tz in [tz1, tz2]:
print '*' * 80
print tz
print
for dst in dsts:
for hour in range(0, 4):
updated = tz.normalize(dst + timedelta(hours=hour))
print updated.strftime("%d.%m.%Y-%H:%M %z %Z")
print
print '*' * 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment