|
15 | 15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
16 | 16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
17 | 17 | # |
18 | | -# $Id: date.py,v 1.19 2002-02-21 23:11:45 richard Exp $ |
| 18 | +# $Id: date.py,v 1.20 2002-02-21 23:34:51 richard Exp $ |
19 | 19 |
|
20 | 20 | __doc__ = """ |
21 | 21 | Date, time and time interval handling. |
@@ -123,8 +123,8 @@ def __add__(self, other): |
123 | 123 | elif second > 59: minute += 1; second -= 60 |
124 | 124 | if minute < 0: hour -= 1; minute += 60 |
125 | 125 | elif minute > 59: hour += 1; minute -= 60 |
126 | | - if hour < 0: day -= 1; hour += 60 |
127 | | - elif hour > 59: day += 1; hour -= 60 |
| 126 | + if hour < 0: day -= 1; hour += 24 |
| 127 | + elif hour > 59: day += 1; hour -= 24 |
128 | 128 |
|
129 | 129 | # fix up the month so we're within range |
130 | 130 | while month < 1 or month > 12: |
@@ -157,31 +157,35 @@ def __sub__(self, other): |
157 | 157 | 1. an interval from this date to produce another date. |
158 | 158 | 2. a date from this date to produce an interval. |
159 | 159 | """ |
160 | | - if isinstance(other, Date): |
161 | | - # TODO this code will fall over laughing if the dates cross |
162 | | - # leap years, phases of the moon, .... |
163 | | - a = calendar.timegm((self.year, self.month, self.day, self.hour, |
164 | | - self.minute, self.second, 0, 0, 0)) |
165 | | - b = calendar.timegm((other.year, other.month, other.day, |
166 | | - other.hour, other.minute, other.second, 0, 0, 0)) |
167 | | - diff = a - b |
168 | | - if diff < 0: |
169 | | - sign = 1 |
170 | | - diff = -diff |
171 | | - else: |
172 | | - sign = -1 |
173 | | - S = diff%60 |
174 | | - M = (diff/60)%60 |
175 | | - H = (diff/(60*60))%60 |
176 | | - if H>1: S = 0 |
177 | | - d = (diff/(24*60*60))%30 |
178 | | - if d>1: H = S = M = 0 |
179 | | - m = (diff/(30*24*60*60))%12 |
180 | | - if m>1: H = S = M = 0 |
181 | | - y = (diff/(365*24*60*60)) |
182 | | - if y>1: d = H = S = M = 0 |
183 | | - return Interval((y, m, d, H, M, S), sign=sign) |
184 | | - return self.__add__(other) |
| 160 | + if isinstance(other, Interval): |
| 161 | + other = Interval(other.get_tuple(), sign=-other.sign) |
| 162 | + return self.__add__(other) |
| 163 | + |
| 164 | + assert isinstance(other, Date), 'May only subtract Dates or Intervals' |
| 165 | + |
| 166 | + # TODO this code will fall over laughing if the dates cross |
| 167 | + # leap years, phases of the moon, .... |
| 168 | + a = calendar.timegm((self.year, self.month, self.day, self.hour, |
| 169 | + self.minute, self.second, 0, 0, 0)) |
| 170 | + b = calendar.timegm((other.year, other.month, other.day, |
| 171 | + other.hour, other.minute, other.second, 0, 0, 0)) |
| 172 | + diff = a - b |
| 173 | + if diff < 0: |
| 174 | + sign = 1 |
| 175 | + diff = -diff |
| 176 | + else: |
| 177 | + sign = -1 |
| 178 | + S = diff%60 |
| 179 | + M = (diff/60)%60 |
| 180 | + H = (diff/(60*60))%60 |
| 181 | + if H>1: S = 0 |
| 182 | + d = (diff/(24*60*60))%30 |
| 183 | + if d>1: H = S = M = 0 |
| 184 | + m = (diff/(30*24*60*60))%12 |
| 185 | + if m>1: H = S = M = 0 |
| 186 | + y = (diff/(365*24*60*60)) |
| 187 | + if y>1: d = H = S = M = 0 |
| 188 | + return Interval((y, m, d, H, M, S), sign=sign) |
185 | 189 |
|
186 | 190 | def __cmp__(self, other): |
187 | 191 | """Compare this date to another date.""" |
@@ -433,6 +437,11 @@ def test(): |
433 | 437 |
|
434 | 438 | # |
435 | 439 | # $Log: not supported by cvs2svn $ |
| 440 | +# Revision 1.19 2002/02/21 23:11:45 richard |
| 441 | +# . fixed some problems in date calculations (calendar.py doesn't handle over- |
| 442 | +# and under-flow). Also, hour/minute/second intervals may now be more than |
| 443 | +# 99 each. |
| 444 | +# |
436 | 445 | # Revision 1.18 2002/01/23 20:00:50 jhermann |
437 | 446 | # %e is a UNIXism and not documented for Python |
438 | 447 | # |
|
0 commit comments