helpers.py
author Tero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 06:19:12 +0200
changeset 58 ce028d356e1f
parent 55 5667d2bbdc50
child 60 759369a79527
permissions -rw-r--r--
special-case current month in calendar
"""
    Some additional helpers
"""

import qmsk.web.helpers

import datetime, calendar

class Helpers (qmsk.web.helpers.Helpers) :
    """
        Our set of helpers, inheriting from base helpers
    """

    def tz_name (self, tz) :
        """
            Returns a string describing the given timezone
        """

        return str(tz)

    def fmt_date (self, date) :
        """
            Formats a date
        """
        
        # XXX: hardcoded
        return date.strftime('%Y-%m-%d')

    def fmt_month (self, date) :
        """
            Formats a month
        """

        return date.strftime('%B %Y')
        
    def fmt_weekday (self, wday) :
        """
            Formats an abbreviated weekday name
        """

        return calendar.day_abbr[wday]

    def build_date (self, month, mday) :
        """
            Returns a datetime.date for the given (month.year, month.month, mday)
        """

        return datetime.date(month.year, month.month, mday)
    
    def now (self) :
        """
            Build current time
        """

        return self.ctx['timezone'].localize(datetime.datetime.now())

    def today (self) :
        """
            Build today's date
        """
        
        return self.now().date()

    def is_today (self, date) :
        """
            Checks if the given date is today
        """

        # construct current date
        return date == self.today()
    
    def is_this_month (self, month) :
        """
            Checks the given month is the current month
        """

        today = self.today()

        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
        """

        if month.month == 1 :
            return month.year - 1

        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

    def prev_month (self, month) :
        """
            Returns the month before the given one
        """

        if month.month == 1 :
            return 12

        else :
            return month.month - 1

    def next_month (self, month) :
        """
            Returns the month after the given one
        """

        if month.month == 12 :
            return 1

        else :
            return month.month + 1