====== Python 日历相关的函数 ====== WEEKDAYS = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; #WEEKDAYS = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; MONTH_NAME = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] def get_month_days(year, month): global MONTH_DAYS; if(month==2): if(((year%4 == 0) and (year%100 != 0)) or (year%400 == 0)): return 29 else: return 28 else: return(MONTH_DAYS[month]); def get_syear_days(syear): if(((syear%4 == 0) and (syear%100 != 0)) or (syear%400 == 0)): return 366 else: return 365 def get_week_of_syear(syear, smonth, sday): weekday = get_weekday(syear, 1, 1) first_week_days = 7 - weekday day_of_syear = get_day_of_syear(syear, smonth, sday) week_of_syear = (day_of_syear - first_week_days) / 7 if ((day_of_syear - first_week_days) % 7): week_of_syear += 1 week_of_syear += 1 # the first week. return week_of_syear def get_day_of_syear(syear, smonth, sday): """ get given day's number of sun year """ days = 0 for i in range(1, smonth): days += get_month_days(syear, i) days += sday return days def get_weekday(month, day, year): if(month == 1 or month == 2): month += 12; year -= 1; # w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 y = year % 100; c = int(math.floor(year/100)); m = month; d = day; #echo "y - c - m - d\n"; w = int (y+ math.floor(y/4) + math.floor(c/4) -2*c+ math.floor(26*(m+1)/10) +d-1) #echo w . "\n"; w = w % 7; if (w < 0): w += 7; return w; #weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] def get_constellation(month, day): dates = (21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22) constellations = [ "Capricorn(the Goat)", "Aquarius(the Water Carrier)", "Pisces(the Fishes)", "Aries(the Ram)", "Taurus(the Bull)", "Gemini(the Twins)", "Cancer(the Crab)", "Leo(the Lion)", "Virgo(the Virgin)", "Libra(the Scales)", "Scorpio(the Scorpion)", "Sagittarius(the Archer)", "Capricorn(the Goat)" ] if day < dates[month-1]: return constellations[month-1] else: return constellations[month] def get_month_days(year, month): MONTH_DAYS = [0,31,28,31,30,31,30,31,31,30,31,30,31]; if(month==2): if(((year%4 == 0) and (year%100 != 0)) or (year%400 == 0)): return 29 else: return 28 else: return(MONTH_DAYS[month]); def gen_year_calendar(year): year_table = "" year_table += ""; global MONTH_NAME; month_per_line = 3 for i in range(1,13): month_str = MONTH_NAME[i] if(i%month_per_line == 1): year_table += ""; year_table += '"; if(i%month_per_line == 0): year_table += ""; year_table += "
' % (year, month_str, month_str, year); year_table += "

%s %d

"; year_table += gen_month_calendar(year, i); year_table += "
"; return year_table; def gen_month_calendar(year, month): global WEEKDAYS; weekday = get_weekday(month, 1, year); table_str = ""; for w in WEEKDAYS: table_str += "" % w[:3] table_str += ""; table_str += ""; for j in range(weekday): table_str += ""; month_days = get_month_days(year, month); tr_num = 0 for day in range(1, month_days+1): j = day + weekday - 1 if(j%7 == 0 and j!=0): table_str += ""; tr_num += 1 table_str += '' % \ (WEEKDAYS[j%7][:3], year, MONTH_NAME[month], day, day) if(j%7 == 6): table_str += ""; if table_str[-5:] != "": table_str += ""; while tr_num < 5: table_str += '' tr_num += 1 table_str += "
%s
 
%d
 
"; return table_str; def gen_day_calendar(year, month, day): day_html = "" days_of_year = get_day_of_syear(year, month, day) week_of_syear = get_week_of_syear(year, month, day) days_to_end = get_syear_days(year) - days_of_year day_html += '' day_html += '' % ( WEEKDAYS[get_weekday(month, day, year)] ) day_html += '' % ( days_of_year) day_html += '' % ( week_of_syear) day_html += '' % (days_to_end) day_html += '' % (get_constellation(month, day)) day_html += '' % (MONTH_NAME[month], day, MONTH_NAME[month], day) day_html += '
Days of Week:%s
Days of Year:%d
Week of Year:%d
Days to End of Year:%d
Constellation:%s
Today In History:%s %d History
'; return day_html