taichino / croniter

croniter is a python module to provide iteration for datetime object.
http://github.com/taichino/croniter
387 stars 105 forks source link

respect local time zone when using datetime to get_next #117

Closed perklet closed 5 years ago

perklet commented 5 years ago
ipdb> datetime.fromtimestamp(croniter(config['cron']).get_next()).strftime("%Y-%m-%d %H:%M:%S")
'2019-01-10 08:00:00'

ipdb> croniter(config['cron']).get_next(datetime).strftime("%Y-%m-%d %H:%M:%S")
'2019-01-10 00:00:00'

ipdb> config['cron']
'0 0 10 * *'

ipdb> get_localzone()
<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>

As you can see, my local timezone is Asia/Shanghai, the result timestamp is correct(assuming the cron expression is evaluated in UTC time), but when I use datetime as a parameter, the result does not consider timezone.

Or maybe, should the cron expression be evaluated in local timezone? maybe we should add a timezone parameter to the __init__ method?

kiorky commented 5 years ago

For your code being timezone sensible, you need to make it explicitly hande it by setting it.

Please see https://github.com/taichino/croniter/blob/master/src/croniter/tests/test_croniter.py#L635 for an exemple

kiorky commented 5 years ago

You can make pytz returns you your actual timezone by using:

import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal

# get local timezone    
local_tz = get_localzone()