created timedelta to seconds function because python2.6-datetime doesn't support .total_seconds()

This commit is contained in:
Peter van 't Zand 2015-07-15 13:19:47 +02:00
parent ad29e04e3e
commit c0d478f14c
2 changed files with 11 additions and 2 deletions

View File

@ -155,7 +155,7 @@ class croniter(object):
if d.tzinfo is not None:
d = d.replace(tzinfo=None) - d.utcoffset()
return (d - datetime.datetime(1970, 1, 1)).total_seconds()
return self._timedelta_to_seconds(d - datetime.datetime(1970, 1, 1))
def _timestamp_to_datetime(self, timestamp):
"""
@ -167,6 +167,15 @@ class croniter(object):
return result
def _timedelta_to_seconds(self, td):
"""
Converts a 'datetime.timedelta' object `td` into seconds contained in
the duration
"""
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) \
/ 10**6
# iterator protocol, to enable direct use of croniter
# objects in a loop, like "for dt in croniter('5 0 * * *'): ..."
# or for combining multiple croniters into single

View File

@ -414,7 +414,7 @@ class CroniterTest(base.TestCase):
for expected_date, expected_offset in expected_schedule:
d = callback()
self.assertEqual(expected_date, d.replace(tzinfo=None))
self.assertEqual(expected_offset, d.utcoffset().total_seconds())
self.assertEqual(expected_offset, croniter._timedelta_to_seconds(d.utcoffset()))
def testTimezoneWinterTime(self):
tz = pytz.timezone('Europe/Athens')