Mercurial > p > roundup > code
comparison roundup/date.py @ 641:cfa460943d4d
Oops, there's 24 hours in a day...
...and subtraction of intervals now works properly.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 21 Feb 2002 23:34:52 +0000 |
| parents | 7dd13fd5d8ea |
| children | 5e0a75bfdd90 |
comparison
equal
deleted
inserted
replaced
| 640:7dd13fd5d8ea | 641:cfa460943d4d |
|---|---|
| 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.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 __doc__ = """ | 20 __doc__ = """ |
| 21 Date, time and time interval handling. | 21 Date, time and time interval handling. |
| 22 """ | 22 """ |
| 23 | 23 |
| 121 hour < 0 or hour > 59): | 121 hour < 0 or hour > 59): |
| 122 if second < 0: minute -= 1; second += 60 | 122 if second < 0: minute -= 1; second += 60 |
| 123 elif second > 59: minute += 1; second -= 60 | 123 elif second > 59: minute += 1; second -= 60 |
| 124 if minute < 0: hour -= 1; minute += 60 | 124 if minute < 0: hour -= 1; minute += 60 |
| 125 elif minute > 59: hour += 1; minute -= 60 | 125 elif minute > 59: hour += 1; minute -= 60 |
| 126 if hour < 0: day -= 1; hour += 60 | 126 if hour < 0: day -= 1; hour += 24 |
| 127 elif hour > 59: day += 1; hour -= 60 | 127 elif hour > 59: day += 1; hour -= 24 |
| 128 | 128 |
| 129 # fix up the month so we're within range | 129 # fix up the month so we're within range |
| 130 while month < 1 or month > 12: | 130 while month < 1 or month > 12: |
| 131 if month < 1: year -= 1; month += 12 | 131 if month < 1: year -= 1; month += 12 |
| 132 if month > 12: year += 1; month -= 12 | 132 if month > 12: year += 1; month -= 12 |
| 155 def __sub__(self, other): | 155 def __sub__(self, other): |
| 156 """ Subtract: | 156 """ Subtract: |
| 157 1. an interval from this date to produce another date. | 157 1. an interval from this date to produce another date. |
| 158 2. a date from this date to produce an interval. | 158 2. a date from this date to produce an interval. |
| 159 """ | 159 """ |
| 160 if isinstance(other, Date): | 160 if isinstance(other, Interval): |
| 161 # TODO this code will fall over laughing if the dates cross | 161 other = Interval(other.get_tuple(), sign=-other.sign) |
| 162 # leap years, phases of the moon, .... | 162 return self.__add__(other) |
| 163 a = calendar.timegm((self.year, self.month, self.day, self.hour, | 163 |
| 164 self.minute, self.second, 0, 0, 0)) | 164 assert isinstance(other, Date), 'May only subtract Dates or Intervals' |
| 165 b = calendar.timegm((other.year, other.month, other.day, | 165 |
| 166 other.hour, other.minute, other.second, 0, 0, 0)) | 166 # TODO this code will fall over laughing if the dates cross |
| 167 diff = a - b | 167 # leap years, phases of the moon, .... |
| 168 if diff < 0: | 168 a = calendar.timegm((self.year, self.month, self.day, self.hour, |
| 169 sign = 1 | 169 self.minute, self.second, 0, 0, 0)) |
| 170 diff = -diff | 170 b = calendar.timegm((other.year, other.month, other.day, |
| 171 else: | 171 other.hour, other.minute, other.second, 0, 0, 0)) |
| 172 sign = -1 | 172 diff = a - b |
| 173 S = diff%60 | 173 if diff < 0: |
| 174 M = (diff/60)%60 | 174 sign = 1 |
| 175 H = (diff/(60*60))%60 | 175 diff = -diff |
| 176 if H>1: S = 0 | 176 else: |
| 177 d = (diff/(24*60*60))%30 | 177 sign = -1 |
| 178 if d>1: H = S = M = 0 | 178 S = diff%60 |
| 179 m = (diff/(30*24*60*60))%12 | 179 M = (diff/60)%60 |
| 180 if m>1: H = S = M = 0 | 180 H = (diff/(60*60))%60 |
| 181 y = (diff/(365*24*60*60)) | 181 if H>1: S = 0 |
| 182 if y>1: d = H = S = M = 0 | 182 d = (diff/(24*60*60))%30 |
| 183 return Interval((y, m, d, H, M, S), sign=sign) | 183 if d>1: H = S = M = 0 |
| 184 return self.__add__(other) | 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 def __cmp__(self, other): | 190 def __cmp__(self, other): |
| 187 """Compare this date to another date.""" | 191 """Compare this date to another date.""" |
| 188 if other is None: | 192 if other is None: |
| 189 return 1 | 193 return 1 |
| 431 if __name__ == '__main__': | 435 if __name__ == '__main__': |
| 432 test() | 436 test() |
| 433 | 437 |
| 434 # | 438 # |
| 435 # $Log: not supported by cvs2svn $ | 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 # Revision 1.18 2002/01/23 20:00:50 jhermann | 445 # Revision 1.18 2002/01/23 20:00:50 jhermann |
| 437 # %e is a UNIXism and not documented for Python | 446 # %e is a UNIXism and not documented for Python |
| 438 # | 447 # |
| 439 # Revision 1.17 2002/01/16 07:02:57 richard | 448 # Revision 1.17 2002/01/16 07:02:57 richard |
| 440 # . lots of date/interval related changes: | 449 # . lots of date/interval related changes: |
