question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a job with CronTrigger.from_crontab() does not default to scheduler timezone

See original GitHub issue

In some situations, the job time zone is not defaulted to the scheduler time zone, as is specified in the docs.

Expected Behavior

From the docs: https://apscheduler.readthedocs.io/en/latest/modules/triggers/cron.html?highlight=add_job

timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)

The time zone used for the scheduled job should be defaulted to the scheduler’s time zone.

Current Behavior

The time zone used for the scheduled job is the system time zone.

Steps to Reproduce

Here is a snippet of code that I used to reproduce the issue:

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from pytz import utc
import logging

def job_function():
    print('hello world!')

scheduler = BlockingScheduler(timezone=utc)
scheduler.add_job(job_function, CronTrigger.from_crontab('46 19 * * *'))
scheduler.print_jobs()
scheduler.start()

For reference, I am in the US/Eastern time zone. I would expect this job to default to using the time zone of the scheduler (UTC), and to be scheduled at 19:46 UTC every day. Instead, the job is running at 19:46 US/Eastern time every day.

Context (Environment)

I am working on a home automation system project, and I use apscheduler to schedule things like alarms, etc. It runs on the Raspbian linux distro, but I do most of my development on Fedora 29 or in alpine linux based docker containers. Python version is 3.7.1

Detailed Description

I took a quick look at the code, and the root cause seemed to be that the “default to scheduler time zone” logic was only applied when the trigger argument passed to .add_job() was the alias name of the trigger.

Relevant code from apscheduler/base.py:

    def _create_trigger(self, trigger, trigger_args):
        if isinstance(trigger, BaseTrigger):
            return trigger     #   <---- time zone logic below is not applied
        elif trigger is None:
            trigger = 'date'
        elif not isinstance(trigger, six.string_types):
            raise TypeError('Expected a trigger instance or string, got %s instead' %
                            trigger.__class__.__name__)

        # Use the scheduler's time zone if nothing else is specified
        trigger_args.setdefault('timezone', self.timezone)

        # Instantiate the trigger class
        return self._create_plugin_instance('trigger', trigger, trigger_args)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ivanmartinvallecommented, Sep 18, 2019

For anyone else running into this, the workaround looks to be the following:

CronTrigger.from_crontab(':cron_expression', 'UTC') # or whatever timezone you want
2reactions
bhushanbrbcommented, Feb 20, 2020

More precise for CRONTrigger --> CronTrigger.from_crontab(‘0 21 * * THU’, timezone=‘UTC’)

Read more comments on GitHub >

github_iconTop Results From Across the Web

apscheduler.triggers.cron — APScheduler 3.9.1 documentation
The cron trigger works with the so-called “wall clock” time. Thus, if the selected time zone observes DST (daylight saving time), you should...
Read more >
How to use the apscheduler.triggers.cron.CronTrigger function ...
To help you get started, we've selected a few apscheduler.triggers.cron.CronTrigger examples, based on popular ways it is used in public projects.
Read more >
create apscheduler job trigger from cron expression
I've created a function to map values to trigger. The below function will return a tuple in the order year, month, day, week,...
Read more >
Scheduled job builds are not triggering on time
At Job level a Timezone ID can be specified in the cron tab ( TZ=<Timezone ID> ). It overrides the Server time zone...
Read more >
CronTrigger (Quartz Enterprise Job Scheduler 1.8.2 API)
This method should not be invoked by client code. getTimeZone. public TimeZone getTimeZone(). Returns the time zone for which the cronExpression of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found