一、功能描述:
二、代码:
from sys import *
Weeks = ['Sun', "Mon", 'Tus', 'Wed', 'Thu', 'Fri', 'Sat']
try :
date_in = argv[1]
except:
print('\n ERROR! you forgot to enter the date you want to view\n')
date_in = input("Please input a date!\n > ")
# print(date_in)
year, month, day = date_in.split('/')
try :
year, month, day = int(year), int(month), int(day)
except :
year = int(year)
month = int(month)
day = 0
# print(year, month, day)
if month < 1 or month > 12:
print('\n ERROR! the entered MONTH is invalid\n')
exit()
if year > 2000:
d = (year-1-2000)//4-(year-1-2000)//100+(year-1-2000)//400+1
elif year == 2000:
dy = 0
m2 = 1
else:
d = (year-2000)//4-(year-2000)//100+(year-2000)//400
dy=(year-2000)+d
if (year%4==0 and year%100 != 0) or (year%100==0 and year%400==0):
m2 = 1
else:
m2 = 0
if month == 1 :
dm = 0
month = 31
elif month == 2 :
dm = 3
if d == 1 :
month = 29
else:
month = 28
elif month == 3 :
dm = 3 + m2
month = 31
elif month == 4 :
dm = 6 + m2
month = 30
elif month == 5 :
dm = 1 + m2
month = 31
elif month == 6 :
dm = 4 + m2
month = 30
elif month == 7 :
dm = 6 + m2
month = 31
elif month == 8 :
dm = 2 + m2
month = 31
elif month == 9 :
dm = 5 + m2
month = 30
elif month == 10 :
dm = m2
month = 31
elif month == 11 :
dm = 3 + m2
month = 30
elif month == 12 :
dm = 5 + m2
month = 31
else:
print("What is it?")
if day < 0 or day > month :
print("\n Error! The entered Day is invalid\n")
exit()
week = (dy + dm + day - 1 + 6) % 7
if week < 0 :
week += 7
if day > 0:
if week == 0:
WEEK = Weeks[0]
elif week == 1:
WEEK = Weeks[1]
elif week == 2:
WEEK = Weeks[2]
elif week == 3:
WEEK = Weeks[3]
elif week == 4:
WEEK = Weeks[4]
elif week ==5:
WEEK = Weeks[5]
elif week == 6:
WEEK = Weeks[6]
else:
pass
print("\n this day is ", WEEK, "(",int(week),")"+ "\n OK!\n")
else:
week = (week+1) % 7
print("\n the calender of this month as following")
print("\n ***************************************")
print(" SUN MON TUR WEN THU FRI STA\n")
for i in range(int(week)):
print(" ", end = ' ')
for i in range(1, month+1):
print(" %2d "%i, end = ' ')
if (week+1) % 7 == 0 and i != month :
print("\n")
week = week + 1
print("\n****************************************\n")
print("\n OK! \n")
三、代码拆解:
参数获取:
日期判断:
week = (dy + dm + day - 1 + 6) % 7
dy=(year-2000)+d
if year > 2000:
d = (year-1-2000)//4-(year-1-2000)//100+(year-1-2000)//400+1
elif year == 2000:
dy = 0
m2 = 1
else:
d = (year-2000)//4-(year-2000)//100+(year-2000)//400
代码备注: