Code

implemented the missing Interval.__add__
[roundup.git] / roundup / date.py
index 8e20c49ac0a54b911291256690e3179a126f5463..6299318d0cbbb65a299f61d465e68e01cb885ba6 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.26 2002-09-10 00:18:20 richard Exp $
+# $Id: date.py,v 1.35 2002-10-11 01:25:40 richard Exp $
 
 __doc__ = """
 Date, time and time interval handling.
@@ -153,14 +153,15 @@ class Date:
         """
         return Date(self.addInterval(interval))
 
-    # XXX deviates from spec to allow subtraction of dates as well
+    # deviates from spec to allow subtraction of dates as well
     def __sub__(self, other):
         """ Subtract:
              1. an interval from this date to produce another date.
              2. a date from this date to produce an interval.
         """
         if isinstance(other, Interval):
-            other = Interval(other.get_tuple(), sign=-other.sign)
+            other = Interval(other.get_tuple())
+            other.sign *= -1
             return self.__add__(other)
 
         assert isinstance(other, Date), 'May only subtract Dates or Intervals'
@@ -215,21 +216,26 @@ class Date:
 
     def set(self, spec, offset=0, date_re=re.compile(r'''
             (((?P<y>\d\d\d\d)-)?((?P<m>\d\d?)-(?P<d>\d\d?))?)? # yyyy-mm-dd
-            (?P<n>\.)?                                         # .
-            (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)?      # hh:mm:ss
-            (?P<o>.+)?                                         # offset
-            ''', re.VERBOSE), serialised_re=re.compile('''
-            (?P<y>\d{4})(?P<m>\d{2})(?P<d>\d{2}) # yyyymmdd
-            (?P<H>\d{2})(?P<M>\d{2})(?P<S>\d{2}) # HHMMSS
+            (?P<n>\.)?                                     # .
+            (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d))?)?  # hh:mm:ss
+            (?P<o>.+)?                                     # offset
+            ''', re.VERBOSE), serialised_re=re.compile(r'''
+            (\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)
             ''', re.VERBOSE)):
         ''' set the date to the value in spec
         '''
         m = serialised_re.match(spec)
-        if not m:
-            m = date_re.match(spec)
-            if not m:
-                raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].'
-                    '[[h]h:mm[:ss]][offset]')
+        if m is not None:
+            # we're serialised - easy!
+            self.year, self.month, self.day, self.hour, self.minute, \
+                self.second = map(int, m.groups()[:6])
+            return
+
+        # not serialised data, try usual format
+        m = date_re.match(spec)
+        if m is None:
+            raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].'
+                '[[h]h:mm[:ss]][offset]')
 
         info = m.groupdict()
 
@@ -315,9 +321,14 @@ class Interval:
         if type(spec) == type(''):
             self.set(spec)
         else:
-            self.sign = sign
-            self.year, self.month, self.day, self.hour, self.minute, \
-                self.second = spec
+            if len(spec) == 7:
+                self.sign, self.year, self.month, self.day, self.hour, \
+                    self.minute, self.second = spec
+            else:
+                # old, buggy spec form
+                self.sign = sign
+                self.year, self.month, self.day, self.hour, self.minute, \
+                    self.second = spec
 
     def __cmp__(self, other):
         """Compare this interval to another interval."""
@@ -343,6 +354,22 @@ class Interval:
             l.append('%d:%02d'%(self.hour, self.minute))
         return ' '.join(l)
 
+    def __add__(self, other):
+        if isinstance(other, Date):
+            # the other is a Date - produce a Date
+            return Date(other.addInterval(self))
+        elif isinstance(other, Interval):
+            # add the other Interval to this one
+            a = self.get_tuple()
+            b = other.get_tuple()
+            if b[0] < 0:
+                i = Interval([x-y for x,y in zip(a[1:],b[1:])])
+            else:
+                i = Interval([x+y for x,y in zip(a[1:],b[1:])])
+            return i
+        # nope, no idea what to do with this other...
+        raise TypeError, "Can't add %r"%other
+
     def set(self, spec, interval_re=re.compile('''
             \s*(?P<s>[-+])?         # + or -
             \s*((?P<y>\d+\s*)y)?    # year
@@ -351,7 +378,7 @@ class Interval:
             \s*((?P<d>\d+\s*)d)?    # day
             \s*(((?P<H>\d+):(?P<M>\d+))?(:(?P<S>\d+))?)?   # time
             \s*''', re.VERBOSE), serialised_re=re.compile('''
-            (?P<s>[+-])(?P<y>\d{4})(?P<m>\d{2})(?P<d>\d{2})
+            (?P<s>[+-])?1?(?P<y>([ ]{3}\d|\d{4}))(?P<m>\d{2})(?P<d>\d{2})
             (?P<H>\d{2})(?P<M>\d{2})(?P<S>\d{2})''', re.VERBOSE)):
         ''' set the date to the value in spec
         '''
@@ -435,7 +462,8 @@ class Interval:
             self.minute, self.second)
 
     def serialise(self):
-        return '%s%4d%02d%02d%02d%02d%02d'%(self.sign, self.year, self.month,
+        sign = self.sign > 0 and '+' or '-'
+        return '%s%04d%02d%02d%02d%02d%02d'%(sign, self.year, self.month,
             self.day, self.hour, self.minute, self.second)