From: Ed Page Date: Mon, 17 May 2010 23:43:14 +0000 (-0500) Subject: The radio was in MST but the UI selected as if it was in local time. An X-Git-Url: http://git.maemo.org/git/?p=watersofshiloah;a=commitdiff_plain;h=ca348c0558e040574fb19e06f0a71a08afc41acc The radio was in MST but the UI selected as if it was in local time. An issue with the simple way of fixing this is Maemo doesn't report daylight is True, so had to do my own daylight detection for the radio program. --- diff --git a/src/mormonchannel_gtk.py b/src/mormonchannel_gtk.py index e9dc1c8..f2b7166 100755 --- a/src/mormonchannel_gtk.py +++ b/src/mormonchannel_gtk.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- """ -@todo Timezone issue with Mormon CHannel @todo Fix scrollto for Fremantle @todo backgrounds need some resizing @todo Icons on source selector need work diff --git a/src/util/time_utils.py b/src/util/time_utils.py new file mode 100644 index 0000000..90ec84d --- /dev/null +++ b/src/util/time_utils.py @@ -0,0 +1,94 @@ +from datetime import tzinfo, timedelta, datetime + +ZERO = timedelta(0) +HOUR = timedelta(hours=1) + + +def first_sunday_on_or_after(dt): + days_to_go = 6 - dt.weekday() + if days_to_go: + dt += timedelta(days_to_go) + return dt + + +# US DST Rules +# +# This is a simplified (i.e., wrong for a few cases) set of rules for US +# DST start and end times. For a complete and up-to-date set of DST rules +# and timezone definitions, visit the Olson Database (or try pytz): +# http://www.twinsun.com/tz/tz-link.htm +# http://sourceforge.net/projects/pytz/ (might not be up-to-date) +# +# In the US, since 2007, DST starts at 2am (standard time) on the second +# Sunday in March, which is the first Sunday on or after Mar 8. +DSTSTART_2007 = datetime(1, 3, 8, 2) +# and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov. +DSTEND_2007 = datetime(1, 11, 1, 1) +# From 1987 to 2006, DST used to start at 2am (standard time) on the first +# Sunday in April and to end at 2am (DST time; 1am standard time) on the last +# Sunday of October, which is the first Sunday on or after Oct 25. +DSTSTART_1987_2006 = datetime(1, 4, 1, 2) +DSTEND_1987_2006 = datetime(1, 10, 25, 1) +# From 1967 to 1986, DST used to start at 2am (standard time) on the last +# Sunday in April (the one on or after April 24) and to end at 2am (DST time; +# 1am standard time) on the last Sunday of October, which is the first Sunday +# on or after Oct 25. +DSTSTART_1967_1986 = datetime(1, 4, 24, 2) +DSTEND_1967_1986 = DSTEND_1987_2006 + + +class USTimeZone(tzinfo): + + def __init__(self, hours, reprname, stdname, dstname): + self.stdoffset = timedelta(hours=hours) + self.reprname = reprname + self.stdname = stdname + self.dstname = dstname + + def __repr__(self): + return self.reprname + + def tzname(self, dt): + if self.dst(dt): + return self.dstname + else: + return self.stdname + + def utcoffset(self, dt): + return self.stdoffset + self.dst(dt) + + def dst(self, dt): + if dt is None or dt.tzinfo is None: + # An exception may be sensible here, in one or both cases. + # It depends on how you want to treat them. The default + # fromutc() implementation (called by the default astimezone() + # implementation) passes a datetime with dt.tzinfo is self. + return ZERO + assert dt.tzinfo is self + + # Find start and end times for US DST. For years before 1967, return + # ZERO for no DST. + if 2006 < dt.year: + dststart, dstend = DSTSTART_2007, DSTEND_2007 + elif 1986 < dt.year < 2007: + dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006 + elif 1966 < dt.year < 1987: + dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986 + else: + return ZERO + + start = first_sunday_on_or_after(dststart.replace(year=dt.year)) + end = first_sunday_on_or_after(dstend.replace(year=dt.year)) + + # Can't compare naive to aware objects, so strip the timezone from + # dt first. + if start <= dt.replace(tzinfo=None) < end: + return HOUR + else: + return ZERO + + +Eastern = USTimeZone(-5, "Eastern", "EST", "EDT") +Central = USTimeZone(-6, "Central", "CST", "CDT") +Mountain = USTimeZone(-7, "Mountain", "MST", "MDT") +Pacific = USTimeZone(-8, "Pacific", "PST", "PDT") diff --git a/src/windows/radio.py b/src/windows/radio.py index 436f532..d75db1a 100644 --- a/src/windows/radio.py +++ b/src/windows/radio.py @@ -6,6 +6,7 @@ import gtk import hildonize import util.misc as misc_utils +import util.time_utils as time_utils import banners import presenter @@ -83,7 +84,7 @@ class RadioWindow(windows._base.BasicWindow): self._layout.pack_start(self._loadingBanner.toplevel, False, False) self._layout.pack_start(self._radioLayout, True, True) - self._dateShown = datetime.datetime.now() + self._dateShown = datetime.datetime.now(tz=time_utils.Mountain) self._update_title() def show(self):