Mercurial > p > roundup > code
comparison roundup/date.py @ 861:68cef2bb929d
fixed the date module so that Date(". - 2d") works
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Sun, 14 Jul 2002 06:05:50 +0000 |
| parents | 5e0a75bfdd90 |
| children | a568596dbea7 |
comparison
equal
deleted
inserted
replaced
| 860:2df32a73eb45 | 861:68cef2bb929d |
|---|---|
| 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.21 2002-05-15 06:32:46 richard Exp $ | 18 # $Id: date.py,v 1.22 2002-07-14 06:05:50 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 |
| 89 y,m,d,H,M,S,x,x,x = spec | 89 y,m,d,H,M,S,x,x,x = spec |
| 90 ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0)) | 90 ts = calendar.timegm((y,m,d,H+offset,M,S,0,0,0)) |
| 91 self.year, self.month, self.day, self.hour, self.minute, \ | 91 self.year, self.month, self.day, self.hour, self.minute, \ |
| 92 self.second, x, x, x = time.gmtime(ts) | 92 self.second, x, x, x = time.gmtime(ts) |
| 93 | 93 |
| 94 def applyInterval(self, interval): | 94 def addInterval(self, interval): |
| 95 ''' Apply the interval to this date | 95 ''' Add the interval to this date, returning the date tuple |
| 96 ''' | 96 ''' |
| 97 t = (self.year + interval.year, | |
| 98 self.month + interval.month, | |
| 99 self.day + interval.day, | |
| 100 self.hour + interval.hour, | |
| 101 self.minute + interval.minute, | |
| 102 self.second + interval.second, 0, 0, 0) | |
| 103 self.year, self.month, self.day, self.hour, self.minute, \ | |
| 104 self.second, x, x, x = time.gmtime(calendar.timegm(t)) | |
| 105 | |
| 106 def __add__(self, other): | |
| 107 """Add an interval to this date to produce another date. | |
| 108 """ | |
| 109 # do the basic calc | 97 # do the basic calc |
| 110 sign = other.sign | 98 sign = interval.sign |
| 111 year = self.year + sign * other.year | 99 year = self.year + sign * interval.year |
| 112 month = self.month + sign * other.month | 100 month = self.month + sign * interval.month |
| 113 day = self.day + sign * other.day | 101 day = self.day + sign * interval.day |
| 114 hour = self.hour + sign * other.hour | 102 hour = self.hour + sign * interval.hour |
| 115 minute = self.minute + sign * other.minute | 103 minute = self.minute + sign * interval.minute |
| 116 second = self.second + sign * other.second | 104 second = self.second + sign * interval.second |
| 117 | 105 |
| 118 # now cope with under- and over-flow | 106 # now cope with under- and over-flow |
| 119 # first do the time | 107 # first do the time |
| 120 while (second < 0 or second > 59 or minute < 0 or minute > 59 or | 108 while (second < 0 or second > 59 or minute < 0 or minute > 59 or |
| 121 hour < 0 or hour > 59): | 109 hour < 0 or hour > 59): |
| 146 if month > 12: year += 1; month -= 12 | 134 if month > 12: year += 1; month -= 12 |
| 147 | 135 |
| 148 # re-figure the number of days for this month | 136 # re-figure the number of days for this month |
| 149 if month == 2 and calendar.isleap(year): month_days = 29 | 137 if month == 2 and calendar.isleap(year): month_days = 29 |
| 150 else: month_days = mdays[month] | 138 else: month_days = mdays[month] |
| 151 | 139 return (year, month, day, hour, minute, second, 0, 0, 0) |
| 152 return Date((year, month, day, hour, minute, second, 0, 0, 0)) | 140 |
| 141 def applyInterval(self, interval): | |
| 142 ''' Apply the interval to this date | |
| 143 ''' | |
| 144 self.year, self.month, self.day, self.hour, self.minute, \ | |
| 145 self.second, x, x, x = self.addInterval(interval) | |
| 146 | |
| 147 def __add__(self, interval): | |
| 148 """Add an interval to this date to produce another date. | |
| 149 """ | |
| 150 return Date(self.addInterval(interval)) | |
| 153 | 151 |
| 154 # XXX deviates from spec to allow subtraction of dates as well | 152 # XXX deviates from spec to allow subtraction of dates as well |
| 155 def __sub__(self, other): | 153 def __sub__(self, other): |
| 156 """ Subtract: | 154 """ Subtract: |
| 157 1. an interval from this date to produce another date. | 155 1. an interval from this date to produce another date. |
| 438 if __name__ == '__main__': | 436 if __name__ == '__main__': |
| 439 test() | 437 test() |
| 440 | 438 |
| 441 # | 439 # |
| 442 # $Log: not supported by cvs2svn $ | 440 # $Log: not supported by cvs2svn $ |
| 441 # Revision 1.21 2002/05/15 06:32:46 richard | |
| 442 # . reverting to dates for intervals > 2 months sucks | |
| 443 # | |
| 443 # Revision 1.20 2002/02/21 23:34:51 richard | 444 # Revision 1.20 2002/02/21 23:34:51 richard |
| 444 # Oops, there's 24 hours in a day, and subtraction of intervals now works | 445 # Oops, there's 24 hours in a day, and subtraction of intervals now works |
| 445 # properly. | 446 # properly. |
| 446 # | 447 # |
| 447 # Revision 1.19 2002/02/21 23:11:45 richard | 448 # Revision 1.19 2002/02/21 23:11:45 richard |
