1.1 --- a/imiptools/period.py Sun Oct 25 18:53:50 2015 +0100
1.2 +++ b/imiptools/period.py Sun Oct 25 21:06:02 2015 +0100
1.3 @@ -28,6 +28,10 @@
1.4 get_tzid, \
1.5 to_timezone, to_utc_datetime
1.6
1.7 +def ifnone(x, y):
1.8 + if x is None: return y
1.9 + else: return x
1.10 +
1.11 class Comparable:
1.12
1.13 "A date/datetime wrapper that allows comparisons with other types."
1.14 @@ -114,15 +118,15 @@
1.15
1.16 if isinstance(other, PeriodBase):
1.17 return cmp(
1.18 - (Comparable(self.get_start_point() or StartOfTime()), Comparable(self.get_end_point() or EndOfTime())),
1.19 - (Comparable(other.get_start_point() or StartOfTime()), Comparable(other.get_end_point() or EndOfTime()))
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 )
1.23 else:
1.24 return 1
1.25
1.26 def overlaps(self, other):
1.27 - return Comparable(self.get_end_point()) > Comparable(other.get_start_point()) and \
1.28 - Comparable(self.get_start_point()) < Comparable(other.get_end_point())
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
1.32 def get_key(self):
1.33 return self.get_start(), self.get_end()
1.34 @@ -170,8 +174,10 @@
1.35 dates/datetimes.
1.36 """
1.37
1.38 - self.start = isinstance(start, date) and start or isinstance(start, PointInTime) and start or get_datetime(start) or StartOfTime()
1.39 - self.end = isinstance(end, date) and end or isinstance(end, PointInTime) and end or get_datetime(end) or EndOfTime()
1.40 + if isinstance(start, (date, PointInTime)): self.start = start
1.41 + else: self.start = get_datetime(start) or StartOfTime()
1.42 + if isinstance(end, (date, PointInTime)): self.end = end
1.43 + else: self.end = get_datetime(end) or EndOfTime()
1.44 self.tzid = tzid
1.45 self.origin = origin
1.46
1.47 @@ -188,11 +194,13 @@
1.48
1.49 def get_start_point(self):
1.50 start = self.get_start()
1.51 - return isinstance(start, PointInTime) and start or to_utc_datetime(start, self.get_tzid())
1.52 + if isinstance(start, PointInTime): return start
1.53 + else: return to_utc_datetime(start, self.get_tzid())
1.54
1.55 def get_end_point(self):
1.56 end = self.get_end()
1.57 - return isinstance(end, PointInTime) and end or to_utc_datetime(end, self.get_tzid())
1.58 + if isinstance(end, PointInTime): return end
1.59 + else: return to_utc_datetime(end, self.get_tzid())
1.60
1.61 # Period and event recurrence logic.
1.62