From: anthonybaxter Date: Mon, 3 Nov 2003 10:23:06 +0000 (+0000) Subject: Date arithmetic fixes. Date +/- Interval passes all tests again, after X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=99bb103baea63b2e7a27e3976a75fc1c91169cc0;p=roundup.git 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 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) git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1954 57a73879-2fb5-44c3-a270-3262357dd7e2 --- diff --git a/roundup/date.py b/roundup/date.py index 365aa80..69c9935 100644 --- a/roundup/date.py +++ b/roundup/date.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: date.py,v 1.54 2003-04-23 11:48:05 richard Exp $ +# $Id: date.py,v 1.55 2003-11-03 10:23:05 anthonybaxter Exp $ __doc__ = """ Date, time and time interval handling. @@ -204,22 +204,26 @@ class Date: if month > 12: year += 1; month -= 12 # now do the days, now that we know what month we're in - mdays = calendar.mdays - if month == 2 and calendar.isleap(year): month_days = 29 - else: month_days = mdays[month] - while month < 1 or month > 12 or day < 0 or day > month_days: + def get_mdays(year,month): + if month == 2 and calendar.isleap(year): return 29 + else: return calendar.mdays[month] + + while month < 1 or month > 12 or day < 0 or day > get_mdays(year,month): # now to day under/over - if day < 0: month -= 1; day += month_days - elif day > month_days: month += 1; day -= month_days + if day < 0: + # When going backwards, decrement month, then increment days + month -= 1 + day += get_mdays(year,month) + elif day > get_mdays(year,month): + # When going forwards, decrement days, then increment month + day -= get_mdays(year,month) + month += 1 # possibly fix up the month so we're within range while month < 1 or month > 12: - if month < 1: year -= 1; month += 12 + if month < 1: year -= 1; month += 12 ; day += 31 if month > 12: year += 1; month -= 12 - # re-figure the number of days for this month - if month == 2 and calendar.isleap(year): month_days = 29 - else: month_days = mdays[month] return (year, month, day, hour, minute, second, 0, 0, 0) def applyInterval(self, interval): diff --git a/test/test_dates.py b/test/test_dates.py index bf594e8..a43a7b1 100644 --- a/test/test_dates.py +++ b/test/test_dates.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: test_dates.py,v 1.26 2003-11-02 09:27:50 richard Exp $ +# $Id: test_dates.py,v 1.27 2003-11-03 10:23:06 anthonybaxter Exp $ import unittest, time @@ -205,7 +205,7 @@ class DateTestCase(unittest.TestCase): self.assertEqual(str(then), '2004-02-28.00:00:00') now = Date('2003-03-01.00:00:00') then = now - Interval('1d') - self.assertEqual(str(then), '2003-02-08.00:00:00') + self.assertEqual(str(then), '2003-02-28.00:00:00') now = Date('2003-03-01.00:00:00') then = now - Interval('59d') self.assertEqual(str(then), '2003-01-01.00:00:00') @@ -219,10 +219,10 @@ class DateTestCase(unittest.TestCase): then = now + Interval('2d') self.assertEqual(str(then), '2004-01-01.00:00:00') now = Date('2003-01-01.00:00:00') - then = now + Interval('364d') + then = now + Interval('365d') self.assertEqual(str(then), '2004-01-01.00:00:00') now = Date('2004-01-01.00:00:00') - then = now + Interval('365d') + then = now + Interval('366d') self.assertEqual(str(then), '2005-01-01.00:00:00') def testIntervalSubtractYearBoundary(self):