用户工具

站点工具


python-scripts:date-calendar

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 += "<table class=\"year\">";
  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 += "<tr>";
    year_table += '<td><table><tr><td class="month_name"><h2><a href=\"%d-%s.html\">%s %d</a></h2></tr>' % (year, month_str, month_str, year);
    year_table += "<tr><td>";
    year_table += gen_month_calendar(year, i);
    year_table += "</td></tr></table></td>";
    if(i%month_per_line == 0): year_table += "</tr>";
  year_table += "</table>";
  return year_table;
 
def gen_month_calendar(year, month):
  global WEEKDAYS;
  weekday = get_weekday(month, 1, year);
  table_str = "<table class=\"month\"><tr>";
  for w in WEEKDAYS:
    table_str += "<th>%s</th>" % w[:3]
  table_str += "</tr>";
  table_str += "<tr>";
  for j in range(weekday):
    table_str += "<td>&nbsp;</td>";
  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>";
      tr_num += 1
    table_str += '<td class="%s"><a href="%d-%s-%d.html">%d</a></td>' % \
        (WEEKDAYS[j%7][:3], year, MONTH_NAME[month], day, day)
    if(j%7 == 6):
      table_str += "</tr>";
  if table_str[-5:] != "</tr>":
    table_str += "</tr>";
  while tr_num < 5:
    table_str += '<tr><td colspan="7">&nbsp</td></tr>'
    tr_num += 1
  table_str += "</table>";
  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 += '<table class="day">'
  day_html += '<tr><td>Days of Week:</td><td>%s</td></tr>' % ( WEEKDAYS[get_weekday(month, day, year)] )
  day_html += '<tr><td>Days of Year:</td><td>%d</td></tr>' % ( days_of_year)
  day_html += '<tr><td>Week of Year:</td><td>%d</td></tr>' % ( week_of_syear)
  day_html += '<tr><td>Days to End of Year:</td><td>%d</td></tr>' % (days_to_end)
  day_html += '<tr><td>Constellation:</td><td>%s</td></tr>' % (get_constellation(month, day))
  day_html += '<tr><td>Today In History:</td><td><a target="_blank" href="http://todayinhistory.pythonclub.org/%s-%d.html">%s %d History</a></td></tr>' % (MONTH_NAME[month], day, MONTH_NAME[month], day)
  day_html += '</table>';
  return day_html
python-scripts/date-calendar.txt · 最后更改: 2011/03/24 05:52 (外部编辑)