EventAggregator

Changeset

102:962e330c8c10
2010-03-29 Paul Boddie raw files shortlog changelog graph Introduced a default UTC conversion for Olson-based local times, choosing the time in the zone preceding any zone change in a regime. Added release notes.
EventAggregatorSupport.py (file) README.txt (file)
     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  
     2.1 --- a/README.txt	Mon Mar 22 23:56:39 2010 +0100
     2.2 +++ b/README.txt	Mon Mar 29 02:05:45 2010 +0200
     2.3 @@ -26,6 +26,13 @@
     2.4  Important Notices
     2.5  -----------------
     2.6  
     2.7 +In release 0.6, support for event times has been introduced. Due to the
     2.8 +complicated nature of times, time zones, time regimes, and so on, the
     2.9 +behaviour of the software may change in future versions to support common
    2.10 +use-cases in a more convenient fashion. Please be aware that implicitly chosen
    2.11 +or generated time or time zone information may change for events, particularly
    2.12 +those whose times are ambiguous or ill-defined.
    2.13 +
    2.14  In release 0.5, the "download this calendar" and "subscribe to this calendar"
    2.15  links have been fixed to return only events within the specified period and to
    2.16  work with day- and month-relative calendars. Users who have bookmarks in their
    2.17 @@ -213,7 +220,8 @@
    2.18      replace the link information provided by the RSS and iCalendar summaries.
    2.19    * Fixed the production of the summaries when pages with no available edit
    2.20      log information are to be included.
    2.21 -  * Added support for event times and time zone/regime information.
    2.22 +  * Added support for event times and time zone/regime information. This is
    2.23 +    subject to revision.
    2.24  
    2.25  New in EventAggregator 0.5 (Changes since EventAggregator 0.4)
    2.26  --------------------------------------------------------------