comparison roundup/date.py @ 1941:bcc65c5b86e6

fixed date arithmetic to not allow day-of-month == 0 [SF#853306] fixed date arithmetic to limit hours-per-day to 24, not 60
author Richard Jones <richard@users.sourceforge.net>
date Thu, 04 Dec 2003 23:06:53 +0000
parents 71056b09f2bf
children c538a64b94a7
comparison
equal deleted inserted replaced
1939:1303c208419d 1941:bcc65c5b86e6
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.57 2003-11-19 22:53:15 jlgijsbers Exp $ 18 # $Id: date.py,v 1.58 2003-12-04 23:06:53 richard 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
188 second = self.second + sign * interval.second 188 second = self.second + sign * interval.second
189 189
190 # now cope with under- and over-flow 190 # now cope with under- and over-flow
191 # first do the time 191 # first do the time
192 while (second < 0 or second > 59 or minute < 0 or minute > 59 or 192 while (second < 0 or second > 59 or minute < 0 or minute > 59 or
193 hour < 0 or hour > 59): 193 hour < 0 or hour > 23):
194 if second < 0: minute -= 1; second += 60 194 if second < 0: minute -= 1; second += 60
195 elif second > 59: minute += 1; second -= 60 195 elif second > 59: minute += 1; second -= 60
196 if minute < 0: hour -= 1; minute += 60 196 if minute < 0: hour -= 1; minute += 60
197 elif minute > 59: hour += 1; minute -= 60 197 elif minute > 59: hour += 1; minute -= 60
198 if hour < 0: day -= 1; hour += 24 198 if hour < 0: day -= 1; hour += 24
199 elif hour > 59: day += 1; hour -= 24 199 elif hour > 23: day += 1; hour -= 24
200 200
201 # fix up the month so we're within range 201 # fix up the month so we're within range
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 def get_mdays(year,month): 207 def get_mdays(year, month):
208 if month == 2 and calendar.isleap(year): return 29 208 if month == 2 and calendar.isleap(year): return 29
209 else: return calendar.mdays[month] 209 else: return calendar.mdays[month]
210 210
211 while month < 1 or month > 12 or day < 0 or day > get_mdays(year,month): 211 while month < 1 or month > 12 or day < 1 or day > get_mdays(year,month):
212 # now to day under/over 212 # now to day under/over
213 if day < 0: 213 if day < 1:
214 # When going backwards, decrement month, then increment days 214 # When going backwards, decrement month, then increment days
215 month -= 1 215 month -= 1
216 day += get_mdays(year,month) 216 day += get_mdays(year,month)
217 elif day > get_mdays(year,month): 217 elif day > get_mdays(year,month):
218 # When going forwards, decrement days, then increment month 218 # When going forwards, decrement days, then increment month

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