|
如果你用过linux,你可能知道在linux下面的有一个强大的calendar功能,即日历
在python中,同样也有这样的一个强大的calendar
下面是我做的demo:
1 #python中的calendar
2
3 import calendar
4
5 #返回指定年的某月
6 def get_month(year, month):
7 return calendar.month(year, month)
8
9 #返回指定年的日历
10 def get_calendar(year):
11 return calendar.calendar(year)
12
13 #判断某一年是否为闰年,如果是,返回True,如果不是,则返回False
14 def is_leap(year):
15 return calendar.isleap(year)
16
17 #返回某个月的weekday的第一天和这个月的所有天数
18 def get_month_range(year, month):
19 return calendar.monthrange(year, month)
20
21 #返回某个月以每一周为元素的序列
22 def get_month_calendar(year, month):
23 return calendar.monthcalendar(year, month)
24
25 def main():
26 year = 2013
27 month = 8
28 test_month = get_month(year, month)
29 print(test_month)
30 print('#' * 50)
31 #print(get_calendar(year))
32 print('{0}这一年是否为闰年?:{1}'.format(year, is_leap(year)))
33 print(get_month_range(year, month))
34 print(get_month_calendar(year, month))
35
36 if __name__ == '__main__':
37 main()
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
August 2013
Mo Tu We Th Fr Sa Su
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
##################################################
2013这一年是否为闰年?:False
(3, 31)
[[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, 0]]
>>>
再附上calendar.py的源码:
1 """Calendar printing functions
2
3 Note when comparing these calendars to the ones printed by cal(1): By
4 default, these calendars have Monday as the first day of the week, and
5 Sunday as the last (the European convention). Use setfirstweekday() to
6 set the first day of the week (0=Monday, 6=Sunday)."""
7
8 import sys
9 import datetime
10 import locale as _locale
11
12 __all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
13 "firstweekday", "isleap", "leapdays", "weekday", "monthrange",
14 "monthcalendar", "prmonth", "month", "prcal", "calendar",
15 "timegm", "month_name", "month_abbr", "day_name", "day_abbr"]
16
17 # Exception raised for bad input (with string parameter for details)
18 error = ValueError
19
20 # Exceptions raised for bad input
21 class IllegalMonthError(ValueError):
22 def __init__(self, month):
23 self.month = month
24 def __str__(self):
25 return "bad month number %r; must be 1-12" % self.month
26
27
28 class IllegalWeekdayError(ValueError):
29 def __init__(self, weekday):
30 self.weekday = weekday
31 def __str__(self):
32 return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
33
34
35 # Constants for months referenced later
36 January = 1
37 February = 2
38
39 # Number of days per month (except for February in leap years)
40 mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
41
42 # This module used to have hard-coded lists of day and month names, as
43 # English strings. The classes following emulate a read-only version of
44 # that, but supply localized names. Note that the values are computed
45 # fresh on each call, in case the user changes locale between calls.
46
47 class _localized_month:
48
49 _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
50 _months.insert(0, lambda x: "")
51
52 def __init__(self, format):
53 self.format = format
54
55 def __getitem__(self, i):
56 funcs = self._months
57 if isinstance(i, slice):
58 return [f(self.format) for f in funcs]
59 else:
60 return funcs(self.format)
61
62 def __len__(self):
63 return 13
64
65
66 class _localized_day:
67
68 # January 1, 2001, was a Monday.
69 _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)]
70
71 def __init__(self, format):
72 self.format = format
73
74 def __getitem__(self, i):
75 funcs = self._days
76 if isinstance(i, slice):
77 return [f(self.format) for f in funcs]
78 else:
79 return funcs(self.format)
80
81 def __len__(self):
82 return 7
83
84
85 # Full and abbreviated names of weekdays
86 day_name = _localized_day('%A')
87 day_abbr = _localized_day('%a')
88
89 # Full and abbreviated names of months (1-based arrays!!!)
90 month_name = _localized_month('%B')
91 month_abbr = _localized_month('%b')
92
93 # Constants for weekdays
94 (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
95
96
97 def isleap(year):
98 """Return True for leap years, False for non-leap years."""
99 return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
100
101
102 def leapdays(y1, y2):
103 """Return number of leap years in range [y1, y2).
104 Assume y1 |
|