calendar --- 日歷相關(guān)函數(shù)?

源代碼: Lib/calendar.py


這個模塊讓你可以輸出像 Unix cal 那樣的日歷,它還提供了其它與日歷相關(guān)的實用函數(shù)。 默認(rèn)情況下,這些日歷把星期一當(dāng)作一周的第一天,星期天為一周的最后一天(按照歐洲慣例)。 可以使用 setfirstweekday() 方法設(shè)置一周的第一天為星期天 (6) 或者其它任意一天。 使用整數(shù)作為指定日期的參數(shù)。 更多相關(guān)的函數(shù),參見 datetimetime 模塊。

在這個模塊中定義的函數(shù)和類都基于一個理想化的日歷,現(xiàn)行公歷向過去和未來兩個方向無限擴(kuò)展。這與 Dershowitz 和 Reingold 的書 "歷法計算" 中所有計算的基本日歷 -- "proleptic Gregorian" 日歷的定義相符合。 ISO 8601標(biāo)準(zhǔn)還規(guī)定了 0 和 負(fù)數(shù)年份。0年指公元前1年, -1年指公元前2年,依此類推。

class calendar.Calendar(firstweekday=0)?

Creates a Calendar object. firstweekday is an integer specifying the first day of the week. MONDAY is 0 (the default), SUNDAY is 6.

Calendar 對象提供了一些可被用于準(zhǔn)備日歷數(shù)據(jù)格式化的方法。 這個類本身不執(zhí)行任何格式化操作。 這部分任務(wù)應(yīng)由子類來完成。

Calendar 類的實例有下列方法:

iterweekdays()?

返回一個迭代器,迭代器的內(nèi)容為一星期的數(shù)字。迭代器的第一個值與 firstweekday 屬性的值一至。

itermonthdates(year, month)?

返回一個迭代器,迭代器的內(nèi)容為 yearmonth 月(1-12)的日期。這個迭代器返回當(dāng)月的所有日期 ( datetime.date 對象),日期包含了本月頭尾用于組成完整一周的日期。

itermonthdays(year, month)?

返回一個迭代器,迭代器的內(nèi)容與 itermonthdates() 類似,為 yearmonth 月的日期,但不受 datetime.date 范圍限制。返回的日期為當(dāng)月每一天的日期對應(yīng)的天數(shù)。對于不在當(dāng)月的日期,顯示為 0。

itermonthdays2(year, month)?

返回一個迭代器,迭代器的內(nèi)容與 itermonthdates() 類似為 yearmonth 月的日期,但不受 datetime.date 范圍的限制。迭代器中的元素為一個由日期和代表星期幾的數(shù)字組成的的元組。

itermonthdays3(year, month)?

返回一個迭代器,迭代器的內(nèi)容與 itermonthdates() 類似為 yearmonth 月的日期,但不受 datetime.date 范圍的限制。迭代器的元素為一個由年,月,日組成的元組。

3.7 新版功能.

itermonthdays4(year, month)?

返回一個迭代器,迭代器的內(nèi)容與 itermonthdates() 類似為 yearmonth 月的日期,但不受 datetime.date 范圍的限制。迭代器的元素為一個由年,月,日和代表星期幾的數(shù)字組成的元組。

3.7 新版功能.

monthdatescalendar(year, month)?

返回一個表示指定年月的周列表。周列表由七個 datetime.date 對象組成。

monthdays2calendar(year, month)?

返回一個表示指定年月的周列表。周列表由七個代表日期的數(shù)字和代表周幾的數(shù)字組成的二元元組。

monthdayscalendar(year, month)?

返回一個表示指定年月的周列表。周列表由七個代表日期的數(shù)字組成。

yeardatescalendar(year, width=3)?

返回可以用來格式化的指定年月的數(shù)據(jù)。返回的值是一個列表,列表是月份組成的行。每一行包含了最多 width 個月(默認(rèn)為3)。每個月包含了4到6周,每周包含1--7天。每一天使用 datetime.date 對象。

yeardays2calendar(year, width=3)?

返回可以用來模式化的指定年月的數(shù)據(jù)(與 yeardatescalendar() 類似)。周列表的元素是由表示日期的數(shù)字和表示星期幾的數(shù)字組成的元組。不在這個月的日子為0。

