Skip to content

Commit 967b9e0

Browse files
author
Anthony Baxter
committed
Date arithmetic fixes.
Date +/- Interval passes all tests again, after fixing a couple of the tests to actually reflect the calendar used on my planet rather than where-ever Richard was when he wrote the test <wink> The basic problem was that when going backwards, the code was adding the days of the current month, rather than the previous month. There's still a bug in the testDateSubtract that I'll fix next. Bugfix candidate (probably)
1 parent ccce6b2 commit 967b9e0

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

roundup/date.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: date.py,v 1.54 2003-04-23 11:48:05 richard Exp $
18+
# $Id: date.py,v 1.55 2003-11-03 10:23:05 anthonybaxter Exp $
1919

2020
__doc__ = """
2121
Date, time and time interval handling.
@@ -204,22 +204,26 @@ def addInterval(self, interval):
204204
if month > 12: year += 1; month -= 12
205205

206206
# now do the days, now that we know what month we're in
207-
mdays = calendar.mdays
208-
if month == 2 and calendar.isleap(year): month_days = 29
209-
else: month_days = mdays[month]
210-
while month < 1 or month > 12 or day < 0 or day > month_days:
207+
def get_mdays(year,month):
208+
if month == 2 and calendar.isleap(year): return 29
209+
else: return calendar.mdays[month]
210+
211+
while month < 1 or month > 12 or day < 0 or day > get_mdays(year,month):
211212
# now to day under/over
212-
if day < 0: month -= 1; day += month_days
213-
elif day > month_days: month += 1; day -= month_days
213+
if day < 0:
214+
# When going backwards, decrement month, then increment days
215+
month -= 1
216+
day += get_mdays(year,month)
217+
elif day > get_mdays(year,month):
218+
# When going forwards, decrement days, then increment month
219+
day -= get_mdays(year,month)
220+
month += 1
214221

215222
# possibly fix up the month so we're within range
216223
while month < 1 or month > 12:
217-
if month < 1: year -= 1; month += 12
224+
if month < 1: year -= 1; month += 12 ; day += 31
218225
if month > 12: year += 1; month -= 12
219226

220-
# re-figure the number of days for this month
221-
if month == 2 and calendar.isleap(year): month_days = 29
222-
else: month_days = mdays[month]
223227
return (year, month, day, hour, minute, second, 0, 0, 0)
224228

225229
def applyInterval(self, interval):

test/test_dates.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_dates.py,v 1.26 2003-11-02 09:27:50 richard Exp $
18+
# $Id: test_dates.py,v 1.27 2003-11-03 10:23:06 anthonybaxter Exp $
1919

2020
import unittest, time
2121

@@ -205,7 +205,7 @@ def testIntervalSubtractMonthBoundary(self):
205205
self.assertEqual(str(then), '2004-02-28.00:00:00')
206206
now = Date('2003-03-01.00:00:00')
207207
then = now - Interval('1d')
208-
self.assertEqual(str(then), '2003-02-08.00:00:00')
208+
self.assertEqual(str(then), '2003-02-28.00:00:00')
209209
now = Date('2003-03-01.00:00:00')
210210
then = now - Interval('59d')
211211
self.assertEqual(str(then), '2003-01-01.00:00:00')
@@ -219,10 +219,10 @@ def testIntervalAddYearBoundary(self):
219219
then = now + Interval('2d')
220220
self.assertEqual(str(then), '2004-01-01.00:00:00')
221221
now = Date('2003-01-01.00:00:00')
222-
then = now + Interval('364d')
222+
then = now + Interval('365d')
223223
self.assertEqual(str(then), '2004-01-01.00:00:00')
224224
now = Date('2004-01-01.00:00:00')
225-
then = now + Interval('365d')
225+
then = now + Interval('366d')
226226
self.assertEqual(str(then), '2005-01-01.00:00:00')
227227

228228
def testIntervalSubtractYearBoundary(self):

0 commit comments

Comments
 (0)