# HG changeset patch # User Paul Boddie # Date 1269972771 -7200 # Node ID c05a82a6e3c1ea2047d48092a54f9f515728ce0c # Parent 962e330c8c1003e1fd96c08a686a951f4a13ab32 Simplified the minute and hour arithmetic when getting UTC times. Moved the 'until' method into the common base class. Changed the workaround for ambiguous or invalid times to use the zone from the time four hours earlier, just in case some zone changes involve wall-clock changes greater than, say, one hour. diff -r 962e330c8c10 -r c05a82a6e3c1 EventAggregatorSupport.py --- a/EventAggregatorSupport.py Mon Mar 29 02:05:45 2010 +0200 +++ b/EventAggregatorSupport.py Tue Mar 30 20:12:51 2010 +0200 @@ -808,6 +808,26 @@ length = min(len(data), len(other_data)) return cmp(self.data[:length], other.data[:length]) + def until(self, start, end, nextfn, prevfn): + + """ + Return a collection of units of time by starting from the given 'start' + and stepping across intervening units until 'end' is reached, using the + given 'nextfn' and 'prevfn' to step from one unit to the next. + """ + + current = start + units = [current] + if current < end: + while current < end: + current = nextfn(current) + units.append(current) + elif current > end: + while current > end: + current = prevfn(current) + units.append(current) + return units + class Month(Temporal): "A simple year-month representation." @@ -868,26 +888,6 @@ return Period([(x - y) for x, y in zip(self.data, start.data)]) - def until(self, start, end, nextfn, prevfn): - - """ - Return a collection of units of time by starting from the given 'start' - and stepping across intervening units until 'end' is reached, using the - given 'nextfn' and 'prevfn' to step from one unit to the next. - """ - - current = start - units = [current] - if current < end: - while current < end: - current = nextfn(current) - units.append(current) - elif current > end: - while current > end: - current = prevfn(current) - units.append(current) - return units - def months_until(self, end): "Return the collection of months from this month until 'end'." @@ -1033,16 +1033,14 @@ date = self.as_date() # Add the minutes and hours. - # NOTE: This makes various assumptions and probably would not work - # NOTE: for general arithmetic. minute += minutes - if minute < 0: - hour -= 1 - minute += 60 - elif minute > 59: - hour += 1 - minute -= 60 + if minute < 0 or minute > 59: + hour += minute / 60 + minute = minute % 60 + + # NOTE: This makes various assumptions and probably would not work + # NOTE: for general arithmetic. hour += hours if hour < 0: @@ -1159,7 +1157,12 @@ try: return self._as_olson_datetime() except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError): - return self._as_olson_datetime(-1) + + # Try again, using an earlier local time and then stepping forward + # in the chosen zone. + # NOTE: Four hours earlier seems reasonable. + + return self._as_olson_datetime(-4) def ambiguous(self):