1.1 --- a/imiptools/period.py Mon Sep 11 23:57:30 2017 +0200
1.2 +++ b/imiptools/period.py Tue Sep 12 18:33:53 2017 +0200
1.3 @@ -33,6 +33,12 @@
1.4 if x is None: return y
1.5 else: return x
1.6
1.7 +def start_point(obj):
1.8 + return Comparable(ifnone(obj.get_start_point(), StartOfTime()))
1.9 +
1.10 +def end_point(obj):
1.11 + return Comparable(ifnone(obj.get_end_point(), EndOfTime()))
1.12 +
1.13 class Comparable:
1.14
1.15 "A date/datetime wrapper that allows comparisons with other types."
1.16 @@ -158,23 +164,23 @@
1.17
1.18 if isinstance(other, PeriodBase):
1.19 return cmp(
1.20 - (Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(self.get_end_point(), EndOfTime()))),
1.21 - (Comparable(ifnone(other.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime())))
1.22 + (start_point(self), end_point(self)),
1.23 + (start_point(other), end_point(other))
1.24 )
1.25 else:
1.26 return 1
1.27
1.28 def overlaps(self, other):
1.29 - return Comparable(ifnone(self.get_end_point(), EndOfTime())) > Comparable(ifnone(other.get_start_point(), StartOfTime())) and \
1.30 - Comparable(ifnone(self.get_start_point(), StartOfTime())) < Comparable(ifnone(other.get_end_point(), EndOfTime()))
1.31 + return end_point(self) > start_point(other) and \
1.32 + start_point(self) < end_point(other)
1.33
1.34 def within(self, other):
1.35 - return Comparable(ifnone(self.get_start_point(), StartOfTime())) >= Comparable(ifnone(other.get_start_point(), StartOfTime())) and \
1.36 - Comparable(ifnone(self.get_end_point(), EndOfTime())) <= Comparable(ifnone(other.get_end_point(), EndOfTime()))
1.37 + return start_point(self) >= start_point(other) and \
1.38 + end_point(self) <= end_point(other)
1.39
1.40 def common(self, other):
1.41 - start = max(Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_start_point(), StartOfTime())))
1.42 - end = min(Comparable(ifnone(self.get_end_point(), EndOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime())))
1.43 + start = max(start_point(self), start_point(other))
1.44 + end = min(end_point(self), end_point(other))
1.45 if start <= end:
1.46 return self.make_corrected(start.dt, end.dt)
1.47 else: