from datetime import date, timedelta
def getLastSunday(theDate):
theWeekday = theDate.isoweekday()
if theWeekday == 7:
theWeekday = 0
return theDate - timedelta(days=theWeekday)
def getNextSaturday(theDate):
theWeekday = theDate.isoweekday()
if theWeekday == 7:
theWeekday = 0
return theDate + timedelta(days=6 - theWeekday)
def genWeekDays(bOW):
return [str((bOW + timedelta(days=i)).day) for i in range(0, 7)]
def genMonthDic(year, month):
bOM = date(year, month, 1)
if month == 12:
eOM = date(year+1, 1, 1) - timedelta(days=1)
else:
eOM = date(year, month+1, 1) - timedelta(days=1)
bOC = getLastSunday(bOM)
eOC = getNextSaturday(eOM)
return {
'year': year,
'month': month,
'days': [genWeekDays(bOC + timedelta(days=i)) for i in range(0, (eOC-bOC).days, 7)]
}
def monthDicToDisplay(md):
print("年份:", md['year'])
print("月份:", md['month'])
print("\t".join(['星期天','星期一','星期二','星期三','星期四','星期五','星期六']))
for i in md['days']:
print("\t".join(i))
print("---------------------------------------------------------------------------")
theYear = int(input("請輸入年份:"))
for i in range(1, 13):
monthDicToDisplay(genMonthDic(theYear, i))
ZnJvbSBkYXRldGltZSBpbXBvcnQgZGF0ZSwgdGltZWRlbHRhCgpkZWYgZ2V0TGFzdFN1bmRheSh0aGVEYXRlKToKICAgIHRoZVdlZWtkYXkgPSB0aGVEYXRlLmlzb3dlZWtkYXkoKQogICAgaWYgdGhlV2Vla2RheSA9PSA3OgogICAgICAgIHRoZVdlZWtkYXkgPSAwCiAgICByZXR1cm4gdGhlRGF0ZSAtIHRpbWVkZWx0YShkYXlzPXRoZVdlZWtkYXkpCgpkZWYgZ2V0TmV4dFNhdHVyZGF5KHRoZURhdGUpOgogICAgdGhlV2Vla2RheSA9IHRoZURhdGUuaXNvd2Vla2RheSgpCiAgICBpZiB0aGVXZWVrZGF5ID09IDc6CiAgICAgICAgdGhlV2Vla2RheSA9IDAKICAgIHJldHVybiB0aGVEYXRlICsgdGltZWRlbHRhKGRheXM9NiAtIHRoZVdlZWtkYXkpCgpkZWYgZ2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