import datetime
class CalenderUtils:
@classmethod
def
get_day_count_of_month(cls, year, month):
if month in (1, 3, 5, 7, 8, 10, 12):
return
31
elif month in (4, 6, 9, 11):
return
30
elif cls.is_leap_year(year):
return
29
else:
return
28
@classmethod
def is_leap_year(cls,
year):
if (year % 4 == 0 and year % 100 != 0) or year %
400 == 0:
return
True
else:
return
False
@classmethod
def
get_total_days_from_1900_01_01(cls, year, month):
days = 0
for y in range(1900, year):
days +=
366 if cls.is_leap_year(y) else 365
for m in range(1, month):
days +=
cls.get_day_count_of_month(year, m)
return days
@classmethod
def
is_first_day_what_day(cls, year, month):
return (1 +
cls.get_total_days_from_1900_01_01(year, month)) % 7
@classmethod
def get_info(cls, year,
month):
first_day =
CalenderUtils.is_first_day_what_day(year, month) # 第一天是星期几
day_count =
CalenderUtils.get_day_count_of_month(year, month)
return first_day, day_count
@classmethod
def print_view(cls,
year, month):
def _tr(s):
d =
{}
for a, b
in zip('1234567890', '1234567890'):
d[a] = b
return
''.join([d[e] for e in s]) if len(s) == 2 else ' ' + d[s]
print()
print()
print('*** {}年{}月 ***'.format(year,
month))
print(' 日 一 二 三 四 五 六')
first_day, day_count = cls.get_info(year,
month)
print(' ' * first_day, end = '')
count = first_day + 1
i = 0
while i < day_count:
print('{}
'.format(_tr(str(i + 1))), end = '')
if count %
7 == 0:
print()
i +=
1
count +=
1
class Calender:
def
__init__(self):
now = datetime.datetime.now()
self.__year = now.year
self.__month = now.month
def
print_view_of_current_month(self):
CalenderUtils.print_view(self.__year,
self.__month)
def
goto_next_month_and_print_view(self):
if self.__month == 12:
self.__year += 1
self.__month = 1
else:
self.__month += 1
self.print_view_of_current_month()
if __name__ == '__main__':
c = Calender()
c.print_view_of_current_month()
c.goto_next_month_and_print_view()
c.goto_next_month_and_print_view()