summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: eae7eff)
raw | patch | inline | side by side (parent: eae7eff)
author | anthonybaxter <anthonybaxter@57a73879-2fb5-44c3-a270-3262357dd7e2> | |
Tue, 4 Nov 2003 12:35:47 +0000 (12:35 +0000) | ||
committer | anthonybaxter <anthonybaxter@57a73879-2fb5-44c3-a270-3262357dd7e2> | |
Tue, 4 Nov 2003 12:35:47 +0000 (12:35 +0000) |
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
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 | patch | blob | history | |
roundup/date.py | patch | blob | history | |
test/test_dates.py | patch | blob | history |
diff --git a/CHANGES.txt b/CHANGES.txt
index 21c24da891e841f68fd88a0ff6fd8f2d6a2e6465..7ef8fda1e4e4dafe8e3dab1d23a5472df9668bd0 100644 (file)
--- a/CHANGES.txt
+++ b/CHANGES.txt
(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 69c9935bb3fbd9286104461fec2d672ef25351ec..d4f5f36d8acae0e51fd8d29120e530678631d579 100644 (file)
--- a/roundup/date.py
+++ b/roundup/date.py
# 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.
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
'''
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 c521c2858e769500dcc36981a0ac234d1686d462..d924ab310c8ccee1b01e730268324d9d0f06e35a 100644 (file)
--- a/test/test_dates.py
+++ b/test/test_dates.py
# 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
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
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'))