Fix UTC as default tzinfo for Jobs (#1696)

1. Made sure that default tzinfo in JobQueue is UTC #1693.
2. Added test that checks that all methods by default set job.tzinfo as UTC.
This commit is contained in:
Andrej730 2020-03-30 19:10:27 +03:00 committed by GitHub
parent e9cb6675ca
commit 9cb34af65a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -285,7 +285,7 @@ class JobQueue(object):
if job.enabled:
try:
current_week_day = datetime.datetime.now(job.tzinfo).date().weekday()
if any(day == current_week_day for day in job.days):
if current_week_day in job.days:
self.logger.debug('Running job %s', job.name)
job.run(self._dispatcher)
@ -400,7 +400,7 @@ class Job(object):
days=Days.EVERY_DAY,
name=None,
job_queue=None,
tzinfo=_UTC):
tzinfo=None):
self.callback = callback
self.context = context
@ -413,7 +413,7 @@ class Job(object):
self._days = None
self.days = days
self.tzinfo = tzinfo
self.tzinfo = tzinfo or _UTC
self._job_queue = weakref.proxy(job_queue) if job_queue is not None else None

View file

@ -28,7 +28,7 @@ from flaky import flaky
from telegram.ext import JobQueue, Updater, Job, CallbackContext
from telegram.utils.deprecate import TelegramDeprecationWarning
from telegram.utils.helpers import _UtcOffsetTimezone
from telegram.utils.helpers import _UtcOffsetTimezone, _UTC
@pytest.fixture(scope='function')
@ -330,3 +330,14 @@ class TestJobQueue(object):
sleep(0.03)
assert self.result == 0
def test_job_default_tzinfo(self, job_queue):
"""Test that default tzinfo is always set to UTC"""
job_1 = job_queue.run_once(self.job_run_once, 0.01)
job_2 = job_queue.run_repeating(self.job_run_once, 10)
job_3 = job_queue.run_daily(self.job_run_once, time=dtm.time(hour=15))
jobs = [job_1, job_2, job_3]
for job in jobs:
assert job.tzinfo == _UTC