Skip to content

Commit 7fb6fa8

Browse files
committed
Import fixes for lack of datetime.timezone in python 2.7 (simple UTC)
Signed-off-by: Steve Arnold <[email protected]>
1 parent 3db4136 commit 7fb6fa8

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

schedule/__init__.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@
3939
"""
4040
import collections
4141
import datetime
42-
from datetime import timezone
4342
import functools
4443
import logging
4544
import random
4645
import time
4746

47+
try:
48+
from datetime import timezone
49+
utc = timezone.utc
50+
except ImportError:
51+
from schedule.timezone import UTC
52+
utc = UTC()
53+
54+
4855
logger = logging.getLogger('schedule')
4956

5057

@@ -150,7 +157,7 @@ def idle_seconds(self):
150157
:return: Number of seconds until
151158
:meth:`next_run <Scheduler.next_run>`.
152159
"""
153-
return (self.next_run - datetime.datetime.now(timezone.utc)).total_seconds()
160+
return (self.next_run - datetime.datetime.now(utc)).total_seconds()
154161

155162

156163
class Job(object):
@@ -397,7 +404,7 @@ def should_run(self):
397404
"""
398405
:return: ``True`` if the job should be run now.
399406
"""
400-
return datetime.datetime.now(timezone.utc) >= self.next_run
407+
return datetime.datetime.now(utc) >= self.next_run
401408

402409
def run(self):
403410
"""
@@ -407,7 +414,7 @@ def run(self):
407414
"""
408415
logger.info('Running job %s', self)
409416
ret = self.job_func()
410-
self.last_run = datetime.datetime.now(timezone.utc)
417+
self.last_run = datetime.datetime.now(utc)
411418
self._schedule_next_run()
412419
return ret
413420

@@ -424,7 +431,7 @@ def _schedule_next_run(self):
424431
interval = self.interval
425432

426433
self.period = datetime.timedelta(**{self.unit: interval})
427-
self.next_run = datetime.datetime.now(timezone.utc) + self.period
434+
self.next_run = datetime.datetime.now(utc) + self.period
428435
if self.start_day is not None:
429436
assert self.unit == 'weeks'
430437
weekdays = (
@@ -455,15 +462,15 @@ def _schedule_next_run(self):
455462
# If we are running for the first time, make sure we run
456463
# at the specified time *today* (or *this hour*) as well
457464
if not self.last_run:
458-
now = datetime.datetime.now(timezone.utc)
465+
now = datetime.datetime.now(utc)
459466
if (self.unit == 'days' and self.at_time > now.time() and
460467
self.interval == 1):
461468
self.next_run = self.next_run - datetime.timedelta(days=1)
462469
elif self.unit == 'hours' and self.at_time.minute > now.minute:
463470
self.next_run = self.next_run - datetime.timedelta(hours=1)
464471
if self.start_day is not None and self.at_time is not None:
465472
# Let's see if we will still make that time we specified today
466-
if (self.next_run - datetime.datetime.now(timezone.utc)).days >= 7:
473+
if (self.next_run - datetime.datetime.now(utc)).days >= 7:
467474
self.next_run -= self.period
468475

469476

schedule/timezone.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import datetime
2+
3+
4+
class UTC(datetime.tzinfo):
5+
"""tzinfo derived concrete class named "UTC" with offset of 0"""
6+
# can be configured here
7+
_offset = datetime.timedelta(seconds=0)
8+
_dst = datetime.timedelta(0)
9+
_name = "UTC"
10+
11+
def utcoffset(self, dt):
12+
return self.__class__._offset
13+
14+
def dst(self, dt):
15+
return self.__class__._dst
16+
17+
def tzname(self, dt):
18+
return self.__class__._name

test_schedule.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
import schedule
1212
from schedule import every
1313

14+
try:
15+
from datetime import timezone
16+
utc = timezone.utc
17+
except ImportError:
18+
from schedule.timezone import UTC
19+
utc = UTC()
20+
1421

1522
def make_mock_job(name=None):
1623
job = mock.Mock()
@@ -260,7 +267,7 @@ def test_next_run_property(self):
260267
assert len(schedule.jobs) == 2
261268
# Make sure the hourly job is first
262269
assert schedule.next_run() == original_datetime(2010, 1, 6, 14, 16,
263-
tzinfo=datetime.timezone.utc)
270+
tzinfo=utc)
264271
assert schedule.idle_seconds() == 60 * 60
265272

266273
def test_cancel_job(self):

0 commit comments

Comments
 (0)