Code

Unit tests and a few fixes.
[roundup.git] / roundup / date.py
index c170fb6b3ac3c61c5f2921ce8fc984cf47b572aa..a7f212a553c0ca3c15a9a96db610c37afe49beb6 100644 (file)
@@ -15,7 +15,7 @@
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-# $Id: date.py,v 1.20 2002-02-21 23:34:51 richard Exp $
+# $Id: date.py,v 1.23 2002-07-18 23:07:08 richard Exp $
 
 __doc__ = """
 Date, time and time interval handling.
@@ -91,29 +91,17 @@ class Date:
             self.year, self.month, self.day, self.hour, self.minute, \
                 self.second, x, x, x = time.gmtime(ts)
 
-    def applyInterval(self, interval):
-        ''' Apply the interval to this date
+    def addInterval(self, interval):
+        ''' Add the interval to this date, returning the date tuple
         '''
-        t = (self.year + interval.year,
-             self.month + interval.month,
-             self.day + interval.day,
-             self.hour + interval.hour,
-             self.minute + interval.minute,
-             self.second + interval.second, 0, 0, 0)
-        self.year, self.month, self.day, self.hour, self.minute, \
-            self.second, x, x, x = time.gmtime(calendar.timegm(t))
-
-    def __add__(self, other):
-        """Add an interval to this date to produce another date.
-        """
         # do the basic calc
-        sign = other.sign
-        year = self.year + sign * other.year
-        month = self.month + sign * other.month
-        day = self.day + sign * other.day
-        hour = self.hour + sign * other.hour
-        minute = self.minute + sign * other.minute
-        second = self.second + sign * other.second
+        sign = interval.sign
+        year = self.year + sign * interval.year
+        month = self.month + sign * interval.month
+        day = self.day + sign * interval.day
+        hour = self.hour + sign * interval.hour
+        minute = self.minute + sign * interval.minute
+        second = self.second + sign * interval.second
 
         # now cope with under- and over-flow
         # first do the time
@@ -148,8 +136,18 @@ class Date:
             # re-figure the number of days for this month
             if month == 2 and calendar.isleap(year): month_days = 29
             else: month_days = mdays[month]
+        return (year, month, day, hour, minute, second, 0, 0, 0)
+
+    def applyInterval(self, interval):
+        ''' Apply the interval to this date
+        '''
+        self.year, self.month, self.day, self.hour, self.minute, \
+            self.second, x, x, x = self.addInterval(interval)
 
-        return Date((year, month, day, hour, minute, second, 0, 0, 0))
+    def __add__(self, interval):
+        """Add an interval to this date to produce another date.
+        """
+        return Date(self.addInterval(interval))
 
     # XXX deviates from spec to allow subtraction of dates as well
     def __sub__(self, other):
@@ -192,6 +190,8 @@ class Date:
         if other is None:
             return 1
         for attr in ('year', 'month', 'day', 'hour', 'minute', 'second'):
+            if not hasattr(other, attr):
+                return 1
             r = cmp(getattr(self, attr), getattr(other, attr))
             if r: return r
         return 0
@@ -306,6 +306,8 @@ class Interval:
         if other is None:
             return 1
         for attr in ('year', 'month', 'day', 'hour', 'minute', 'second'):
+            if not hasattr(other, attr):
+                return 1
             r = cmp(getattr(self, attr), getattr(other, attr))
             if r: return r
         return 0
@@ -366,8 +368,11 @@ class Interval:
     def pretty(self):
         ''' print up the date date using one of these nice formats..
         '''
-        if self.year or self.month > 2:
-            return None
+        if self.year:
+            if self.year == 1:
+                return _('1 year')
+            else:
+                return _('%(number)s years')%{'number': self.year}
         elif self.month or self.day > 13:
             days = (self.month * 30) + self.day
             if days > 28:
@@ -437,6 +442,16 @@ if __name__ == '__main__':
 
 #
 # $Log: not supported by cvs2svn $
+# Revision 1.22  2002/07/14 06:05:50  richard
+#  . fixed the date module so that Date(". - 2d") works
+#
+# Revision 1.21  2002/05/15 06:32:46  richard
+#  . reverting to dates for intervals > 2 months sucks
+#
+# Revision 1.20  2002/02/21 23:34:51  richard
+# Oops, there's 24 hours in a day, and subtraction of intervals now works
+# properly.
+#
 # Revision 1.19  2002/02/21 23:11:45  richard
 #  . fixed some problems in date calculations (calendar.py doesn't handle over-
 #    and under-flow). Also, hour/minute/second intervals may now be more than