ketan khandagale ketan khandagale - 1 year ago 155
Python Question

UnknownTimeZoneError: 'IST' for apscheduler

Iam trying adding a job in apscheduler according to the Indian standard time

scheduler.add_job('cron',start_date=date, hour=time[0], minute=time[1], id=str(job[0]),timezone='IST')

But every time its giving an error UnknownTimeZoneError: 'IST'.
The scheduler is accepting EST,UTC etc. but not IST.

Is IST correct time zone for Indian standard time ? If not how can i schedule a job according to IST ?

Answer Source

According to comments in source code you can pass datetime.tzinfo object to the scheduler:

:param str|datetime.tzinfo timezone: the default time zone (defaults to the local timezone)

With above in mind you could try following:

import datetime
tz = datetime.timezone(datetime.timedelta(hours=5, minutes=30))
scheduler.add_job('cron',start_date=date, hour=time[0], minute=time[1], id=str(job[0]),timezone=tz)

Update: Python 2 doesn't offer concrete implementations of tzinfo class but you can roll your own:

import datetime

class IST(datetime.tzinfo):
    def utcoffset(self, dt):
        return datetime.timedelta(hours=5, minutes=30)

    def tzname(self, dt):
        return "IST"

    def dst(self, dt):
        return datetime.timedelta()

tz = IST()


2016-12-01 14:33:37.031316+05:30
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download