comparison roundup/date.py @ 1885:deba54ed724f

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)
author Anthony Baxter <anthonybaxter@users.sourceforge.net>
date Mon, 03 Nov 2003 10:23:06 +0000
parents 2047425bf7e7
children 41ad8b781232
comparison
equal deleted inserted replaced
1884:dc55a195722d 1885:deba54ed724f
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 # 17 #
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 $
19 19
20 __doc__ = """ 20 __doc__ = """
21 Date, time and time interval handling. 21 Date, time and time interval handling.
22 """ 22 """
23 23
202 while month < 1 or month > 12: 202 while month < 1 or month > 12:
203 if month < 1: year -= 1; month += 12 203 if month < 1: year -= 1; month += 12
204 if month > 12: year += 1; month -= 12 204 if month > 12: year += 1; month -= 12
205 205
206 # now do the days, now that we know what month we're in 206 # now do the days, now that we know what month we're in
207 mdays = calendar.mdays 207 def get_mdays(year,month):
208 if month == 2 and calendar.isleap(year): month_days = 29 208 if month == 2 and calendar.isleap(year): return 29
209 else: month_days = mdays[month] 209 else: return calendar.mdays[month]
210 while month < 1 or month > 12 or day < 0 or day > month_days: 210
211 while month < 1 or month > 12 or day < 0 or day > get_mdays(year,month):
211 # now to day under/over 212 # now to day under/over
212 if day < 0: month -= 1; day += month_days 213 if day < 0:
213 elif day > month_days: month += 1; day -= month_days 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
214 221
215 # possibly fix up the month so we're within range 222 # possibly fix up the month so we're within range
216 while month < 1 or month > 12: 223 while month < 1 or month > 12:
217 if month < 1: year -= 1; month += 12 224 if month < 1: year -= 1; month += 12 ; day += 31
218 if month > 12: year += 1; month -= 12 225 if month > 12: year += 1; month -= 12
219 226
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]
223 return (year, month, day, hour, minute, second, 0, 0, 0) 227 return (year, month, day, hour, minute, second, 0, 0, 0)
224 228
225 def applyInterval(self, interval): 229 def applyInterval(self, interval):
226 ''' Apply the interval to this date 230 ''' Apply the interval to this date
227 ''' 231 '''

Roundup Issue Tracker: http://roundup-tracker.org/