13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联Python教程:calendar—-日历相关函数

calendar —- 日历相关函数

源代码: Lib/calendar.py


这个模块让你可以输出像 Unix cal 那样的日历,它还提供了其它与日历相关的实用函数。 默认情况下,这些日历把星期一当作一周的第一天,星期天为一周的最后一天(按照欧洲惯例)。 可以使用 setfirstweekday() 方法设置一周的第一天为星期天 (6) 或者其它任意一天。 使用整数作为指定日期的参数。 更多相关的函数,参见 datetime 和 time 模块。

在这个模块中定义的函数和类都基于一个理想化的日历,现行公历向过去和未来两个方向无限扩展。这与 Dershowitz 和 Reingold 的书 “历法计算” 中所有计算的基本日历 — “proleptic Gregorian” 日历的定义相符合。 ISO 8601标准还规定了 0 和 负数年份。0年指公元前1年, -1年指公元前2年,依此类推。

class calendar.Calendar(firstweekday=0)

创建一个 Calendar 对象。 firstweekday 是一个用来指定每星期第一天的整数。 MONDAY 是 0 (默认值), SUNDAY 是 6

Calendar 对象提供了一些可被用于准备日历数据格式化的方法。 这个类本身不执行任何格式化操作。 这部分任务应由子类来完成。

Calendar 类的实例有下列方法:

class calendar.TextCalendar(firstweekday=0)

可以使用这个类生成纯文本日历。

TextCalendar 实例有以下方法:

class calendar.HTMLCalendar(firstweekday=0)

可以使用这个类生成 HTML 日历。

HTMLCalendar 实例有以下方法:

HTMLCalendar 有以下属性,你可以重载它们来自定义应用日历的样式。

需要注意的是,尽管上面命名的样式类都是单独出现的(如: cssclass_month cssclass_noday), 但我们可以使用空格将样式类列表中的多个元素分隔开,例如:

 
 
 
 
  1. "text-bold text-red"

下面是一个如何自定义 HTMLCalendar 的示例

 
 
 
 
  1. class CustomHTMLCal(calendar.HTMLCalendar):
  2. cssclasses = [style + " text-nowrap" for style in
  3. calendar.HTMLCalendar.cssclasses]
  4. cssclass_month_head = "text-center month-head"
  5. cssclass_month = "text-center month"
  6. 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)

设置每一周的开始(0 表示星期一,6 表示星期天)。calendar还提供了 MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 和 SUNDAY 几个常量方便使用。例如,设置每周的第一天为星期天

 
 
 
 
  1. import calendar
  2. calendar.setfirstweekday(calendar.SUNDAY)

calendar.firstweekday()

返回当前设置的每星期的第一天的数值。

calendar.isleap(year)

如果 year 是闰年则返回 True ,否则返回 False。

calendar.leapdays(y1, y2)

返回在范围 y1y2 (不包括 y2)之间的闰年的年数,其中 y1y2 是年份。

此函数适用于跨越一个世纪变化的范围。

calendar.weekday(year, month, day)

返回某年( 1970 — …),某月( 112 ),某日( 131 )是星期几( 0 是星期一)。

calendar.weekheader(n)

返回一个包含星期几的缩写名的头。 n 指定星期几缩写的字符宽度。

calendar.monthrange(year, month)

返回指定 年份 的指定 月份 的第一天是星期几和这个月的天数。

calendar.monthcalendar(year, month)

返回表示一个月的日历的矩阵。 每一行代表一周;此月份外的日子由零表示。 每周从周一开始,除非使用 setfirstweekday() 改变设置。

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)

一个不相关但很好用的函数,它接受一个时间元组例如 time 模块中的 gmtime() 函数的返回并返回相应的 Unix 时间戳值,假定 1970 年开始计数, POSIX 编码。实际上, time.gmtime() 和 timegm() 是彼此相反的。

calendar 模块导出以下数据属性:

calendar.day_name

在当前语言环境下表示星期几的数组。

calendar.day_abbr

在当前语言环境下表示星期几缩写的数组。

calendar.month_name

在当前语言环境下表示一年中月份的数组。这遵循一月的月号为 1 的通常惯例,所以它的长度为 13 且 month_name[0] 是空字符串。

calendar.month_abbr

在当前语言环境下表示月份简写的数组。这遵循一月的月号为 1 的通常惯例,所以它的长度为 13 且 month_abbr[0] 是空字符串。

calendar.MONDAY

calendar.TUESDAY

calendar.WEDNESDAY

calendar.THURSDAY

calendar.FRIDAY

calendar.SATURDAY

calendar.SUNDAY

星期内序号的别名,其中 MONDAY0SUNDAY6

参见

模块 datetime

为日期和时间提供与 time 模块相似功能的面向对象接口。

模块 time

底层时间相关函数。


文章名称:创新互联Python教程:calendar—-日历相关函数
网页URL:http://cdbrznjsb.com/article/djojjpe.html

其他资讯

让你的专属顾问为你服务