helpers.py
changeset 112 090192b64d7e
parent 107 67f48e288102
child 113 9fc0eb751b6a
--- a/helpers.py	Wed Feb 11 22:24:55 2009 +0200
+++ b/helpers.py	Wed Feb 11 22:56:43 2009 +0200
@@ -13,6 +13,12 @@
         Our set of helpers, inheriting from base helpers
     """
 
+    # set contructor...
+    set = set
+
+    # reference to calendar instance
+    calendar = calendar.Calendar()
+
     def tz_name (self, tz) :
         """
             Returns a string describing the given timezone
@@ -72,49 +78,53 @@
 
         return (month.year == today.year and month.month == today.month)
 
-    def prev_month_year (self, month) :
-        """
-            Returns the year of the month before the given one
+    @staticmethod
+    def _wrap_year (year, month) :
         """
+            Wraps month to between [1, 12], spilling overflow/underflow by to year.
 
-        if month.month == 1 :
-            return month.year - 1
-
+            Returns (year, month)
+        """
+        
+        # underflow?
+        if month == 0 :
+            # wrap to previous year
+            return (year - 1, 12)
+        
+        # overflow?
+        elif month == 13 :
+            # wrap to next year
+            return (year + 1, 1)
+        
+        # sane value
+        elif 1 <= month <= 12 :
+            return (year, month)
+        
+        # insane value
         else :
-            return month.year
-            
-    def next_month_year (self, month) :
-        """
-            Returns the year of the month after the given one
-        """
-
-        if month.month == 12 :
-            return month.year + 1
-
-        else :
-            return month.year
+            assert False, "invalid year/month: %d/%d" % (year, month)
 
     def prev_month (self, month) :
         """
-            Returns the month before the given one
+            Returns the month preceding the given one (as a datetime.datetime)
         """
-
-        if month.month == 1 :
-            return 12
-
-        else :
-            return month.month - 1
+        
+        # previous month
+        y, m = self._wrap_year(month.year, month.month - 1)
+        
+        # build datetime
+        return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
 
     def next_month (self, month) :
         """
-            Returns the month after the given one
+            Returns the month following the given one (as a datetime.datetime)
         """
-
-        if month.month == 12 :
-            return 1
-
-        else :
-            return month.month + 1
+        
+        # previous month
+        y, m = self._wrap_year(month.year, month.month + 1)
+        
+        # build datetime
+        return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
     
     def fmt_time (self, time=None) :
         """