Fix for dateutil-related bugs

* Should hopefully fix timezone related issues for non-pytz libraries
This commit is contained in:
Josiah Carlson 2018-11-20 13:49:12 -08:00
parent c033a5c6f6
commit 0beeb95d76
3 changed files with 37 additions and 10 deletions

View file

@ -435,16 +435,16 @@ class CronTab(object):
"crontab: %r\n" \ "crontab: %r\n" \
"now: %r", ' '.join(m.input for m in self.matchers), now) "now: %r", ' '.join(m.input for m in self.matchers), now)
if not delta:
onow = now = datetime(1970, 1, 1)
delay = future - now delay = future - now
if tz: if tz:
delay += onow.utcoffset() delay += _fix_none(onow.utcoffset())
delay -= tz.localize(future).utcoffset() if hasattr(tz, 'localize'):
delay -= _fix_none(tz.localize(future).utcoffset())
if not delta: else:
begin = datetime(1970, 1, 1) delay -= _fix_none(future.replace(tzinfo=tz).utcoffset())
delay = future - begin
if tz:
delay -= tz.localize(future).utcoffset()
return delay.days * 86400 + delay.seconds + delay.microseconds / 1000000. return delay.days * 86400 + delay.seconds + delay.microseconds / 1000000.
@ -458,3 +458,8 @@ class CronTab(object):
if not self._test_match(index, entry): if not self._test_match(index, entry):
return False return False
return True return True
def _fix_none(d, _=timedelta(0)):
if d is None:
return _
return d

View file

@ -10,7 +10,7 @@ except:
setup( setup(
name='crontab', name='crontab',
version='0.22.3', version='0.22.4',
description='Parse and use crontab schedules in Python', description='Parse and use crontab schedules in Python',
author='Josiah Carlson', author='Josiah Carlson',
author_email='josiah.carlson@gmail.com', author_email='josiah.carlson@gmail.com',

View file

@ -4,6 +4,7 @@ import datetime
import unittest import unittest
import pytz import pytz
import dateutil.tz
from crontab import CronTab from crontab import CronTab
@ -171,7 +172,7 @@ class TestCrontab(unittest.TestCase):
self.assertTrue(-3600 <= previous <= 0, previous) self.assertTrue(-3600 <= previous <= 0, previous)
ts += datetime.timedelta(seconds=1) ts += datetime.timedelta(seconds=1)
def test_timezones(self): def test_timezones_pytz(self):
s = CronTab('0 9 13 3 * 2016') s = CronTab('0 9 13 3 * 2016')
self.assertEqual(s.next(datetime.datetime(2016, 3, 13), default_utc=True), 32400) self.assertEqual(s.next(datetime.datetime(2016, 3, 13), default_utc=True), 32400)
@ -189,6 +190,27 @@ class TestCrontab(unittest.TestCase):
self.assertEqual(CronTab('30 1 * * * 2018').next(timezone.localize(datetime.datetime(2018, 11, 4, 1, 15))), 900) self.assertEqual(CronTab('30 1 * * * 2018').next(timezone.localize(datetime.datetime(2018, 11, 4, 1, 15))), 900)
self.assertEqual(CronTab('30 1 * * * 2018').next(timezone.localize(datetime.datetime(2018, 11, 4))), 9000) self.assertEqual(CronTab('30 1 * * * 2018').next(timezone.localize(datetime.datetime(2018, 11, 4))), 9000)
def test_timezones_dateutil(self):
s = CronTab('0 9 13 3 * 2016')
self.assertEqual(s.next(datetime.datetime(2016, 3, 13), default_utc=True), 32400)
utc = dateutil.tz.tzutc()
self.assertEqual(s.next(datetime.datetime(2016, 3, 13, tzinfo=utc), default_utc=True), 32400)
x = datetime.datetime(2016, 3, 13, tzinfo=dateutil.tz.gettz('US/Eastern'))
self.assertEqual(s.next(x), 28800)
t = CronTab('0 9 * * * 2018')
self.assertEqual(t.next(datetime.datetime(2018, 11, 4), default_utc=True), 32400)
timezone = dateutil.tz.gettz("America/Los_Angeles")
self.assertEqual(t.next(datetime.datetime(2018, 11, 4, tzinfo=timezone)), 36000)
before = datetime.datetime(2018, 11, 4, 8, 29, tzinfo=utc).astimezone(timezone)
self.assertEqual(CronTab('30 1 * * * 2018').next(before), 60)
before = datetime.datetime(2018, 11, 4, 8, 31, tzinfo=utc).astimezone(timezone)
self.assertEqual(CronTab('30 1 * * * 2018').next(before), 89940)
self.assertEqual(CronTab('30 1 * * * 2018').next(datetime.datetime(2018, 11, 4, 1, 15, tzinfo=timezone)), 900)
self.assertEqual(CronTab('30 1 * * * 2018').next(datetime.datetime(2018, 11, 4, tzinfo=timezone)), 5400)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()