Python calendar.monthcalendar() Examples

The following are 22 code examples of calendar.monthcalendar(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module calendar , or try the search function .
Example #1
Source File: utils.py    From jpholiday with MIT License 6 votes vote down vote up
def _week_day(date, week, weekday):
    """
    特定の月の第1月曜日などを返します。
    """
    if week < 1 or week > 5:
        return None

    if weekday < 1 or weekday > 7:
        return None

    lines = calendar.monthcalendar(date.year, date.month)

    days = []
    for line in lines:
        if line[weekday - 1] == 0:
            continue

        days.append(line[weekday - 1])

    return datetime.date(date.year, date.month, days[week - 1]) 
Example #2
Source File: date.py    From pendulum with MIT License 5 votes vote down vote up
def _last_of_month(self, day_of_week=None):
        """
        Modify to the last occurrence of a given day of the week
        in the current month. If no day_of_week is provided,
        modify to the last day of the month. Use the supplied consts
        to indicate the desired day_of_week, ex. pendulum.MONDAY.

        :type day_of_week: int or None

        :rtype: Date
        """
        dt = self

        if day_of_week is None:
            return dt.set(day=self.days_in_month)

        month = calendar.monthcalendar(dt.year, dt.month)

        calendar_day = (day_of_week - 1) % 7

        if month[-1][calendar_day] > 0:
            day_of_month = month[-1][calendar_day]
        else:
            day_of_month = month[-2][calendar_day]

        return dt.set(day=day_of_month) 
Example #3
Source File: test_calendar.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #4
Source File: test_calendar.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #5
Source File: test_calendar.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #6
Source File: test_calendar.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in range(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #7
Source File: test_calendar.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #8
Source File: calendrical.py    From AlphaPy with Apache License 2.0 5 votes vote down vote up
def get_nth_kday_of_month(gday, gmonth, gyear):
    r"""Convert Gregorian date to RDate format.

    Parameters
    ----------
    gday : int
        Gregorian day.
    gmonth : int
        Gregorian month.
    gyear : int
        Gregorian year.

    Returns
    -------
    nth : int
        Ordinal number of a given day's occurrence within the month,
        for example, the third Friday of the month.
    """

    this_month = calendar.monthcalendar(gyear, gmonth)
    nth_kday_tuple = next(((i, e.index(gday)) for i, e in enumerate(this_month) if gday in e), None)
    tuple_row = nth_kday_tuple[0]
    tuple_pos = nth_kday_tuple[1]
    nth = tuple_row + 1
    if tuple_row > 0 and this_month[0][tuple_pos] == 0:
        nth -= 1
    return nth


#
# Function get_rdate
# 
Example #9
Source File: datetime.py    From pendulum with MIT License 5 votes vote down vote up
def _last_of_month(self, day_of_week=None):
        """
        Modify to the last occurrence of a given day of the week
        in the current month. If no day_of_week is provided,
        modify to the last day of the month. Use the supplied consts
        to indicate the desired day_of_week, ex. DateTime.MONDAY.

        :type day_of_week: int or None

        :rtype: DateTime
        """
        dt = self.start_of("day")

        if day_of_week is None:
            return dt.set(day=self.days_in_month)

        month = calendar.monthcalendar(dt.year, dt.month)

        calendar_day = (day_of_week - 1) % 7

        if month[-1][calendar_day] > 0:
            day_of_month = month[-1][calendar_day]
        else:
            day_of_month = month[-2][calendar_day]

        return dt.set(day=day_of_month) 
Example #10
Source File: datetime.py    From pendulum with MIT License 5 votes vote down vote up
def _first_of_month(self, day_of_week):
        """
        Modify to the first occurrence of a given day of the week
        in the current month. If no day_of_week is provided,
        modify to the first day of the month. Use the supplied consts
        to indicate the desired day_of_week, ex. DateTime.MONDAY.

        :type day_of_week: int

        :rtype: DateTime
        """
        dt = self.start_of("day")

        if day_of_week is None:
            return dt.set(day=1)

        month = calendar.monthcalendar(dt.year, dt.month)

        calendar_day = (day_of_week - 1) % 7

        if month[0][calendar_day] > 0:
            day_of_month = month[0][calendar_day]
        else:
            day_of_month = month[1][calendar_day]

        return dt.set(day=day_of_month) 
Example #11
Source File: test_calendar.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #12
Source File: date.py    From pendulum with MIT License 5 votes vote down vote up
def _first_of_month(self, day_of_week):
        """
        Modify to the first occurrence of a given day of the week
        in the current month. If no day_of_week is provided,
        modify to the first day of the month. Use the supplied consts
        to indicate the desired day_of_week, ex. pendulum.MONDAY.

        :type day_of_week: int

        :rtype: Date
        """
        dt = self

        if day_of_week is None:
            return dt.set(day=1)

        month = calendar.monthcalendar(dt.year, dt.month)

        calendar_day = (day_of_week - 1) % 7

        if month[0][calendar_day] > 0:
            day_of_month = month[0][calendar_day]
        else:
            day_of_month = month[1][calendar_day]

        return dt.set(day=day_of_month) 
Example #13
Source File: recipe-496862.py    From code with MIT License 5 votes vote down vote up
def __init__(self, month, year, indent_level, indent_style):
        'x.__init__(...) initializes x'
        calendar.setfirstweekday(calendar.SUNDAY)
        matrix = calendar.monthcalendar(year, month)
        self.__table = HTML_Table(len(matrix) + 1, 7, indent_level, indent_style)
        for column, text in enumerate(calendar.day_name[-1:] + calendar.day_name[:-1]):
            self.__table.mutate(0, column, '<b>%s</b>' % text)
        for row, week in enumerate(matrix):
            for column, day in enumerate(week):
                if day:
                    self.__table.mutate(row + 1, column, '<b>%02d</b>\n<hr>\n' % day)
        self.__weekday, self.__alldays = calendar.monthrange(year, month)
        self.__weekday = ((self.__weekday + 1) % 7) + 6 
Example #14
Source File: recipe-576922.py    From code with MIT License 5 votes vote down vote up
def get_last_message_id(messages_ids, M, last_wanted):
    for i in messages_ids:
        try:
            message_lines = M.top( str(i), '0')[1]
        except poplib.error_proto:
            print 'Problem in pop top call...'
            continue

        for line in message_lines:
            if line.startswith('Date:'):

                date_hdr = line.partition('Date: ')[2]
                # print date_hdr
                try:
                    (y, month, d, \
                     h, min, sec, \
                     _, _, _, tzoffset) = parsedate_tz(date_hdr)
                except (TypeError): continue
                except (ValueError): continue

                # Python range builtin ?
                if month < 0 or month > 12: continue
                max_day_per_month = max(calendar.monthcalendar(y, month)[-1])
                if d <= 0 or d > max_day_per_month: continue
                if h < 0 or h > 23: continue
                if min < 0 or min > 59: continue

                date = datetime.datetime(y, month, d, h, min, sec)

                print date
                if date < last_wanted:
                    return i 
Example #15
Source File: telegramcalendar.py    From calendar-telegram with MIT License 5 votes vote down vote up
def create_calendar(year=None,month=None):
    """
    Create an inline keyboard with the provided year and month
    :param int year: Year to use in the calendar, if None the current year is used.
    :param int month: Month to use in the calendar, if None the current month is used.
    :return: Returns the InlineKeyboardMarkup object with the calendar.
    """
    now = datetime.datetime.now()
    if year == None: year = now.year
    if month == None: month = now.month
    data_ignore = create_callback_data("IGNORE", year, month, 0)
    keyboard = []
    #First row - Month and Year
    row=[]
    row.append(InlineKeyboardButton(calendar.month_name[month]+" "+str(year),callback_data=data_ignore))
    keyboard.append(row)
    #Second row - Week Days
    row=[]
    for day in ["Mo","Tu","We","Th","Fr","Sa","Su"]:
        row.append(InlineKeyboardButton(day,callback_data=data_ignore))
    keyboard.append(row)

    my_calendar = calendar.monthcalendar(year, month)
    for week in my_calendar:
        row=[]
        for day in week:
            if(day==0):
                row.append(InlineKeyboardButton(" ",callback_data=data_ignore))
            else:
                row.append(InlineKeyboardButton(str(day),callback_data=create_callback_data("DAY",year,month,day)))
        keyboard.append(row)
    #Last row - Buttons
    row=[]
    row.append(InlineKeyboardButton("<",callback_data=create_callback_data("PREV-MONTH",year,month,day)))
    row.append(InlineKeyboardButton(" ",callback_data=data_ignore))
    row.append(InlineKeyboardButton(">",callback_data=create_callback_data("NEXT-MONTH",year,month,day)))
    keyboard.append(row)

    return InlineKeyboardMarkup(keyboard) 
Example #16
Source File: test_calendar.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in range(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #17
Source File: test_calendar.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in range(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #18
Source File: test_calendar.py    From oss-ftp with MIT License 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #19
Source File: test_calendar.py    From BinderFilter with MIT License 5 votes vote down vote up
def check_weeks(self, year, month, weeks):
        cal = calendar.monthcalendar(year, month)
        self.assertEqual(len(cal), len(weeks))
        for i in xrange(len(weeks)):
            self.assertEqual(weeks[i], sum(day != 0 for day in cal[i])) 
Example #20
Source File: calendar_helpers.py    From woid with Apache License 2.0 5 votes vote down vote up
def month_calendar(year, month, days, service):
    calendar.setfirstweekday(calendar.SUNDAY)
    month_calendar = calendar.monthcalendar(int(year), int(month))

    month_name = calendar.month_name[int(month)]
    month_href = reverse('services:month', args=(service.slug, year, month))

    html = '<table><thead>'
    html += '<tr><th colspan="7"><a href="{0}">{1}</a></th></tr>'.format(month_href, month_name)
    html += '<tr><th>s</th><th>m</th><th>t</th><th>w</th><th>t</th><th>f</th><th>s</th></tr>'
    html += '</thead><tbody>'
    for week in month_calendar:
        html += '<tr>'
        for day in week:
            if day == 0:
                str_day = ''
            else:
                str_day = str(day).zfill(2)
            if str_day in days:
                day_href = reverse('services:day', args=(service.slug, year, month, str_day))
                html += '<td><a href="{0}">{1}</a></td>'.format(day_href, str_day)
            else:
                html += '<td>{0}</td>'.format(str_day)
        html += '</tr>'
    html += '</tbody></table>'
    return mark_safe(html) 
Example #21
Source File: calendar_helpers.py    From woid with Apache License 2.0 5 votes vote down vote up
def get_calendar(year, month):
    blank_week = [0, 0, 0, 0, 0, 0, 0]
    calendar.setfirstweekday(calendar.SUNDAY)
    c = calendar.monthcalendar(year, month)
    if len(c) == 4:
        c.append(blank_week)
    if len(c) == 5:
        c.append(blank_week)
    return c 
Example #22
Source File: date_pick.py    From PCWG with MIT License 4 votes vote down vote up
def fill_calendar(self):

        init_x = 40
        y = 70

        step_x = 27
        step_y = 20

        self.canvas.delete(self.base_number_tag)
        self.canvas.update()

        if self.selected_date is None:
            month_calender = calendar.monthcalendar(self.empty_date.year, self.empty_date.month)   
        else:
            month_calender = calendar.monthcalendar(self.selected_date.year, self.selected_date.month)   

        for row in month_calender:
            
            x = init_x 

            for item in row:    
            
                if item > 0:

                    self.canvas.create_text(x,
                                            y,
                                            text=str(item), 
                                            font=Calendar.FONT,
                                            tags=(self.base_number_tag, item))   

                    if not self.selected_date is None:
                        if self.selected_date.day == item:
                            self.move_to(self.circle_selected, (x, y))
                            self.show(self.circle_selected)

                x+= step_x
            
            y += step_y

        self.canvas.tag_bind(self.base_number_tag, "<ButtonRelease-1>", self.click_number)
        self.canvas.tag_bind(self.base_number_tag, "<Enter>", self.on_mouse_over)
        self.canvas.tag_bind(self.base_number_tag, "<Leave>", self.on_mouse_out)