1.1 --- a/EventAggregatorSupport.py Mon Mar 22 23:56:39 2010 +0100
1.2 +++ b/EventAggregatorSupport.py Mon Mar 29 02:05:45 2010 +0200
1.3 @@ -1033,6 +1033,8 @@
1.4 date = self.as_date()
1.5
1.6 # Add the minutes and hours.
1.7 + # NOTE: This makes various assumptions and probably would not work
1.8 + # NOTE: for general arithmetic.
1.9
1.10 minute += minutes
1.11 if minute < 0:
1.12 @@ -1083,6 +1085,17 @@
1.13 minutes = int(match.group("minutes") or 0) * sign
1.14 return hours, minutes
1.15
1.16 + # Attempt to handle Olson time zone identifiers.
1.17 +
1.18 + dt = self.as_olson_datetime()
1.19 + if dt:
1.20 + seconds = dt.utcoffset().seconds
1.21 + hours = seconds / 3600
1.22 + minutes = (seconds % 3600) / 60
1.23 + return hours, minutes
1.24 +
1.25 + # Otherwise return None.
1.26 +
1.27 return None
1.28
1.29 def olson_identifier(self):
1.30 @@ -1101,18 +1114,61 @@
1.31 else:
1.32 return None
1.33
1.34 + def _as_olson_datetime(self, hours=None):
1.35 +
1.36 + """
1.37 + Return a Python datetime object for this datetime interpreted using any
1.38 + Olson time zone identifier and the given 'hours' offset, raising one of
1.39 + the pytz exceptions in case of ambiguity.
1.40 + """
1.41 +
1.42 + olson = self.olson_identifier()
1.43 + if olson and pytz:
1.44 + tz = pytz.timezone(olson)
1.45 + data = self.padded().as_tuple()[:6]
1.46 + dt = datetime.datetime(*data)
1.47 +
1.48 + # With an hours offset, find a time probably in a previously
1.49 + # applicable time zone.
1.50 +
1.51 + if hours is not None:
1.52 + td = datetime.timedelta(0, hours * 3600)
1.53 + dt += td
1.54 +
1.55 + ldt = tz.localize(dt, None)
1.56 +
1.57 + # With an hours offset, adjust the time to define it within the
1.58 + # previously applicable time zone but at the presumably intended
1.59 + # position.
1.60 +
1.61 + if hours is not None:
1.62 + ldt -= td
1.63 +
1.64 + return ldt
1.65 + else:
1.66 + return None
1.67 +
1.68 + def as_olson_datetime(self):
1.69 +
1.70 + """
1.71 + Return a Python datetime object for this datetime interpreted using any
1.72 + Olson time zone identifier, choosing the time from the zone before the
1.73 + period of ambiguity.
1.74 + """
1.75 +
1.76 + try:
1.77 + return self._as_olson_datetime()
1.78 + except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError):
1.79 + return self._as_olson_datetime(-1)
1.80 +
1.81 def ambiguous(self):
1.82
1.83 "Return whether the time is local and ambiguous."
1.84
1.85 - olson = self.olson_identifier()
1.86 - if olson and pytz:
1.87 - try:
1.88 - tz = pytz.timezone(olson)
1.89 - data = self.padded().as_tuple()[:6]
1.90 - dt = tz.localize(datetime.datetime(*data), None)
1.91 - except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError):
1.92 - return 1
1.93 + try:
1.94 + self._as_olson_datetime()
1.95 + except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError):
1.96 + return 1
1.97
1.98 return 0
1.99