Python3 standard library: calendar with dates

1. calendar with dates

The first category of Calendar calendar module, which encapsulates some calculated values, such as a given day of the week or month of the year. Further, TextCalendar HTMLCalendar and outputs the generated class can preformatted.

1.1 Formatting Example

prmonth () function is a simple method, you can generate monthly formatted text output.

import calendar

c = calendar.TextCalendar(calendar.SUNDAY)
c.prmonth ( 2017, 7)

This example in accordance with US practice, will TextCalendar configure one week starting from Sunday. The default will use the European convention, namely weeks starting on Monday. This example generates the following output.

And using HTMLCalendar formatmonth () can generate a table similar to HTML. Output display looks plain text version is roughly the same, but will be surrounded by HTML tags. Each table cell corresponds to a class attribute of the week, so that HTML styles can be developed by CSS. 

In addition to using the default format, in order to generate an output to some other format, may be calculated using the calendar date, and these values ​​are organized as a weekly and monthly intervals, then iterates the processing result. For this task, Calendar of weekheader (), monthcalendar () and yeardays2calendar () method is particularly useful.

Call yeardays2calendar () will generate a sequence consisting of "Moon line" list. List contains some month every month, every month is one week list. Zhou is a list of tuples, tuples by day number (1 to 31) the week number (0-6) constituted. Day of the month as a number other than 0.

import calendar
 import pprint

cal = calendar.Calendar(calendar.SUNDAY)

cal_data = cal.yeardays2calendar(2020, 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, width = 65)

Call yeardays2calendar (2020,3) returns the data in 2020, and every three months the line organization.

This is equivalent to formatyear () data used. 

import calendar

cal = calendar.TextCalendar(calendar.SUNDAY)
print(cal.formatyear(2020, 2, 1, 1, 3))

If given the same parameter, the formatyear () generates the following output.

1.2 locale

If not for the current locale, but to generate a calendar format to another locale, you can use LocaleTextCalendar or LocaleHTMLCalendar.

import calendar

c = calendar.LocaleTextCalendar (locale = ' en_US ' )
c.prmonth ( 2020, 2 )

print()

c = calendar.LocaleTextCalendar(locale='fr_FR')
c.prmonth ( 2020, 3)

The first day of the week does not belong to the set locale. This parameter value is taken from the class calendar, conventional TextCalendar class as well.

1.3 Computation Date

尽管calendar模块主要强调采用不同格式打印完整的日历,但它还是提供了另外一些函数,对采用其他方式处理日期很有用,如为一个重复事件计算日期。例如,Python Atlanta用户组每月的第二个星期四召开一次会议。要计算一年中的会议日期,可以使用monthcalendar()的返回值。

import calendar
import pprint

pprint.pprint(calendar.monthcalendar(2020, 7))

有些日期的值为0。这说明这几天对应的星期几尽管在给定的当前月份里,但它们实际上属于另一个月。

一周中的第一天默认为星期一。可以通过调用setfirstweekday()来改变这个设置,不过由于calendar模块包含了一些常量来索引monthcalendar()返回的日期区间,所以在这种情况下更方便的做法是跳过这一步。

要计算一年的会议日期,假设是每个月的第二个星期四,那么可以查看monthcalendar()的输出,找到星期四对应的日期。一个月的第一周和最后一周都要填充0值作为占位符,分别表示相应日期实际上在前一个月或下一个月。例如,如果一个月从星期五开始,那么第一周星期四位置上的值就是0。

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12384713.html