Skip to content

Commit

Permalink
Fix periods for naive datetime objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlov123 committed Jan 14, 2014
1 parent c0b7447 commit 83e6b30
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions schedule/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ class Period(object):
"""
def __init__(self, events, start, end, parent_persisted_occurrences=None,
occurrence_pool=None, tzinfo=pytz.utc):
self.utc_start = start.astimezone(pytz.utc)
self.utc_end = end.astimezone(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.events = events
self.tzinfo = self._get_tzinfo(tzinfo)
self.occurrence_pool = occurrence_pool
Expand Down Expand Up @@ -132,11 +140,15 @@ def get_periods(self, cls, tzinfo=None):

@property
def start(self):
return self.utc_start.astimezone(self.tzinfo)
if self.tzinfo is not None:
return self.utc_start.astimezone(self.tzinfo)
return self.utc_start.replace(tzinfo=None)

@property
def end(self):
return self.utc_end.astimezone(self.tzinfo)
if self.tzinfo is not None:
return self.utc_end.astimezone(self.tzinfo)
return self.utc_end.replace(tzinfo=None)


class Year(Period):
Expand Down Expand Up @@ -201,7 +213,7 @@ def get_days(self):
def get_day(self, daynumber):
date = self.start
if daynumber > 1:
date += datetime.timedelta(days=daynumber-1)
date += datetime.timedelta(days=daynumber - 1)
return self.create_sub_period(Day, date)

def next_month(self):
Expand Down

0 comments on commit 83e6b30

Please sign in to comment.