万年历之可以看到亿万年后的日历,python实现

编程入门 行业动态 更新时间:2024-10-17 00:17:59

万年历之<a href=https://www.elefans.com/category/jswz/34/1769433.html style=可以看到亿万年后的日历,python实现"/>

万年历之可以看到亿万年后的日历,python实现

1 日期的计算是基于已知条件的,即1800年1月1日是星期三。

2 判断闰年,平年的方法:顺口溜是四年一闰,百年不闰,四百年再闰。

3 至于如何能快速查出亿万年后的日历,如果单单用累加法找,可能真的要打到地老天荒了。在本文中,笔者认为其实在2中,就已经暗示,日历的计算方法,其实是有周期性质的,也就是说其周期为400,那么这样的话,我们其实只须看出400年的具体日历,那么就可以推出亿万年后或者亿万年前的日历。

好了,说了这么多,下面看代码吧,权当python学习过程的一些思考吧!

import time
def print_month(year, month):print()print_month_title(year, month)print_month_body(year%400 + 2000, month)print('\n')def print_month_title(year, month):print('          ', get_month_name(month), ' ', year)print('---------------------------------------')print('   日   一   二   三   四    五   六')def print_month_body(year, month):start_day = get_start_day(year, month)number_of_days_in_month = get_number_of_days_in_month(year, month)# print('\033[35;1m %s \033[m' % (30))today = list(time.localtime(time.time()))i = 0for i in range(start_day):print('     ', end = '')for j in range(1, number_of_days_in_month+1):#这个地方的作用就是如果你查看的是当年的日历,那么这个判断语句就会#把当天的日历用紫红色标出if today[0] == year and today[1] == month and today[2] == j:print('\033[35;1m   %s \033[m' % (j), end = "")else:print(format(j, '4d'), end = ' ')if(j + start_day) % 7 ==0:print()def get_month_name(month):n_month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']month_name = n_month[month-1]return month_name
def get_start_day(year, month):START_DAY_FOR_JAN_1_1800 = 3total_number_of_days = get_total_number_of_days(year, month)return((total_number_of_days + START_DAY_FOR_JAN_1_1800) % 7)
def get_number_of_days_in_month(year, month):if month in [1,3,5,7,8,10,12]:return 31elif month in [4,6,9,11]:return 30elif is_leapyear(year):return 29else:return 28
def get_total_number_of_days(year, month):total = 0for i in range(1800, year):if is_leapyear(i):total += 366else:total += 365for j in range(1, month):total += get_number_of_days_in_month(year, j)return total
def is_leapyear(year):if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):return Trueelse:return False
def main():year = eval(input('Which year would you want to see? Enter full year (e.g., 2001): '))for month_n in range(1,12+1):# year = year%400 + 2000print_month(year, month_n)
if __name__ == '__main__':main()

下面验证一下程序的正确性,先看看2018年的吧,那么运行程序,输入2018,得到部分结果如下左,右为真实的日历:

                                            

5.1号为周二,其31天,吻合,

下面找了2800年2月份的日历


下面用程序运行,计算出2800年2月份的日历,以作对比


通过对比可以看出,我们的程序得出的结果与真正的日历是吻合的,验证了程序的正确性。

更多推荐

万年历之可以看到亿万年后的日历,python实现

本文发布于:2024-03-14 23:52:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1737639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以看到   万年历   年后   日历   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!