ketan khandagale ketan khandagale - 8 days ago 4
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

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()
print datetime.datetime.now(tz)

Output:

2016-12-01 14:33:37.031316+05:30