1.1 --- a/imiptools/period.py Sun Oct 26 19:42:40 2014 +0100
1.2 +++ b/imiptools/period.py Sun Oct 26 19:43:41 2014 +0100
1.3 @@ -4,6 +4,27 @@
1.4
1.5 # Time management.
1.6
1.7 +def have_conflict(freebusy, periods, get_conflicts=False):
1.8 +
1.9 + """
1.10 + Return whether any period in 'freebusy' overlaps with the given 'periods',
1.11 + returning a collection of such overlapping periods if 'get_conflicts' is
1.12 + set to a true value.
1.13 + """
1.14 +
1.15 + conflicts = []
1.16 + for start, end in periods:
1.17 + period = period_overlaps(freebusy, (start, end))
1.18 + if period:
1.19 + if get_conflicts:
1.20 + conflicts.append(period)
1.21 + else:
1.22 + return True
1.23 + if get_conflicts:
1.24 + return conflicts
1.25 + else:
1.26 + return False
1.27 +
1.28 def insert_period(freebusy, period):
1.29 insort_left(freebusy, period)
1.30
1.31 @@ -17,12 +38,20 @@
1.32 i += 1
1.33
1.34 def period_overlaps(freebusy, period):
1.35 +
1.36 + """
1.37 + Return from 'freebusy' any period overlapping with the given 'period', or
1.38 + None if no overlap occurs.
1.39 + """
1.40 +
1.41 dtstart, dtend = period[:2]
1.42 i = bisect_left(freebusy, (dtstart, dtend, None))
1.43 return (
1.44 - i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend)
1.45 + i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) and freebusy[i]
1.46 or
1.47 - i > 0 and freebusy[i - 1][1] > dtstart
1.48 + i > 0 and freebusy[i - 1][1] > dtstart and freebusy[i - 1]
1.49 + or
1.50 + None
1.51 )
1.52
1.53 # vim: tabstop=4 expandtab shiftwidth=4