helpers.py
changeset 112 090192b64d7e
parent 107 67f48e288102
child 113 9fc0eb751b6a
equal deleted inserted replaced
111:95c0c49d76aa 112:090192b64d7e
    11 class Helpers (qmsk.web.helpers.Helpers) :
    11 class Helpers (qmsk.web.helpers.Helpers) :
    12     """
    12     """
    13         Our set of helpers, inheriting from base helpers
    13         Our set of helpers, inheriting from base helpers
    14     """
    14     """
    15 
    15 
       
    16     # set contructor...
       
    17     set = set
       
    18 
       
    19     # reference to calendar instance
       
    20     calendar = calendar.Calendar()
       
    21 
    16     def tz_name (self, tz) :
    22     def tz_name (self, tz) :
    17         """
    23         """
    18             Returns a string describing the given timezone
    24             Returns a string describing the given timezone
    19         """
    25         """
    20 
    26 
    70 
    76 
    71         today = self.today()
    77         today = self.today()
    72 
    78 
    73         return (month.year == today.year and month.month == today.month)
    79         return (month.year == today.year and month.month == today.month)
    74 
    80 
    75     def prev_month_year (self, month) :
    81     @staticmethod
    76         """
    82     def _wrap_year (year, month) :
    77             Returns the year of the month before the given one
    83         """
    78         """
    84             Wraps month to between [1, 12], spilling overflow/underflow by to year.
    79 
    85 
    80         if month.month == 1 :
    86             Returns (year, month)
    81             return month.year - 1
    87         """
    82 
    88         
       
    89         # underflow?
       
    90         if month == 0 :
       
    91             # wrap to previous year
       
    92             return (year - 1, 12)
       
    93         
       
    94         # overflow?
       
    95         elif month == 13 :
       
    96             # wrap to next year
       
    97             return (year + 1, 1)
       
    98         
       
    99         # sane value
       
   100         elif 1 <= month <= 12 :
       
   101             return (year, month)
       
   102         
       
   103         # insane value
    83         else :
   104         else :
    84             return month.year
   105             assert False, "invalid year/month: %d/%d" % (year, month)
    85             
       
    86     def next_month_year (self, month) :
       
    87         """
       
    88             Returns the year of the month after the given one
       
    89         """
       
    90 
       
    91         if month.month == 12 :
       
    92             return month.year + 1
       
    93 
       
    94         else :
       
    95             return month.year
       
    96 
   106 
    97     def prev_month (self, month) :
   107     def prev_month (self, month) :
    98         """
   108         """
    99             Returns the month before the given one
   109             Returns the month preceding the given one (as a datetime.datetime)
   100         """
   110         """
   101 
   111         
   102         if month.month == 1 :
   112         # previous month
   103             return 12
   113         y, m = self._wrap_year(month.year, month.month - 1)
   104 
   114         
   105         else :
   115         # build datetime
   106             return month.month - 1
   116         return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
   107 
   117 
   108     def next_month (self, month) :
   118     def next_month (self, month) :
   109         """
   119         """
   110             Returns the month after the given one
   120             Returns the month following the given one (as a datetime.datetime)
   111         """
   121         """
   112 
   122         
   113         if month.month == 12 :
   123         # previous month
   114             return 1
   124         y, m = self._wrap_year(month.year, month.month + 1)
   115 
   125         
   116         else :
   126         # build datetime
   117             return month.month + 1
   127         return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
   118     
   128     
   119     def fmt_time (self, time=None) :
   129     def fmt_time (self, time=None) :
   120         """
   130         """
   121             Format given time, or current time
   131             Format given time, or current time
   122         """
   132         """