39
39
"""
40
40
import collections
41
41
import datetime
42
- from datetime import timezone
43
42
import functools
44
43
import logging
45
44
import random
46
45
import time
47
46
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
+
48
55
logger = logging .getLogger ('schedule' )
49
56
50
57
@@ -150,7 +157,7 @@ def idle_seconds(self):
150
157
:return: Number of seconds until
151
158
:meth:`next_run <Scheduler.next_run>`.
152
159
"""
153
- return (self .next_run - datetime .datetime .now (timezone . utc )).total_seconds ()
160
+ return (self .next_run - datetime .datetime .now (utc )).total_seconds ()
154
161
155
162
156
163
class Job (object ):
@@ -397,7 +404,7 @@ def should_run(self):
397
404
"""
398
405
:return: ``True`` if the job should be run now.
399
406
"""
400
- return datetime .datetime .now (timezone . utc ) >= self .next_run
407
+ return datetime .datetime .now (utc ) >= self .next_run
401
408
402
409
def run (self ):
403
410
"""
@@ -407,7 +414,7 @@ def run(self):
407
414
"""
408
415
logger .info ('Running job %s' , self )
409
416
ret = self .job_func ()
410
- self .last_run = datetime .datetime .now (timezone . utc )
417
+ self .last_run = datetime .datetime .now (utc )
411
418
self ._schedule_next_run ()
412
419
return ret
413
420
@@ -424,7 +431,7 @@ def _schedule_next_run(self):
424
431
interval = self .interval
425
432
426
433
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
428
435
if self .start_day is not None :
429
436
assert self .unit == 'weeks'
430
437
weekdays = (
@@ -455,15 +462,15 @@ def _schedule_next_run(self):
455
462
# If we are running for the first time, make sure we run
456
463
# at the specified time *today* (or *this hour*) as well
457
464
if not self .last_run :
458
- now = datetime .datetime .now (timezone . utc )
465
+ now = datetime .datetime .now (utc )
459
466
if (self .unit == 'days' and self .at_time > now .time () and
460
467
self .interval == 1 ):
461
468
self .next_run = self .next_run - datetime .timedelta (days = 1 )
462
469
elif self .unit == 'hours' and self .at_time .minute > now .minute :
463
470
self .next_run = self .next_run - datetime .timedelta (hours = 1 )
464
471
if self .start_day is not None and self .at_time is not None :
465
472
# 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 :
467
474
self .next_run -= self .period
468
475
469
476
0 commit comments