yeardayscalendar(year, width=3)?

返回可以用來模式化的指定年月的數(shù)據(jù)(與 yeardatescalendar() 類似)。周列表的元素是表示日期的數(shù)字。不在這個月的日子為0。

class calendar.TextCalendar(firstweekday=0)?

可以使用這個類生成純文本日歷。

TextCalendar 實例有以下方法:

formatmonth(theyear, themonth, w=0, l=0)?

返回一個多行字符串來表示指定年月的日歷。w 為日期的寬度,但始終保持日期居中。l 指定了每星期占用的行數(shù)。以上這些還依賴于構(gòu)造器或者 setfirstweekday() 方法指定的周的第一天是哪一天。

prmonth(theyear, themonth, w=0, l=0)?

formatmonth() 方法一樣,返回一個月的日歷。

formatyear(theyear, w=2, l=1, c=6, m=3)?

返回一個多行字符串,這個字符串為一個 m 列日歷??蛇x參數(shù) w, l, 和 c 分別表示日期列數(shù), 周的行數(shù), 和月之間的間隔。同樣,以上這些還依賴于構(gòu)造器或者 setfirstweekday() 指定哪一天為一周的第一天。日歷的第一年由平臺依賴于使用的平臺。

pryear(theyear, w=2, l=1, c=6, m=3)?

formatyear() 方法一樣,返回一整年的日歷。

class calendar.HTMLCalendar(firstweekday=0)?

可以使用這個類生成 HTML 日歷。

HTMLCalendar 實例有以下方法:

formatmonth(theyear, themonth, withyear=True)?

返回一個 HTML 表格作為指定年月的日歷。 withyear 為真,則年份將會包含在表頭,否則只顯示月份。

formatyear(theyear, width=3)?

返回一個 HTML 表格作為指定年份的日歷。 width (默認(rèn)為3) 用于規(guī)定每一行顯示月份的數(shù)量。

formatyearpage(theyear, width=3, css='calendar.css', encoding=None)?

返回一個完整的 HTML 頁面作為指定年份的日歷。 width*(默認(rèn)為3) 用于規(guī)定每一行顯示的月份數(shù)量。 *css 為層疊樣式表的名字。如果不使用任何層疊樣式表,可以使用 None 。 encoding 為輸出頁面的編碼 (默認(rèn)為系統(tǒng)的默認(rèn)編碼)。

HTMLCalendar 有以下屬性,你可以重載它們來自定義應(yīng)用日歷的樣式。

cssclasses?

一個對應(yīng)星期一到星期天的 CSS class 列表。默認(rèn)列表為

cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]

可以向每天加入其它樣式

cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]

需要注意的是,列表的長度必須為7。

cssclass_noday?

工作日的 CSS 類在上個月或下個月發(fā)生。

3.7 新版功能.

cssclasses_weekday_head?

用于標(biāo)題行中的工作日名稱的 CSS 類 列表。默認(rèn)值與 cssclasses 相同。

3.7 新版功能.

cssclass_month_head?

月份的頭 CSS 類(由 formatmonthname() 使用)。默認(rèn)值為 "month" 。

3.7 新版功能.

cssclass_month?

某個月的月歷的 CSS 類(由 formatmonth() 使用)。默認(rèn)值為 "month" 。

3.7 新版功能.

cssclass_year?

某年的年歷的 CSS 類(由 formatyear() 使用)。默認(rèn)值為 "year"

3.7 新版功能.

cssclass_year_head?

年歷的·表頭 CSS 類(由 formatyear() 使用)。默認(rèn)值為 "year" 。

3.7 新版功能.

需要注意的是,盡管上面命名的樣式類都是單獨出現(xiàn)的(如: cssclass_month cssclass_noday), 但我們可以使用空格將樣式類列表中的多個元素分隔開,例如:

"text-bold text-red"

下面是一個如何自定義 HTMLCalendar 的示例

class CustomHTMLCal(calendar.HTMLCalendar):
    cssclasses = [style + " text-nowrap" for style in
                  calendar.HTMLCalendar.cssclasses]
    cssclass_month_head = "text-center month-head"
    cssclass_month = "text-center month"
    cssclass_year = "text-italic lead"
