How to pass UTC timezone for APScheduler 3.0?
Asked Answered
U

3

9

Any Heroku folks on here? It seems their system will not execute things from APScheduler labeled as cron. FYI: I'm using the free package. Using this example, the interval will run, the cron will not. Has anyone else run into this?

EDIT: Its been suggested that I specify UTC I am unsure how to do that using add_job. Any takers? Because I know this isn't currently right:

from apscheduler.schedulers.blocking import BlockingScheduler
from pytz import utc

sched = BlockingScheduler(timezone=utc)

def grabit():
    print "This job is run every weekday"

def tick():
    print "every 5 minutes"

sched.add_job(grabit, 'cron', day_of_week='mon-fri', hour=0, minute=13, id="get_things", replace_existing=True)
sched.add_job(tick, 'interval', minutes=5)
sched.start()
Unerring answered 10/9, 2015 at 0:36 Comment(0)
B
19

You can pass the string like this:

sched = BlockingScheduler(timezone="Asia/Kolkata")

And find the string using thisL

from tzlocal import get_localzone
tz = get_localzone()
print(tz)

The object will contain the string

Burgonet answered 21/6, 2016 at 19:27 Comment(0)
T
0

Update to dark Knight's solution:

from tzlocal import get_localzone
tz = get_localzone()
print(tz)

Output:

Asia/Kolkata
Tokay answered 3/7, 2020 at 22:24 Comment(2)
Hi Manoj, welcome to stackoverflow. It's not clear to me how this answers the question being asked. Could you please elaborate?Deena
Thank you demented hedgehog for the warm welcome. I came across this and thought of adding an update, I found to the dark Knight's solution.Tokay
M
0

The question is asking about UTC timezone:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler(timezone=datetime.UTC)

def tick():
    print "every 5 minutes"

sched.add_job(tick, 'interval', minutes=5)
sched.start()

Use datatime's UTC is better because this is a builtin package. If you check args required by BaseScheduler, it accepts str|datetime.tzinfo.

Monte answered 1/9, 2024 at 19:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.