?? monthcalendar.cs
字號:
using System;
using System.Collections;
using System.Text;
namespace Org.InteliIM.Activities.Schedule
{
/// <summary>
/// 帶農歷的日歷
/// </summary>
public class MonthCalendar
{
#region 靜態成員
private static int[] lunarInfo=new int[]{
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
0x14b63};
private static int[] solarMonth=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
private static string[] Animals = new string[]{"鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"};
private static string[] solarTerm = new string[]{"小寒","大寒","立春","雨水","驚蟄","春分","清明","谷雨","立夏","小滿","芒種","夏至","小暑","大暑","立秋","處暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"};
private static int[] solarTermInfo = new int[]{0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758};
private static string[] nStr1 = new string[]{"日","一","二","三","四","五","六","七","八","九","十"};
private static string[] nStr2 = new string[]{"初","十","廿","卅","□"};
/// <summary>
/// 國歷節日
/// </summary>
private static Festival[] solarFestivals = new Festival[]{
new Festival(1, 1, "元旦節", true),
new Festival(2, 2, "世界濕地日"),
new Festival(2, 10, "國際氣象節"),
new Festival(2, 14, "情人節"),
new Festival(3, 1, "國際海豹日"),
new Festival(3, 3, "全國愛耳日"),
new Festival(3, 5, "學雷鋒紀念日"),
new Festival(3, 8, "婦女節"),
new Festival(3, 12, "植樹節 孫中山逝世紀念日"),
new Festival(3, 14, "國際警察日"),
new Festival(3, 15, "消費者權益日"),
new Festival(3, 17, "中國國醫節 國際航海日"),
new Festival(3, 21, "世界森林日 消除種族歧視國際日 世界兒歌日"),
new Festival(3, 22, "世界水日"),
new Festival(3, 23, "世界氣象日"),
new Festival(3, 24, "世界防治結核病日"),
new Festival(3, 25, "全國中小學生安全教育日"),
new Festival(3, 30, "巴勒斯坦國土日"),
new Festival(4, 1, "愚人節 全國愛國衛生運動月(四月) 稅收宣傳月(四月)"),
new Festival(4,7,"世界衛生日"),
new Festival(4,22,"世界地球日"),
new Festival(4,23,"世界圖書和版權日"),
new Festival(4,24,"亞非新聞工作者日"),
new Festival(5,1,"勞動節", true),
new Festival(5,2,"勞動節假日",true),
new Festival(5,3,"勞動節假日", true),
new Festival(5,4,"青年節"),
new Festival(5,5,"碘缺乏病防治日"),
new Festival(5,8,"世界紅十字日"),
new Festival(5,12,"國際護士節"),
new Festival(5,15,"國際家庭日"),
new Festival(5,17,"國際電信日"),
new Festival(5,18,"國際博物館日"),
new Festival(5,20,"全國學生營養日"),
new Festival(5,23,"國際牛奶日"),
new Festival(5,31,"世界無煙日"),
new Festival(6, 1, "國際兒童節"),
new Festival(6, 5, "世界環境保護日"),
new Festival(6, 6, "全國愛眼日"),
new Festival(6, 17, "防治荒漠化和干旱日"),
new Festival(6, 23, "國際奧林匹克日"),
new Festival(6, 25, "全國土地日"),
new Festival(6, 26, "國際禁毒日"),
new Festival(7, 1, "香港回歸紀念日 中共誕辰 世界建筑日"),
new Festival(7, 2, "國際體育記者日"),
new Festival(7, 7, "抗日戰爭紀念日"),
new Festival(7, 11, "世界人口日"),
new Festival(7, 30, "非洲婦女日"),
new Festival(8, 1, "建軍節"),
new Festival(8, 8, "中國男子節(爸爸節)"),
new Festival(8, 15, "抗日戰爭勝利紀念"),
new Festival(9, 8, "國際掃盲日 國際新聞工作者日"),
new Festival(9, 9, "毛澤東逝世紀念"),
new Festival(9, 10, "中國教師節"),
new Festival(9, 14, "世界清潔地球日"),
new Festival(9, 16, "國際臭氧層保護日"),
new Festival(9, 18, "九·一八事變紀念日"),
new Festival(9, 20, "國際愛牙日"),
new Festival(9, 27, "世界旅游日"),
new Festival(9, 28, "孔子誕辰"),
new Festival(10, 1, "國慶節 世界音樂日 國際老人節", true),
new Festival(10, 2, "國慶節假日 國際和平與民主自由斗爭日", true),
new Festival(10, 3, "國慶節假日", true),
new Festival(10, 4, "世界動物日"),
new Festival(10, 6, "老人節"),
new Festival(10, 8, "全國高血壓日 世界視覺日"),
new Festival(10, 9, "世界郵政日 萬國郵聯日"),
new Festival(10, 10, "辛亥革命紀念日 世界精神衛生日"),
new Festival(10, 13, "世界保健日 國際教師節"),
new Festival(10, 14, "世界標準日"),
new Festival(10, 15, "國際盲人節(白手杖節)"),
new Festival(10, 16, "世界糧食日"),
new Festival(10, 17, "世界消除貧困日"),
new Festival(10, 22, "世界傳統醫藥日"),
new Festival(10, 24, "聯合國日"),
new Festival(10, 31, "世界勤儉日"),
new Festival(11, 7, "十月社會主義革命紀念日"),
new Festival(11, 8, "中國記者日"),
new Festival(11, 9, "全國消防安全宣傳教育日"),
new Festival(11, 10, "世界青年節"),
new Festival(11, 11, "國際科學與和平周(本日所屬的一周)"),
new Festival(11, 12, "孫中山誕辰紀念日"),
new Festival(11, 14, "世界糖尿病日"),
new Festival(11, 17, "國際大學生節 世界學生節"),
new Festival(11, 20, "彝族年", true),
new Festival(11, 21, "彝族年 世界問候日 世界電視日", true),
new Festival(11, 22, "彝族年", true),
new Festival(11, 29, "國際聲援巴勒斯坦人民國際日"),
new Festival(12, 1, "世界艾滋病日"),
new Festival(12, 3, "世界殘疾人日"),
new Festival(12, 5, "國際經濟和社會發展志愿人員日"),
new Festival(12, 8, "國際兒童電視日"),
new Festival(12, 9, "世界足球日"),
new Festival(12, 10, "世界人權日"),
new Festival(12, 12, "西安事變紀念日"),
new Festival(12, 13, "南京大屠殺(1937年)紀念日!緊記血淚史!"),
new Festival(12, 20, "澳門回歸紀念"),
new Festival(12, 21, "國際籃球日"),
new Festival(12, 24, "平安夜"),
new Festival(12, 25, "圣誕節"),
new Festival(12, 26, "毛澤東誕辰紀念")};
/// <summary>
/// 農歷節日
/// </summary>
private static Festival[] lunarFestivals = new Festival[]{
new Festival(1, 1, "春節", true),
new Festival(1, 2, "春節", true),
new Festival(1, 3, "春節", true),
new Festival(1, 15, "元宵節"),
new Festival(5, 5, "端午節"),
new Festival(6, 24, "火把節", true),
new Festival(6, 25, "火把節", true),
new Festival(6, 26, "火把節", true),
new Festival(7, 7, "七夕情人節"),
new Festival(7, 15, "中元節"),
new Festival(8, 15, "中秋節"),
new Festival(9, 9, "重陽節"),
new Festival(12, 8, "臘八節"),
new Festival(12, 24, "小年"),
new Festival(1, 0, "除夕")};
/// <summary>
/// 某月的第幾個星期幾
/// </summary>
private static WeekDayFestival[] weekDayFestivals = new WeekDayFestival[]{
new WeekDayFestival(1, 5, 0, "世界麻風日"), //一月的最后一個星期日(月倒數第一個星期日)
new WeekDayFestival(5, 2,0, "國際母親節"),
new WeekDayFestival(5, 3, 0, "全國助殘日"),
new WeekDayFestival(6, 3, 0, "父親節"),
new WeekDayFestival(7, 3, 0, "被奴役國家周"),
new WeekDayFestival(9, 3, 2, "國際和平日"),
new WeekDayFestival(9, 4, 0, "國際聾人節 世界兒童日"),
new WeekDayFestival(9, 5, 0, "世界海事日"),
new WeekDayFestival(10, 1, 1, "國際住房日"),
new WeekDayFestival(10, 1, 3, "國際減輕自然災害日(減災日)"),
new WeekDayFestival(11, 4, 4, "感恩節")};
/// <summary>
/// 返回農歷 y 年的總天數
/// </summary>
/// <param name="lunarYear"></param>
/// <returns></returns>
public static int GetLunarDays(int lunarYear)
{
int i, sum = 348;
for(i=0x8000; i>0x8; i>>=1) sum += ((lunarInfo[lunarYear-1900] & i) != 0)? 1: 0;
return(sum+GetLeapDays(lunarYear));
}
/// <summary>
/// 返回農歷 y 年閏月的天數
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static int GetLeapDays(int year)
{
if(GetLeapMonth(year) != 0) return(((lunarInfo[year-1900] & 0x10000) != 0)? 30: 29);
else return(0);
}
/// <summary>
/// 返回農歷 y年閏哪個月 1-12 , 沒閏返回 0
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static int GetLeapMonth(int year)
{
return(lunarInfo[year-1900] & 0xf);
}
/// <summary>
/// 返回公歷某年某月的天數
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <returns>該月的天數</returns>
public static int GetSolarDays(int year,int month)
{
if(month==2)
return(((year%4 == 0) && (year%100 != 0) || (year%400 == 0))? 29: 28);
else
return(solarMonth[month - 1]);
}
/// <summary>
/// 返回農歷 y年m月的總天數
/// </summary>
/// <param name="lunarYear"></param>
/// <param name="lunarMonth"></param>
/// <returns></returns>
public static int GetLunarDays(int lunarYear, int lunarMonth)
{
return( ((lunarInfo[lunarYear-1900] & (0x10000>>lunarMonth)) != 0)? 30: 29 );
}
/// <summary>
/// 返回公歷日期對應的農歷日期
/// </summary>
/// <param name="solarDate">公歷日期</param>
/// <returns>農歷日期</returns>
public static LunarDate GetLunarDate(DateTime solarDate)
{
LunarDate lunarDate = new LunarDate();
int i, leap=0, temp=0;
int offset = solarDate.Subtract(new DateTime(1900, 1, 31)).Days;
for(i=1900; i<2050 && offset>0; i++) { temp=GetLunarDays(i); offset-=temp; }
if(offset<0) { offset+=temp; i--; }
lunarDate.Year = i;
leap = GetLeapMonth(i); //閏哪個月
lunarDate.IsLeap = false;
for(i=1; i<13 && offset>0; i++)
{
//閏月
if(leap>0 && i==(leap+1) && lunarDate.IsLeap==false)
{ --i; lunarDate.IsLeap = true; temp = GetLeapDays(lunarDate.Year); }
else
{ temp = GetLunarDays(lunarDate.Year, i); }
//解除閏月
if(lunarDate.IsLeap==true && i==(leap+1)) lunarDate.IsLeap = false;
offset -= temp;
}
if(offset==0 && leap>0 && i==leap+1)
if(lunarDate.IsLeap)
{ lunarDate.IsLeap = false; }
else
{ lunarDate.IsLeap = true; --i; }
if(offset<0){ offset += temp; --i; }
lunarDate.Month = i;
lunarDate.Day = offset + 1;
return lunarDate;
}
/// <summary>
/// 返回某年的第n個節氣為幾日(從0小寒起算)
/// </summary>
/// <param name="year"></param>
/// <param name="no"></param>
/// <returns></returns>
public static int GetSolarTerm(int year,int no)
{
DateTime originDate = new DateTime(1900, 1, 6, 2, 5, 0);
DateTime offDate = originDate.AddMilliseconds(31556925974.7*(year-1900) + (double)MonthCalendar.solarTermInfo[no]*60000);
return(offDate.Day);
}
/// <summary>
/// 返回某年的生肖
/// </summary>
/// <param name="solarYear"></param>
/// <returns></returns>
public static string GetAnimal(int solarYear)
{
return MonthCalendar.Animals[(solarYear-4)%12];
}
/// <summary>
/// 獲取農歷日期的中文名稱
/// </summary>
/// <param name="lunarDay"></param>
/// <returns></returns>
public static string GetLunarDayName(int lunarDay)
{
string s = "";
switch (lunarDay)
{
case 10:
s = "初十";
break;
case 20:
s = "二十";
break;
case 30:
s = "三十";
break;
default :
s = nStr2[(int) Math.Floor(lunarDay/10)] + nStr1[lunarDay%10];
break;
}
return(s);
}
#endregion
/// <summary>
/// 節日
/// </summary>
public class Festival
{
private int month = 1;
/// <summary>
/// 月
/// </summary>
public int Month
{
get
{
return this.month;
}
set
{
this.month = value;
}
}
private int day = 1;
/// <summary>
/// 日
/// </summary>
public int Day
{
get
{
return this.day;
}
set
{
this.day = value;
}
}
private string name = null;
/// <summary>
/// 節日名稱
/// </summary>
public string Name
{
get
{
if(this.name == null)
this.name = "";
return this.name;
}
set
{
this.name = value;
}
}
private bool vocation = false;
/// <summary>
/// 是否放假
/// </summary>
public bool Vocation
{
get
{
return this.vocation;
}
set
{
this.vocation = value;
}
}
/// <summary>
/// 構造函數
/// </summary>
/// <param name="month"></param>
/// <param name="day"></param>
/// <param name="name"></param>
public Festival(int month, int day, string name)
:this(month, day, name, false)
{
}
/// <summary>
/// 構造函數
/// </summary>
/// <param name="month"></param>
/// <param name="day"></param>
/// <param name="name"></param>
/// <param name="vocation"></param>
public Festival(int month, int day, string name, bool vocation)
{
this.Month = month;
this.Day = day;
this.Name = name;
this.Vocation = vocation;
}
}
/// <summary>
/// 基于某月的第幾個星期幾的節日
/// </summary>
public class WeekDayFestival
{
private int month = 1;
/// <summary>
/// 月
/// </summary>
public int Month
{
get
{
return this.month;
}
set
{
this.month = value;
}
}
private int week = 1;
/// <summary>
/// 周
/// </summary>
public int Week
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -