# HG changeset patch # User Paul Boddie # Date 1427217533 -3600 # Node ID 259dd95cb0a062c5d5a3c31a4f0192963ab139ea # Parent cbf1c8cf9bdfb1ff7cc3ef350aeb17316810eb78# Parent 4c9b410b0e00c1dd2e71a57e24128c4b4afb2c47 Merged period-sorting changes, reverting period materialisation changes. diff -r cbf1c8cf9bdf -r 259dd95cb0a0 imiptools/data.py --- a/imiptools/data.py Tue Mar 24 17:56:12 2015 +0100 +++ b/imiptools/data.py Tue Mar 24 18:18:53 2015 +0100 @@ -426,18 +426,14 @@ origin_value = origin and ("RDATE",) or () for rdate in rdates: if isinstance(rdate, tuple): - start, end = rdate - start = to_datetime(start, tzid) - end = to_datetime(end, tzid) - periods.add((start, end) + origin_value) + periods.add(rdate + origin_value) else: - rdate = to_datetime(rdate, tzid) periods.add((rdate, rdate + duration) + origin_value) # Return a sorted list of the periods. periods = list(periods) - periods.sort() + periods.sort(cmp=compare_periods(tzid)) # Exclude exception dates. @@ -446,12 +442,8 @@ if exdates: for exdate in exdates: if isinstance(exdate, tuple): - start, end = exdate - start = to_datetime(start, tzid) - end = to_datetime(end, tzid) - period = start, end + period = exdate else: - exdate = to_datetime(exdate, tzid) period = (exdate, exdate + duration) i = bisect_left(periods, period) while i < len(periods) and periods[i][:2] == period: @@ -459,6 +451,17 @@ return periods +class compare_periods: + def __init__(self, tzid): + self.tzid = tzid + def __call__(self, first, second): + first_start, first_end = first[:2] + second_start, second_end = second[:2] + return cmp( + (to_datetime(first_start, self.tzid), to_datetime(first_end, self.tzid)), + (to_datetime(second_start, self.tzid), to_datetime(second_end, self.tzid)) + ) + def get_periods_for_freebusy(obj, periods, tzid): """ diff -r cbf1c8cf9bdf -r 259dd95cb0a0 imiptools/dates.py