From 83e6b3076ef2cec49477c5bac473e623ff7a6137 Mon Sep 17 00:00:00 2001 From: Paul Poulakis Date: Tue, 14 Jan 2014 13:44:51 +0200 Subject: [PATCH] Fix periods for naive datetime objects. --- schedule/periods.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/schedule/periods.py b/schedule/periods.py index 867ee4db..276aed5f 100644 --- a/schedule/periods.py +++ b/schedule/periods.py @@ -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 @@ -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): @@ -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):