2016-12-01 38 views
0

蔭試圖apscheduler根據印度標準時間UnknownTimeZoneError:「IST」:「IST」爲apscheduler

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

但每次它給了一個錯誤UnknownTimeZoneError添加作業。 調度程序正在接受EST,UTC等,但不接受IST。

IST是印度標準時間的正確時區嗎?如果不是,我如何根據IST安排工作?

回答

1

根據comments in source code你可以通過datetime.tzinfo對象調度:

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

根據以上考慮,你可以嘗試以下操作:

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) 

更新: Python 2中並沒有提供具體的tzinfo類的實現,但您可以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) 

輸出:

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

是時區模塊中2.7.12解密?因爲它給我的錯誤對象沒有模塊時區。我是否需要導入其他包? –

+0

@ketankhandagale更新了Python 2 – niemmi

+0

謝謝!! ..我用tz = pytz.timezone('Asia/Calcutta')..... :) –