    #######################################
    # From calendar.py
    # (and a few things from me)
    #######################################
    
    # Constants for months referenced later
    January = 1
    February = 2
    
    # Number of days per month (except for February in leap years)
    mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    # Constants for weekdays
    (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)    

    _firstweekday  = `MONDAY`  # 0 = Monday, 6 = Sunday
    
    def weekday_of_date(yr, mon, dt):        
        # Method used is one found from http://klausler.com/new-dayofweek.html
        yr = yr - 1900
        wkday = yr + (yr // 4)
        if isleap(yr) and mon <= February:
            wkday -= 1
        wkday += dt
        mon_skew = 0
        if mon == 5:
            mon_skew = 1
        elif mon == 8:
            mon_skew = 2
        elif mon == 2 or mon == 3 or mon == 11:
            mon_skew = 3
        elif mon == 6:
            mon_skew = 4
        elif mon == 9 or mon == 12:
            mon_skew = 5
        elif mon == 4 or mon == 7:
            mon_skew = 6

        wkday += mon_skew

        wkday = wkday % 7

        # This date comes `wkday` days after Sunday
        # Adjust for Sunday = 6, Monday = 0, ...
        if wkday == 0: wkday = 6
        else:          wkday -= 1

        return wkday

    #######################################
    # Functions from calendar.py
    #######################################
    def isleap(year):
        """Return 1 for leap years, 0 for non-leap years."""
        return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

    def weekday(year, month, day):
        """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
           day (1-31)."""
        #return datetime.date(year, month, day).weekday()
        return weekday_of_date( year, month, day )

    def monthrange(year, month):
        """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
           year, month."""
        day1 = weekday(year, month, 1)
        ndays = mdays[month] + (month == February and isleap(year))
        return day1, ndays

    def monthcalendar(year, month):
        """Return a matrix representing a month's calendar.
           Each row represents a week; days outside this month are zero."""
        day1, ndays = monthrange(year, month)
        rows = []
        r7 = range(7)
        # for leading 0's in first week
        d = ( int(_firstweekday) - day1 + 6 ) % 7 - 5
        while d <= ndays:
            row = [0, 0, 0, 0, 0, 0, 0]
            for i in r7:
                if 1 <= d <= ndays: row[i] = d
                d = d + 1
            rows.append(row)
        return rows
