From be333fc6a75cbdab04553ffd4c3d752860302303 Mon Sep 17 00:00:00 2001 From: anthonybaxter Date: Tue, 4 Nov 2003 12:35:47 +0000 Subject: [PATCH] Date - Date works again. Note that it only produces Intervals with days, hours, minutes and seconds. Making it produce years and months is a lot more work. bugfix candidate git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1963 57a73879-2fb5-44c3-a270-3262357dd7e2 --- CHANGES.txt | 3 +++ roundup/date.py | 31 +++++++++++++++++-------------- test/test_dates.py | 16 +++++++++------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 21c24da..7ef8fda 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -24,6 +24,9 @@ Fixed: (bug #798659). - Remove empty lines before sending strings off to the csv parser (bug #821364). +- Date arithmetic was utterly broken, and has been for a long time. + Date +/- Interval now works, and Date - Date also works (produces + an Interval. Cleanup: - Replace curuserid attribute on Database with the extended getuid() method. diff --git a/roundup/date.py b/roundup/date.py index 69c9935..d4f5f36 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.55 2003-11-03 10:23:05 anthonybaxter Exp $ +# $Id: date.py,v 1.56 2003-11-04 12:35:47 anthonybaxter Exp $ __doc__ = """ Date, time and time interval handling. @@ -226,6 +226,9 @@ class Date: return (year, month, day, hour, minute, second, 0, 0, 0) + def differenceDate(self, other): + "Return the difference between this date and another date" + def applyInterval(self, interval): ''' Apply the interval to this date ''' @@ -250,29 +253,29 @@ class Date: assert isinstance(other, Date), 'May only subtract Dates or Intervals' - # TODO this code will fall over laughing if the dates cross - # leap years, phases of the moon, .... + return self.dateDelta(other) + + def dateDelta(self, other): + """ Produce an Interval of the difference between this date + and another date. Only returns days:hours:minutes:seconds. + """ + # Returning intervals larger than a day is almost + # impossible - months, years, weeks, are all so imprecise. a = calendar.timegm((self.year, self.month, self.day, self.hour, self.minute, self.second, 0, 0, 0)) b = calendar.timegm((other.year, other.month, other.day, other.hour, other.minute, other.second, 0, 0, 0)) diff = a - b - if diff < 0: + if diff > 0: sign = 1 - diff = -diff else: sign = -1 + diff = -diff S = diff%60 M = (diff/60)%60 - H = (diff/(60*60))%60 - if H>1: S = 0 - d = (diff/(24*60*60))%30 - if d>1: H = S = M = 0 - m = (diff/(30*24*60*60))%12 - if m>1: H = S = M = 0 - y = (diff/(365*24*60*60)) - if y>1: d = H = S = M = 0 - return Interval((y, m, d, H, M, S), sign=sign) + H = (diff/(60*60))%24 + d = diff/(24*60*60) + return Interval((0, 0, d, H, M, S), sign=sign) def __cmp__(self, other): """Compare this date to another date.""" diff --git a/test/test_dates.py b/test/test_dates.py index c521c28..d924ab3 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.28 2003-11-03 10:33:23 anthonybaxter Exp $ +# $Id: test_dates.py,v 1.29 2003-11-04 12:35:47 anthonybaxter Exp $ import unittest, time @@ -173,9 +173,9 @@ class DateTestCase(unittest.TestCase): now = Date('.') now.hour = now.minute = now.second = 0 then = now + Interval('2d') - ae(str(Interval(str(then))), '+ 2d') + ae((Interval(str(then))), Interval('- 2d')) then = now - Interval('2d') - ae(str(Interval(str(then))), '- 2d') + ae(Interval(str(then)), Interval('+ 2d')) def testIntervalAddMonthBoundary(self): # force the transition over a month boundary @@ -245,12 +245,14 @@ class DateTestCase(unittest.TestCase): self.assertEqual(i, Interval('-28d')) i = Date('2003-03-01.00:00:00') - Date('2003-02-01.00:00:00') self.assertEqual(i, Interval('28d')) - i = Date('2003-03-03.00:00:00') - Date('2002-02-01.00:00:00') + i = Date('2003-03-03.00:00:00') - Date('2003-02-01.00:00:00') self.assertEqual(i, Interval('30d')) - i = Date('2003-03-03.00:00:00') - Date('2002-04-01.00:00:00') + i = Date('2003-03-03.00:00:00') - Date('2002-02-01.00:00:00') + self.assertEqual(i, Interval('395d')) + i = Date('2003-03-03.00:00:00') - Date('2003-04-01.00:00:00') self.assertEqual(i, Interval('-29d')) - i = Date('2003-03-01.00:00:00') - Date('2002-02-01.00:00:00') - self.assertEqual(i, Interval('1m')) + i = Date('2003-03-01.00:00:00') - Date('2003-02-01.00:00:00') + self.assertEqual(i, Interval('28d')) # force the transition over a year boundary i = Date('2003-01-01.00:00:00') - Date('2002-01-01.00:00:00') self.assertEqual(i, Interval('365d')) -- 2.30.2