Skip to content

Commit

Permalink
Improve Periods timezone robustness.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlov123 committed Jan 14, 2014
1 parent 83e6b30 commit 280f8e7
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions schedule/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ class Period(object):
"""
def __init__(self, events, start, end, parent_persisted_occurrences=None,
occurrence_pool=None, tzinfo=pytz.utc):
if start.tzinfo is not None:
self.utc_start = start.astimezone(pytz.utc)
else:
self.utc_start = pytz.utc.localize(start)

if end.tzinfo is not None:
self.utc_end = end.astimezone(pytz.utc)
else:
self.utc_end = pytz.utc.localize(end)
self.utc_start = self._normalize_timezone_to_utc(start, tzinfo)

self.utc_end = self._normalize_timezone_to_utc(end, tzinfo)

self.events = events
self.tzinfo = self._get_tzinfo(tzinfo)
self.occurrence_pool = occurrence_pool
if parent_persisted_occurrences is not None:
self._persisted_occurrences = parent_persisted_occurrences

def _normalize_timezone_to_utc(self, point_in_time, tzinfo):
if point_in_time.tzinfo is not None:
return point_in_time.astimezone(pytz.utc)
if tzinfo is not None:
return tzinfo.localize(point_in_time).astimezone(pytz.utc)
return pytz.utc.localize(point_in_time)

def __eq__(self, period):
return self.utc_start == period.utc_start and self.utc_end == period.utc_end and self.events == period.events

Expand Down

0 comments on commit 280f8e7

Please sign in to comment.