Skip to content

Commit cf9a094

Browse files
author
Anthony Baxter
committed
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
1 parent 3065ba3 commit cf9a094

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Fixed:
2424
(bug #798659).
2525
- Remove empty lines before sending strings off to the csv parser
2626
(bug #821364).
27+
- Date arithmetic was utterly broken, and has been for a long time.
28+
Date +/- Interval now works, and Date - Date also works (produces
29+
an Interval.
2730

2831
Cleanup:
2932
- Replace curuserid attribute on Database with the extended getuid() method.

roundup/date.py

Lines changed: 17 additions & 14 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.55 2003-11-03 10:23:05 anthonybaxter Exp $
18+
# $Id: date.py,v 1.56 2003-11-04 12:35:47 anthonybaxter Exp $
1919

2020
__doc__ = """
2121
Date, time and time interval handling.
@@ -226,6 +226,9 @@ def get_mdays(year,month):
226226

227227
return (year, month, day, hour, minute, second, 0, 0, 0)
228228

229+
def differenceDate(self, other):
230+
"Return the difference between this date and another date"
231+
229232
def applyInterval(self, interval):
230233
''' Apply the interval to this date
231234
'''
@@ -250,29 +253,29 @@ def __sub__(self, other):
250253

251254
assert isinstance(other, Date), 'May only subtract Dates or Intervals'
252255

253-
# TODO this code will fall over laughing if the dates cross
254-
# leap years, phases of the moon, ....
256+
return self.dateDelta(other)
257+
258+
def dateDelta(self, other):
259+
""" Produce an Interval of the difference between this date
260+
and another date. Only returns days:hours:minutes:seconds.
261+
"""
262+
# Returning intervals larger than a day is almost
263+
# impossible - months, years, weeks, are all so imprecise.
255264
a = calendar.timegm((self.year, self.month, self.day, self.hour,
256265
self.minute, self.second, 0, 0, 0))
257266
b = calendar.timegm((other.year, other.month, other.day,
258267
other.hour, other.minute, other.second, 0, 0, 0))
259268
diff = a - b
260-
if diff < 0:
269+
if diff > 0:
261270
sign = 1
262-
diff = -diff
263271
else:
264272
sign = -1
273+
diff = -diff
265274
S = diff%60
266275
M = (diff/60)%60
267-
H = (diff/(60*60))%60
268-
if H>1: S = 0
269-
d = (diff/(24*60*60))%30
270-
if d>1: H = S = M = 0
271-
m = (diff/(30*24*60*60))%12
272-
if m>1: H = S = M = 0
273-
y = (diff/(365*24*60*60))
274-
if y>1: d = H = S = M = 0
275-
return Interval((y, m, d, H, M, S), sign=sign)
276+
H = (diff/(60*60))%24
277+
d = diff/(24*60*60)
278+
return Interval((0, 0, d, H, M, S), sign=sign)
276279

277280
def __cmp__(self, other):
278281
"""Compare this date to another date."""

test/test_dates.py

Lines changed: 9 additions & 7 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.28 2003-11-03 10:33:23 anthonybaxter Exp $
18+
# $Id: test_dates.py,v 1.29 2003-11-04 12:35:47 anthonybaxter Exp $
1919

2020
import unittest, time
2121

@@ -173,9 +173,9 @@ def testIntervalInitDate(self):
173173
now = Date('.')
174174
now.hour = now.minute = now.second = 0
175175
then = now + Interval('2d')
176-
ae(str(Interval(str(then))), '+ 2d')
176+
ae((Interval(str(then))), Interval('- 2d'))
177177
then = now - Interval('2d')
178-
ae(str(Interval(str(then))), '- 2d')
178+
ae(Interval(str(then)), Interval('+ 2d'))
179179

180180
def testIntervalAddMonthBoundary(self):
181181
# force the transition over a month boundary
@@ -245,12 +245,14 @@ def testDateSubtract(self):
245245
self.assertEqual(i, Interval('-28d'))
246246
i = Date('2003-03-01.00:00:00') - Date('2003-02-01.00:00:00')
247247
self.assertEqual(i, Interval('28d'))
248-
i = Date('2003-03-03.00:00:00') - Date('2002-02-01.00:00:00')
248+
i = Date('2003-03-03.00:00:00') - Date('2003-02-01.00:00:00')
249249
self.assertEqual(i, Interval('30d'))
250-
i = Date('2003-03-03.00:00:00') - Date('2002-04-01.00:00:00')
250+
i = Date('2003-03-03.00:00:00') - Date('2002-02-01.00:00:00')
251+
self.assertEqual(i, Interval('395d'))
252+
i = Date('2003-03-03.00:00:00') - Date('2003-04-01.00:00:00')
251253
self.assertEqual(i, Interval('-29d'))
252-
i = Date('2003-03-01.00:00:00') - Date('2002-02-01.00:00:00')
253-
self.assertEqual(i, Interval('1m'))
254+
i = Date('2003-03-01.00:00:00') - Date('2003-02-01.00:00:00')
255+
self.assertEqual(i, Interval('28d'))
254256
# force the transition over a year boundary
255257
i = Date('2003-01-01.00:00:00') - Date('2002-01-01.00:00:00')
256258
self.assertEqual(i, Interval('365d'))

0 commit comments

Comments
 (0)