Python STL Calendar

Source: Internet
Author: User
Tags html tags string format pprint

Role: The Calendar module implements classes to handle dates and manage values for the year, month, and week.

Python version: I.4 version, updated in 2.5

The Calendar module defines the Calendar class, which encapsulates the calculation of some values, such as the number of weeks in a given one month or year. In addition, the Textcalendar and Htmlcalendar classes can generate pre-formatted output.

Module overview
  1. Calendar.calendar (year,w=2,l=1,c=6)
    Returns the year calendar of a multi-line string format with a 3-month line with a distance of C. The daily width interval is w characters. The length of each line is 21* w+18+2* C. L is the number of rows per week.
  2. Calendar.firstweekday ()
    Returns the settings for the current weekly start date. By default, 0 is returned when the Caendar module is first loaded, which is Monday.
  3. Calendar.isleap (year)
    is a leap year that returns true, otherwise false.
  4. Calendar.leapdays (Y1,y2)
    Returns the total number of leap years between y1,y2 two.
  5. Calendar.month (year,month,w=2,l=1)
    Returns a multi-line string format for year month calendar, two row headings, and one week row. The daily width interval is w characters. The length of each line is 7* w+6. L is the number of rows per week.
  6. Calendar.monthcalendar (Year,month)
    Returns a single-level nested list of integers. Each sub-list is loaded with integers representing one weeks. The date of year month is set to 0, and the day of the month is indicated by the day of the week, starting from 1.
  7. Calendar.monthrange (Year,month)
    Returns a two integer. The first is the day of the week of the month, and the second is the date code for that month. Day from 0 (Monday) to 6 (Sunday); The month is from 1 to 12.
  8. Calendar.prcal (year,w=2,l=1,c=6)
    Equivalent to print Calendar.calendar (year,w,l,c).
  9. Calendar.prmonth (year,month,w=2,l=1)
    Equivalent to print Calendar.calendar (year,w,l,c).
  10. Calendar.setfirstweekday (Weekday)
    Set the starting date code for the week. 0 (Monday) to 6 (Sunday).
  11. CALENDAR.TIMEGM (Tupletime)
    Contrary to Time.gmtime: accepts a time tuple form, returning the timestamp of that moment (the number of floating-point seconds elapsed after the 1970 era).
  12. Calendar.weekday (Year,month,day)
    Returns the date code for the given date. 0 (Monday) to 6 (Sunday). The month is 1 (January) to 12 (December).
Formatting

The Prmonth method is a simple function that can generate one months of formatted text output.

import calendarcal = calendar.TextCalendar(calendar.SUNDAY)print cal.formatyear(2011, 2, 1, 1, 3)

This example is based on the American practice of configuring Textcalendar as a week starting from Sunday. The default is to use European conventions, which are one week from Monday onwards.

The output is as follows:

                              January February marchsu Mo tu We Th Fr Sa Su Mo tu W  E Th fr sa Su Mo Tu We Th fr SA 1 1 2 3 4 5 1 2 3 4 5 2 3 4 5 6 7 8 6   7 8 9 10 11 12 6 7 8 9 10 11 12 9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 1916 17 18 19 20 21 22                  It is a $2623 and a. 3130-April-------   May junesu mo tu we th fr sa su mo tu we th fr sa su mo tu we th fr sa 1  2 1 2 3 4 5 6 7 1 2 3 4 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 1110 11 12 13  14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 1817 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 2524 25  From $ July August Septembersu Mo Tu, I, I Th Fr Sa Su Mo Tu We TH Fr sa Su Mo Tu We Th Fr sa 1 2 1 2 3 4 5 6 1 2 3 3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 1010 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 1717 18 19 20 21 22 23 2               1 in all, the 2424, and the 3031 October, and so on, and so on.                   November decembersu Mo tu we th fr sa su mo tu we th fr sa su mo tu we th fr sa 1 1 2 3 4 5 1 2 3 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 1716 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 2423 24 (+) (+ 3130 31[finished in 0.1s]).

A similar HTML table can be generated using Htmlcalendar and Formatmonth (). The output appears to be roughly the same as the plain text version, but is surrounded by HTML tags. Each table cell has a class property that corresponds to the day of the week, so you can specify HTML styles through CSS.

To generate output using a format other than the available default formats, you can use calendar to calculate dates, organize them into weeks and months, and then iterate over the results. For this task, the Weekheader (), MonthCalendar (), and Yeardays2calendar () methods of the calendar are particularly useful.