class calendar.LocaleTextCalendar(firstweekday=0, locale=None)?

This subclass of TextCalendar can be passed a locale name in the constructor and will return month and weekday names in the specified locale.

class calendar.LocaleHTMLCalendar(firstweekday=0, locale=None)?

This subclass of HTMLCalendar can be passed a locale name in the constructor and will return month and weekday names in the specified locale.

備注

The constructor, formatweekday() and formatmonthname() methods of these two classes temporarily change the LC_TIME locale to the given locale. Because the current locale is a process-wide setting, they are not thread-safe.

對于簡單的文本日歷,這個模塊提供了以下方法。

calendar.setfirstweekday(weekday)?

設(shè)置每一周的開始(0 表示星期一,6 表示星期天)。calendar還提供了 MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAYSUNDAY 幾個常量方便使用。例如,設(shè)置每周的第一天為星期天

import calendar
calendar.setfirstweekday(calendar.SUNDAY)
calendar.firstweekday()?

返回當(dāng)前設(shè)置的每星期的第一天的數(shù)值。

calendar.isleap(year)?

如果 year 是閏年則返回 True ,否則返回 False

calendar.leapdays(y1, y2)?

返回在范圍 y1y2 (包含 y1 和 y2 )之間的閏年的年數(shù),其中 y1y2 是年份。

此函數(shù)適用于跨越一個世紀(jì)變化的范圍。

calendar.weekday(year, month, day)?

返回某年( 1970 -- ...),某月( 1 -- 12 ),某日( 1 -- 31 )是星期幾( 0 是星期一)。

calendar.weekheader(n)?

返回一個包含星期幾的縮寫名的頭。 n 指定星期幾縮寫的字符寬度。

calendar.monthrange(year, month)?

返回指定 年份 的指定 月份 的第一天是星期幾和這個月的天數(shù)。

calendar.monthcalendar(year, month)?

返回表示一個月的日歷的矩陣。 每一行代表一周;此月份外的日子由零表示。 每周從周一開始,除非使用 setfirstweekday() 改變設(shè)置。

calendar.prmonth(theyear, themonth, w=0, l=0)?

打印由 month() 返回的一個月的日歷。

calendar.month(theyear, themonth, w=0, l=0)?

使用 TextCalendar 類的 formatmonth() 以多行字符串形式返回月份日歷。

calendar.prcal(year, w=0, l=0, c=6, m=3)?

打印由 calendar() 返回的整年的日歷。

calendar.calendar(year, w=2, l=1, c=6, m=3)?

使用 TextCalendar 類的 formatyear() 返回整年的3列的日歷以多行字符串的形式。

calendar.timegm(tuple)?

一個不相關(guān)但很好用的函數(shù),它接受一個時間元組例如 time 模塊中的 gmtime() 函數(shù)的返回并返回相應(yīng)的 Unix 時間戳值,假定 1970 年開始計數(shù), POSIX 編碼。實際上, time.gmtime()timegm() 是彼此相反的。

calendar 模塊導(dǎo)出以下數(shù)據(jù)屬性:

calendar.day_name?

在當(dāng)前語言環(huán)境下表示星期幾的數(shù)組。

calendar.day_abbr?

在當(dāng)前語言環(huán)境下表示星期幾縮寫的數(shù)組。

calendar.month_name?

在當(dāng)前語言環(huán)境下表示一年中月份的數(shù)組。這遵循一月的月號為 1 的通常慣例,所以它的長度為 13 且 month_name[0] 是空字符串。

calendar.month_abbr?

在當(dāng)前語言環(huán)境下表示月份簡寫的數(shù)組。這遵循一月的月號為 1 的通常慣例,所以它的長度為 13 且 month_abbr[0] 是空字符串。

calendar.MONDAY?
calendar.TUESDAY?
calendar.WEDNESDAY?
calendar.THURSDAY?
calendar.FRIDAY?
calendar.SATURDAY?
calendar.SUNDAY?

Aliases for day numbers, where MONDAY is 0 and SUNDAY is 6.

參見

模塊 datetime

為日期和時間提供與 time 模塊相似功能的面向?qū)ο蠼涌凇?/p>

模塊 time

底層時間相關(guān)函數(shù)。