[Python] Enabling Daylight saving adjustment for Flask-APScheduler

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to run APScheduler for my Flask app, but I can't get daylight saving time working for the cron time.

    In my app.py, I have something like this:

    from flask import Flask
    import pytz
    from .scheduled_jobs import ScheduledJobs

    class SchedulerAppConfig:
    SCHEDULER_TIMEZONE = timezone=pytz.timezone('Europe/London')

    app = Flask(__name__)
    app.config.from_object(SchedulerAppConfig())

    ScheduledJobs.init(app)


    Then in scheduled_jobs.py, I have this:

    from flask_apscheduler import APScheduler

    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()

    @scheduler.task('cron', id='test_job_1', hour='09', minute='15')
    def test_job_1():
    logger.info("Test job 1 executed")


    I was expecting the task would be smart enough to have itself adjusted to the British summer time and start at 09:15, but the scheduler task seem to run at 10:15 rather than 09:15 (London time is UTC+1 in the summer and UTC+0 during winter).

    What have I have missed?

    Does the timezone in SchedulerAppConfig matter in my case? I don't see setting the timezone here does anything for me.

    I am running the Flask app on a generic Ubuntu inside a Docker container. Do I need to set anything about the date/time settings on Ubuntu?

    Continue reading...

Compartilhe esta Página