Calling Yeardays2calendar generates a sequence of "month column" lists. Each list contains a few months, and each month is a weekly list. The week is a list of tuples, which are made up of day numbers (1?31) and Days of the Week (0~6). The day number other than the month is 0.

import calendarimport pprintcal = calendar.Calendar(calendar.SUNDAY)cal_data = cal.yeardays2calendar(2011, 3)print 'len(cal_data)      :', len(cal_data)top_months = cal_data[0]print 'len(top_months)    :', len(top_months)first_month = top_months[0]print 'len(first_month)   :', len(first_month)print 'first_month:'pprint.pprint(first_month)

Calling Yeardays2calendar (2011, 3) returns data for the 20LL year, organized by 3 months per column.

len(cal_data)      : 4len(top_months)    : 3len(first_month)   : 6first_month:[[(0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5)], [(2, 6), (3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5)], [(9, 6), (10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5)], [(16, 6), (17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5)], [(23, 6), (24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5)], [(30, 6), (31, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5)]][Finished in 0.1s]

This is equivalent to the data used by Formatyear.

import calendarcal = calendar.TextCalendar(calendar.SUNDAY)print cal.formatyear(2011, 2, 1, 1, 3)

For the same parameters, Formatyear produces the following output.

                              January February marchsu Mo tu We Th Fr Sa Su Mo tu W  E Th fr sa Su Mo Tu We Th fr SA 1 1 2 3 4 5 1 2 3 4 5 2 3 4 5 6 7 8 6   7 8 9 10 11 12 6 7 8 9 10 11 12 9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 1916 17 18 19 20 21 22                  It is a $2623 and a. 3130-April-------   May junesu mo tu we th fr sa su mo tu we th fr sa su mo tu we th fr sa 1  2 1 2 3 4 5 6 7 1 2 3 4 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 1110 11 12 13  14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 1817 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 2524 25  From $ July August Septembersu Mo Tu, I, I Th Fr Sa Su Mo Tu We TH Fr sa Su Mo Tu We Th Fr sa 1 2 1 2 3 4 5 6 1 2 3 3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 1010 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 1717 18 19 20 21 22 23 2               1 in all, the 2424, and the 3031 October, and so on, and so on.                   November decembersu Mo tu we th fr sa su mo tu we th fr sa su mo tu we th fr sa 1 1 2 3 4 5 1 2 3 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 1716 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 2423 24 (+) (+ 3130 31[finished in 0.1s]).

The Day_name, Day_abbr, Month_name, and MONTH_ABBR module properties are useful for generating output in custom formats (for example, include links in HTML output). These properties are correctly and automatically configured for the current localized environment.

Calculate date

Although the Calendar module focuses on printing a full calendar in a different format, it also provides other functions that are useful for working with dates in other ways, such as calculating dates for a repeating exciting piece. For example, the Python Atlanta Users Group will convene a meeting every month for the second Thursday. To calculate the meeting date in a year, you can use the return value of MonthCalendar

import calendarimport pprintpprint.pprint(calendar.monthcalendar(2011, 7))

Some dates have a value of 0. This means that although these days are are in another month, they are in the same one weeks as the days given in the current month.

[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]][Finished in 0.1s]

The first day of the week defaults to Monday. You can change this setting by calling Setfirstweekday, but because the Calendar module contains constants to index the date range returned by MonthCalendar (), it is easier to skip this step in this case.

To calculate the 2011 meeting date, assuming that it is the second Thursday of each month, the 0 value indicates whether the first week of Thursday is included in the month (or not included in this month, for example, starting from Friday).

import calendar# Show every monthfor month in range(1, 13):    # Compute the dates for each week that overlaps the month    c = calendar.monthcalendar(2011, month)    first_week = c[0]    second_week = c[1]    third_week = c[2]    # If there is a Thursday in the first week, the second Thursday    # is in the second week.  Otherwise, the second Thursday must     # be in the third week.    if first_week[calendar.THURSDAY]:        meeting_date = second_week[calendar.THURSDAY]    else:        meeting_date = third_week[calendar.THURSDAY]    print '%3s: %2s' % (calendar.month_abbr[month], meeting_date)

So, this year's meeting schedule is:

Jan: 13Feb: 10Mar: 10Apr: 14May: 12Jun:  9Jul: 14Aug: 11Sep:  8Oct: 13Nov: 10Dec:  8[Finished in 0.1s]

Python STL Calendar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.