finally fix the DST handling properly using pytz

This commit is contained in:
steffen123 2010-07-29 16:18:05 +02:00
parent 5721b5b23c
commit 792a2aec4f

View File

@ -26,8 +26,11 @@ import codecs
from decimal import Decimal from decimal import Decimal
import operator import operator
from xml.dom.minidom import Node from xml.dom.minidom import Node
import time import time
import datetime import datetime
from pytz import timezone
import pytz
import logging import logging
# logging has been set up in fpdb.py or HUD_main.py, use their settings: # logging has been set up in fpdb.py or HUD_main.py, use their settings:
@ -497,23 +500,24 @@ or None if we fail to get the info """
@staticmethod @staticmethod
def changeTimezone(time, givenTimezone, wantedTimezone): def changeTimezone(time, givenTimezone, wantedTimezone):
offest = datetime.timedelta(hours=0) #print "raw time:",time, "given TZ:", givenTimezone
if givenTimezone=="ET" and wantedTimezone=="UTC": if wantedTimezone=="UTC":
# approximate rules for ET daylight savings time: wantedTimezone = pytz.utc
if ( time.month == 12 # all of Dec else:
or (time.month == 11 and time.day > 4) # and most of November raise Error #TODO raise appropriate error
or time.month < 3 # and all of Jan/Feb
or (time.month == 3 and time.day < 11) ): # and 1st 10 days of March
offset = datetime.timedelta(hours=5) # are EST: assume 5 hour offset (ET without daylight saving)
else:
offset = datetime.timedelta(hours=4) # rest is EDT: assume 4 hour offset (ET with daylight saving)
#print " tz = %s start = %s" % (tz, str(hand.starttime))
elif givenTimezone=="CET" and wantedTimezone=="UTC":
offset = datetime.timedelta(hours=1)
# adjust time into UTC: if givenTimezone=="ET":
time = time + offset givenTimezone = timezone('US/Eastern')
return time elif givenTimezone=="CET":
givenTimezone = timezone('Europe/Berlin')
#Note: Daylight Saving Time is standardised across the EU so this should be fine
else:
raise Error #TODO raise appropriate error
localisedTime = givenTimezone.localize(time)
utcTime = localisedTime.astimezone(wantedTimezone)
#print "utcTime:",utcTime
return utcTime
#end @staticmethod def changeTimezone #end @staticmethod def changeTimezone
@staticmethod @staticmethod