Fix for dateutil-related bugs
* Should hopefully fix timezone related issues for non-pytz libraries
This commit is contained in:
parent
c033a5c6f6
commit
0beeb95d76
3 changed files with 37 additions and 10 deletions
|
@ -435,16 +435,16 @@ class CronTab(object):
|
|||
"crontab: %r\n" \
|
||||
"now: %r", ' '.join(m.input for m in self.matchers), now)
|
||||
|
||||
if not delta:
|
||||
onow = now = datetime(1970, 1, 1)
|
||||
|
||||
delay = future - now
|
||||
if tz:
|
||||
delay += onow.utcoffset()
|
||||
delay -= tz.localize(future).utcoffset()
|
||||
|
||||
if not delta:
|
||||
begin = datetime(1970, 1, 1)
|
||||
delay = future - begin
|
||||
if tz:
|
||||
delay -= tz.localize(future).utcoffset()
|
||||
delay += _fix_none(onow.utcoffset())
|
||||
if hasattr(tz, 'localize'):
|
||||
delay -= _fix_none(tz.localize(future).utcoffset())
|
||||
else:
|
||||
delay -= _fix_none(future.replace(tzinfo=tz).utcoffset())
|
||||
|
||||
return delay.days * 86400 + delay.seconds + delay.microseconds / 1000000.
|
||||
|
||||
|
@ -458,3 +458,8 @@ class CronTab(object):
|
|||
if not self._test_match(index, entry):
|
||||
return False
|
||||
return True
|
||||
|
||||
def _fix_none(d, _=timedelta(0)):
|
||||
if d is None:
|
||||
return _
|
||||
return d
|
||||
|
|
2
setup.py
2
setup.py
|
@ -10,7 +10,7 @@ except:
|
|||
|
||||
setup(
|
||||
name='crontab',
|
||||
version='0.22.3',
|
||||
version='0.22.4',
|
||||
description='Parse and use crontab schedules in Python',
|
||||
author='Josiah Carlson',
|
||||
author_email='josiah.carlson@gmail.com',
|
||||
|
|
|
@ -4,6 +4,7 @@ import datetime
|
|||
import unittest
|
||||
|
||||
import pytz
|
||||
import dateutil.tz
|
||||
|
||||
from crontab import CronTab
|
||||
|
||||
|
@ -171,7 +172,7 @@ class TestCrontab(unittest.TestCase):
|
|||
self.assertTrue(-3600 <= previous <= 0, previous)
|
||||
ts += datetime.timedelta(seconds=1)
|
||||
|
||||
def test_timezones(self):
|
||||
def test_timezones_pytz(self):
|
||||
s = CronTab('0 9 13 3 * 2016')
|
||||
|
||||
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))), 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__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in a new issue