แนวคิดในการทำ
ในการทำโปรแกรมนี้จะสร้าง List มาเก็บข้อมูลสอง List เหมือนกับการสร้างโปรแกรมแสดงปฏิทิน วันและเดือนปัจจุบัน และทำการหาวันที่ของเดือนปัจจุบันมาเก็บใน List เหมือนกับในโปรแกรมแสดงปฏิทิน วันและเดือนปัจจุบัน แต่ในโปรแกรมนี้เมื่อเราหาวันที่ของเดือนปัจจุบันเสร็จแล้ว ก็ให้ทำการหาวันที่ของเดือนก่อนหน้า และเดือนหลังจากเดือนปัจจุบันเพิ่มแล้วนำเลขวันที่มาเก็บไว้ใน List เหมือนกับโปรแกรมแสดงปฏิทิน วันและเดือนปัจจุบัน เสร็จแล้วก็นำค่าใน List นั้นไปสร้างเป็นปฏิทินในไฟล์ html
import time
time=time.localtime()
print time
year=time[0]
month=time[1]
numDay=time[2]
day=time[6]
list_numDay=[]
list_numDay.append(numDay)
list_day=[]
list_day.append(day)
def finalNumDay(month,year):
final_numDay=31 if month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12 else 30
if year%4==0:
if month==2:
final_numDay=29
else:
if month==2:
final_numDay=28
return final_numDay
if numDay>1:
num_temp=numDay
day_temp=day
while (num_temp>1):
num_temp=num_temp-1
list_numDay.insert(0,num_temp)
day_temp=day_temp-1
if day_temp<0:
day_temp=6
list_day.insert(0,day_temp)
day_temp=day
for i in range(numDay+1,finalNumDay(month,year)+1):
list_numDay.append(i)
day_temp=day_temp+1
if day_temp>6:
day_temp=0
list_day.append(day_temp)
list_temp=[]
for i in range(1,month):
final_numDay=finalNumDay(i,year)
for j in range(1,final_numDay+1):
list_temp.append(j)
start=list_day[0]
while len(list_temp)>0:
list_numDay.insert(0,list_temp.pop())
start=start-1
if start<0:
start=6
list_day.insert(0,start)
end=list_day[len(list_day)-1]
for i in range(month+1,13):
final_numDay=finalNumDay(i,year)
for j in range(1,final_numDay+1):
list_numDay.append(j)
end=end+1
if end>6:
end=0
list_day.append(end)
def str_month(month):
if month==1:
str_month='January'
elif month==2:
str_month='February'
elif month==3:
str_month='March'
elif month==4:
str_month='April'
elif month==5:
str_month='May'
elif month==6:
str_month='June'
elif month==7:
str_month='July'
elif month==8:
str_month='August'
elif month==9:
str_month='September'
elif month==10:
str_month='October'
elif month==11:
str_month='November'
elif month==12:
str_month='December'
return str_month
str_table=''
count_month=0
count=0
for i in list_numDay:
if i==1:
count_month+=1
str_table=str_table+"</table><br><table border=1 align=\"center\">"
str_table=str_table+"<caption>"+str_month(count_month)+str(year)+"</caption>"+"""
<tr>
<th width="50">Monday</th>
<th width="50">Tuesday</th>
<th width="50">Wednesday</th>
<th width="50">Thursday</th>
<th width="50">Friday</th>
<th width="50">Saturday</th>
<th width="50" bgcolor="#FF0000">Sunday</th>
</tr>
<tr align="center">
"""
for j in range(list_day[count]):
str_table=str_table+"<td></td>"
if list_day[count]==0:
str_table=str_table+"</tr><tr align=\"center\">"
if list_numDay[count]==numDay and count_month==month:
str_table=str_table+"<td bgcolor=\"#00FF00\">"+str(list_numDay[count])+"</td>"
else:
str_table=str_table+"<td>"+str(list_numDay[count])+"</td>"
count+=1
str_table+='</tr>'
html="""
<html>
<head><title>Calendar"""+str(year)+"""</title></head>
<body>"""+str_table+"""
</body>
</html>
"""
f=open('work2_3.html','w')
f.write(html)
f.close()
print list_numDay
print list_day
ตัวอย่างเมื่อทำการ RUN
ไม่มีความคิดเห็น:
แสดงความคิดเห็น