# HG changeset patch # User Paul Boddie # Date 1427210095 -3600 # Node ID c057e9aac79c391f8a32b3b813914512d0a8d235 # Parent 21f7421c806afae71fa3195c4fd34f4917f54d14 Allow the sorting of periods to handle both dates and datetimes. diff -r 21f7421c806a -r c057e9aac79c imiptools/data.py --- a/imiptools/data.py Mon Mar 23 00:39:26 2015 +0100 +++ b/imiptools/data.py Tue Mar 24 16:14:55 2015 +0100 @@ -433,7 +433,7 @@ # Return a sorted list of the periods. periods = list(periods) - periods.sort() + periods.sort(cmp=compare_periods(tzid)) # Exclude exception dates. @@ -451,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): """