Fixes issue with timezones, more tests

This commit is contained in:
Josiah Carlson 2018-11-08 14:25:43 -08:00
parent 68e47df736
commit c033a5c6f6
3 changed files with 17 additions and 4 deletions

View file

@ -3,6 +3,7 @@
crontab.py crontab.py
Written July 15, 2011 by Josiah Carlson Written July 15, 2011 by Josiah Carlson
Copyright 2011-2018 Josiah Carlson
Released under the GNU LGPL v2.1 and v3 Released under the GNU LGPL v2.1 and v3
available: available:
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
@ -433,16 +434,18 @@ class CronTab(object):
"author with the following information:\n" \ "author with the following information:\n" \
"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)
delay = future - now delay = future - now
if tz: if tz:
delay += tz.utcoffset(now) delay += onow.utcoffset()
delay -= tz.utcoffset(future) delay -= tz.localize(future).utcoffset()
if not delta: if not delta:
begin = datetime(1970, 1, 1) begin = datetime(1970, 1, 1)
delay = future - begin delay = future - begin
if tz: if tz:
delay -= tz.utcoffset(future) delay -= tz.localize(future).utcoffset()
return delay.days * 86400 + delay.seconds + delay.microseconds / 1000000. return delay.days * 86400 + delay.seconds + delay.microseconds / 1000000.
def previous(self, now=None, delta=True, default_utc=WARN_CHANGE): def previous(self, now=None, delta=True, default_utc=WARN_CHANGE):

View file

@ -10,7 +10,7 @@ except:
setup( setup(
name='crontab', name='crontab',
version='0.22.2', version='0.22.3',
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

@ -179,6 +179,16 @@ class TestCrontab(unittest.TestCase):
self.assertEqual(s.next(pytz.timezone('US/Eastern').localize(datetime.datetime(2016, 3, 13))), 28800) self.assertEqual(s.next(pytz.timezone('US/Eastern').localize(datetime.datetime(2016, 3, 13))), 28800)
t = CronTab('0 9 * * * 2018')
self.assertEqual(t.next(datetime.datetime(2018, 11, 4), default_utc=True), 32400)
timezone = pytz.timezone("America/Los_Angeles")
self.assertEqual(t.next(timezone.localize(datetime.datetime(2018, 11, 4))), 36000)
before = pytz.utc.localize(datetime.datetime(2018, 11, 4, 8, 29)).astimezone(timezone)
self.assertEqual(CronTab('30 1 * * * 2018').next(before), 3660)
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)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()