1.1 --- a/imiptools/data.py Sun Jun 04 18:28:18 2017 +0200
1.2 +++ b/imiptools/data.py Sun Jun 04 20:15:44 2017 +0200
1.3 @@ -259,12 +259,12 @@
1.4
1.5 return (dtstart, dtstart_attr), (dtend, dtend_attr)
1.6
1.7 - def get_periods(self, tzid, end=None, inclusive=False):
1.8 + def get_periods(self, tzid, start=None, end=None, inclusive=False):
1.9
1.10 """
1.11 Return periods defined by this object, employing the given 'tzid' where
1.12 no time zone information is defined, and limiting the collection to a
1.13 - window of time with the given 'end'.
1.14 + window of time with the given 'start' and 'end'.
1.15
1.16 If 'end' is omitted, only explicit recurrences and recurrences from
1.17 explicitly-terminated rules will be returned.
1.18 @@ -273,7 +273,7 @@
1.19 will be included.
1.20 """
1.21
1.22 - return get_periods(self, tzid, end, inclusive)
1.23 + return get_periods(self, tzid, start, end, inclusive)
1.24
1.25 def has_period(self, tzid, period):
1.26
1.27 @@ -282,7 +282,7 @@
1.28 zone information is defined, has the given 'period'.
1.29 """
1.30
1.31 - return period in self.get_periods(tzid, period.get_start_point(), inclusive=True)
1.32 + return period in self.get_periods(tzid, end=period.get_start_point(), inclusive=True)
1.33
1.34 def has_recurrence(self, tzid, recurrenceid):
1.35
1.36 @@ -292,23 +292,24 @@
1.37 """
1.38
1.39 start_point = self.get_recurrence_start_point(recurrenceid, tzid)
1.40 - for p in self.get_periods(tzid, start_point, inclusive=True):
1.41 + for p in self.get_periods(tzid, end=start_point, inclusive=True):
1.42 if p.get_start_point() == start_point:
1.43 return True
1.44 return False
1.45
1.46 - def get_active_periods(self, recurrenceids, tzid, end=None):
1.47 + def get_active_periods(self, recurrenceids, tzid, start=None, end=None):
1.48
1.49 """
1.50 Return all periods specified by this object that are not replaced by
1.51 those defined by 'recurrenceids', using 'tzid' as a fallback time zone
1.52 - to convert floating dates and datetimes, and using 'end' to indicate the
1.53 - end of the time window within which periods are considered.
1.54 + to convert floating dates and datetimes, and using 'start' and 'end' to
1.55 + respectively indicate the start and end of the time window within which
1.56 + periods are considered.
1.57 """
1.58
1.59 # Specific recurrences yield all specified periods.
1.60
1.61 - periods = self.get_periods(tzid, end)
1.62 + periods = self.get_periods(tzid, start, end)
1.63
1.64 if self.get_recurrenceid():
1.65 return periods
1.66 @@ -1035,12 +1036,13 @@
1.67
1.68 return delegators
1.69
1.70 -def get_periods(obj, tzid, end=None, inclusive=False):
1.71 +def get_periods(obj, tzid, start=None, end=None, inclusive=False):
1.72
1.73 """
1.74 Return periods for the given object 'obj', employing the given 'tzid' where
1.75 no time zone information is available (for whole day events, for example),
1.76 - confining materialised periods to before the given 'end' datetime.
1.77 + confining materialised periods to after the given 'start' datetime and
1.78 + before the given 'end' datetime.
1.79
1.80 If 'end' is omitted, only explicit recurrences and recurrences from
1.81 explicitly-terminated rules will be returned.
1.82 @@ -1083,11 +1085,32 @@
1.83 end = end and min(until_dt, end) or until_dt
1.84 inclusive = True
1.85
1.86 + # Define a selection period with a start point. The end will be handled
1.87 + # in the materialisation process below.
1.88 +
1.89 + selection_period = Period(start, None)
1.90 +
1.91 + # Obtain period instances, starting from the main period. Since counting
1.92 + # must start from the first period, filtering from a start date must be
1.93 + # done after the instances have been obtained.
1.94 +
1.95 for recurrence_start in selector.materialise(dtstart, end, parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive):
1.96 +
1.97 + # Determine the resolution of the period.
1.98 +
1.99 create = len(recurrence_start) == 3 and date or datetime
1.100 recurrence_start = to_timezone(create(*recurrence_start), obj_tzid)
1.101 recurrence_end = recurrence_start + main_period.get_duration()
1.102 - periods.append(RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr))
1.103 +
1.104 + # Create the period with accompanying metadata based on the main
1.105 + # period and event details.
1.106 +
1.107 + period = RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr)
1.108 +
1.109 + # Filter out periods before the start.
1.110 +
1.111 + if period.within(selection_period):
1.112 + periods.append(period)
1.113
1.114 else:
1.115 periods = []
1.116 @@ -1138,13 +1161,14 @@
1.117
1.118 return senders
1.119
1.120 -def get_window_end(tzid, days=100):
1.121 +def get_window_end(tzid, days=100, start=None):
1.122
1.123 """
1.124 Return a datetime in the time zone indicated by 'tzid' marking the end of a
1.125 - window of the given number of 'days'.
1.126 + window of the given number of 'days'. If 'start' is not indicated, the start
1.127 + of the window will be the current moment.
1.128 """
1.129
1.130 - return to_timezone(datetime.now(), tzid) + timedelta(days)
1.131 + return to_timezone(start or datetime.now(), tzid) + timedelta(days)
1.132
1.133 # vim: tabstop=4 expandtab shiftwidth=4