python万年历_python万年历

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值