Created
June 23, 2011 19:52
-
-
Save pkqk/1043465 to your computer and use it in GitHub Desktop.
Timezones how the fuck do they work
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
from datetime import datetime, tzinfo, timedelta | |
class TimeZone(tzinfo): | |
def __init__(self, name, offset): | |
self.name = name | |
self.offset = offset | |
self.timedelta = timedelta(hours=offset) | |
def utcoffset(self, dt): | |
return self.timedelta | |
def dst(self, dt): | |
return self.timedelta | |
def tzname(self, dt): | |
return self.name | |
bst = TimeZone('BST', 1) | |
gmt = TimeZone('UTC', 0) | |
datetime_in_bst = datetime.now(bst) | |
datetime_in_gmt = datetime.now(gmt) | |
now_without_tz = datetime.now() | |
print datetime_in_bst.strftime("%H:%M:%S %Z") | |
print datetime_in_gmt.strftime("%H:%M:%S %Z") | |
print datetime_in_gmt.astimezone(bst).strftime("%H:%M:%S %Z") | |
print now_without_tz.strftime("%H:%M:%S %Z") | |
try: | |
print now_without_tz.astimezone(bst).strftime("%H:%M:%S %Z") | |
except Exception, e: | |
print e.message | |
now_with_tz = now_without_tz.replace(tzinfo=gmt) | |
print now_with_tz.astimezone(bst).strftime("%H:%M:%S %Z") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment