阳历和农历转化类(C#版)

来源:互联网 发布:软件项目规划方案 编辑:程序博客网 时间:2024/04/27 19:05

/*********************
 * *  Author: minsun@126.com
 * *  CreateTime: 2004-11-26 19:09:41
 *  *********************
 * * */

//本类根据CSDN上一些相关的文章中的算法改进而来,在此也对参考文章的作者
//表示感谢!
using System;

namespace DateConvert
{
 /// <summary>
 /// Summary description for Class3.
 /// </summary>
 ///

 ///<summary>
 //  若函式的返回值为 0 表示没有错误, 1 为输入之年份错误, 2 为输入之月份错误,
 //  3 为输入之日期错误.
 //  输入之西历年须在 1937 - 2031 间
 //  输入之农历年须在 1936 - 2030 间
 //  若须扩充, 则增加 LunarCal[]
 //
 // struct ConvDate
 //  {
 //    int Source;     
 //    int SolarYear;    输出或输入之西历年份
 //    int SolarMonth;  西历月
 //    int SolarDate;    西历日
 //    int LunarYear;    输出或输入之农历年份
 //    int LunarMonth;  农历月
 //    int LunarDate;    农历日
 //    int Weekday;      该日为星期几 ( 0==星期日, 1==星期一, ... )
 //    int Kan;          该日天干    ( 0==甲, 1==乙, ..., 9==癸 )
 //    int Chih;        该日地支    ( 0==子, 1==丑, ..., 11==亥 )
 //  };
 ///</summary>
 ///<author> minsun@126.com </author>
 ///<date> 2004-11-26 19:09:50 </date>
 ///<remarks>
 ///
 ///</remarks>
 ///******************************************************************
 ///使用说明
 ///DateTime handleDate = DateTime.Today;
 ///ConvDate convDate  =  new ConvDate( false , handleDate.Year , handleDate.Month ,handleDate.Day );
 //////true表示由阳历转化为农历;false表示用农历转化为阳历.
 ///int result = ( new ConvertDate()).CalConv( ref convDate);
 ///if ( result == 0 ) ///0 表示转化成功
 ///{
 /// handleDate = Convert.ToDateTime ( convDate.SolarWholeDate );
 ///}
 ///取convDate的各个属性,可以得到转化后的数据。
 ///*******************************************************************
    
 public class ConvertDate
 {
  public ConvertDate()
  {
   //
   // TODO: Add constructor logic here
   //
  }
  private const  int  FIRSTYEAR =  1936 ; /* The first year in LunarCal[] */
  
  private TagLunarCal[] LunarCal =new  TagLunarCal[]
  {
   new TagLunarCal( new int[17]{ 23,  3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }),  /* 1936 */
   new TagLunarCal( new int[17]{ 41,  0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 30,  7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 49,  0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 38,  0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }),  /* 1940 */
   new TagLunarCal( new int[17]{ 26,  6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 45,  0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 35,  0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 24,  4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 }),  /* 1944 */
   new TagLunarCal( new int[17]{ 43,  0, 0,  5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 32,  0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 21,  2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 40,  0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }),  /* 1948 */
   new TagLunarCal( new int[17]{ 28,  7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 47,  0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 36,  0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 26,  5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 }),  /* 1952 */
   new TagLunarCal( new int[17]{ 44,  0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 33,  0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 23,  3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 }),
   new TagLunarCal( new int[17]{ 42,  0, 6,  2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 }),  /* 1956 */
   new TagLunarCal( new int[17]{ 30,  8, 1,  8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 48,  0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 38,  0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 27,  6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 }),  /* 1960 */
   new TagLunarCal( new int[17]{ 45,  0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 35,  0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 24,  4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 43,  0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 }),  /* 1964 */
   new TagLunarCal( new int[17]{ 32,  0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 20,  3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 39,  0, 6,  0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 29,  7, 0,  5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 }),  /* 1968 */
   new TagLunarCal( new int[17]{ 47,  0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 36,  0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 26,  5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 45,  0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 }),  /* 1972 */
   new TagLunarCal( new int[17]{ 33,  0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 22,  4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 41,  0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 30,  8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 }),  /* 1976 */
   new TagLunarCal( new int[17]{ 48,  0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 }),
   new TagLunarCal( new int[17]{ 37,  0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 27,  6, 0,  3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 46,  0, 1,  8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 }),  /* 1980 */
   new TagLunarCal( new int[17]{ 35,  0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 }),
   new TagLunarCal( new int[17]{ 24,  4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 }),
   new TagLunarCal( new int[17]{ 43,  0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 }),
   new TagLunarCal( new int[17]{ 32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 }),  /* 1984 */
   new TagLunarCal( new int[17]{ 50,  0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 39,  0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1 }),
   new TagLunarCal( new int[17]{ 28,  6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 }),
   new TagLunarCal( new int[17]{ 47,  0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 }),  /* 1988 */
   new TagLunarCal( new int[17]{ 36,  0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 26,  5, 0,  1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 }),
   new TagLunarCal( new int[17]{ 45,  0, 1,  6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 34,  0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 }),  /* 1992 */
   new TagLunarCal( new int[17]{ 22,  3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 40,  0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 30,  8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 49,  0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1 }),  /* 1996 */
   new TagLunarCal( new int[17]{ 37,  0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 27,  5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 46,  0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 35,  0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 }),  /* 2000 */
   new TagLunarCal( new int[17]{ 23,  4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 42,  0, 1,  4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 31,  0, 2,  9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 21,  2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 }),  /* 2004 */
   new TagLunarCal( new int[17]{ 39,  0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 28,  7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 48,  0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 37,  0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 }),  /* 2008 */
   new TagLunarCal( new int[17]{ 25,  5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 44,  0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 }),
   new TagLunarCal( new int[17]{ 33,  0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 22,  4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }),  /* 2012 */
   new TagLunarCal( new int[17]{ 40,  0, 1,  2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 30,  9, 2,  7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 49,  0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 38,  0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 }),  /* 2016 */
   new TagLunarCal( new int[17]{ 27,  6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 }),
   new TagLunarCal( new int[17]{ 46,  0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 35,  0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 24,  4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }),  /* 2020 */
   new TagLunarCal( new int[17]{ 42,  0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 31,  0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 }),
   new TagLunarCal( new int[17]{ 21,  2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 40,  0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 }),  /* 2024 */
   new TagLunarCal( new int[17]{ 28,  6, 2,  5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 }),
   new TagLunarCal( new int[17]{ 47,  0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 36,  0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 25,  5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 }),  /* 2028 */
   new TagLunarCal( new int[17]{ 43,  0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 }),
   new TagLunarCal( new int[17]{ 32,  0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 }),
   new TagLunarCal( new int[17]{ 22,  3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 })
  };

  private  const int LASTYEAR = 2031;

  /* 西历年每月之日数 */
  int[]  SolarCal = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
          
  /* 西历年每月之累积日数, 平年与闰年 */
  int[][] SolarDays =  new int[][] { new int[]{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396 },new int []{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 } };

  /* 求此西历年是否为闰年, 返回 0 为平年, 1 为闰年 */
  int GetLeap( int year )
  {
   if ( year % 400 == 0 )
    return 1;
   else if ( year % 100 == 0 )
    return 0;
   else if ( year % 4 == 0 )
    return 1;
   else
    return 0;
  }

  /* 西历农历转换 */
  public  int CalConv( ref ConvDate cd )
  {
   int leap, d, sm, y, im, l1, l2, acc, i, lm, kc;

   if (  cd.Source == 0 )  /* Solar */
   {
                
    if ( cd.SolarYear <= FIRSTYEAR || cd.SolarYear > LASTYEAR )
     return 1;

    sm = cd.SolarMonth - 1;
    if ( sm < 0 || sm > 11 )
     return 2;
    leap = GetLeap( cd.SolarYear );

    if ( sm == 1 )
     d = leap + 28;
    else
     d = SolarCal[sm];

    if ( cd.SolarDate < 1 || cd.SolarDate > d )
     return 3;

    y = cd.SolarYear - FIRSTYEAR;
    acc = SolarDays[leap][sm] + cd.SolarDate;
    cd.Weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
    kc = acc + LunarCal[y].BaseKanChih;
    cd.Kan = kc % 10;
    cd.Chih = kc % 12;
    if ( acc <= LunarCal[y].BaseDays )
    {
     y--;
     cd.LunarYear = cd.SolarYear - 1;
     leap = GetLeap( cd.LunarYear );
     sm += 12;
     acc = SolarDays[leap][sm] + cd.SolarDate;
    }
    else
     cd.LunarYear = cd.SolarYear;
    l1 = LunarCal[y].BaseDays;
    for ( i=0; i<13; i++ )
    {
     l2 = l1 + LunarCal[y].MonthDays[i] + 29;
     if ( acc <= l2 )
      break;
     l1 = l2;
    }
    cd.LunarMonth = i + 1;
    cd.LunarDate = acc - l1;
    im = LunarCal[y].Intercalation;
    if ( im != 0 && cd.LunarMonth > im )
    {
     cd.LunarMonth--;
     if ( cd.LunarMonth == im )
      cd.LunarMonth = -im;
    }
    if ( cd.LunarMonth > 12 )
     cd.LunarMonth -= 12;
   }
   else  /* Lunar */
   {
    if ( cd.LunarYear < FIRSTYEAR || cd.LunarYear >= LASTYEAR )
     return 1;
    y = cd.LunarYear - FIRSTYEAR;
    im = LunarCal[y].Intercalation;
    lm = cd.LunarMonth;
    if ( lm < 0 )
    {
     if ( lm != -im )
      return 2;
    }
    else if ( lm < 1 || lm > 12 )
     return 2;
    if ( im != 0 )
    {
     if ( lm > im )
      lm++;
     else if ( lm == -im )
      lm = im + 1;
    }
    lm--;
    if ( cd.LunarDate > LunarCal[y].MonthDays[lm] + 29 )
     return 3;
    acc = LunarCal[y].BaseDays;
    for ( i=0; i<lm; i++ )
     acc += LunarCal[y].MonthDays[i] + 29;
    acc += cd.LunarDate;
    leap = GetLeap( cd.LunarYear );
    for ( i=13; i>=0; i-- )
     if ( acc > SolarDays[leap][i] )
      break;
    cd.SolarDate = acc - SolarDays[leap][i];
    if ( i <= 11 )
    {
     cd.SolarYear = cd.LunarYear;
     cd.SolarMonth = i + 1;
    }
    else
    {
     cd.SolarYear = cd.LunarYear + 1;
     cd.SolarMonth = i - 11;
    }
    leap = GetLeap( cd.SolarYear );
    y = cd.SolarYear - FIRSTYEAR;
    acc = SolarDays[leap][cd.SolarMonth-1] + cd.SolarDate;
    cd.Weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
    kc = acc + LunarCal[y].BaseKanChih;
    cd.Kan = kc % 10;
    cd.Chih = kc % 12;
   }
   return 0;
  }


 }
 public class  ConvDate
 {
  public int Source;
  public int SolarYear;
  public int SolarMonth;
  public int SolarDate;
  public int LunarYear;
  public int LunarMonth;
  public int LunarDate;
  public int Weekday;
  public   int Kan;
  public   int Chih;


  string[]  KanDetails=new string[]{"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
  string[]  ZhiDetail=new string[]{"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
  string[]  Animals=new string[]{"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
  string[]  LunarTerms = new string[]{"小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"};

  string[]  nStr1 = new string[]{"日","一","二","三","四","五","六","七","八","九","十"};
  string[]  nStr2 = new string[]{"初","十","廿","卅"," "};
  string[]  monthNames = new string[]{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
  string[]  solarTerms = new string[]  {
             "0101*元旦",
             "0214 情人节",
             "0308 妇女节",
             "0312 植树节",
             "0315 消费者权益日",
             "0317 St. Patrick's",
             "0401 愚人节",
             "0501 劳动节",
             "0504 青年节",
             "0512 护士节",          
             "0601 儿童节",          
             "0614 Flag Day",
             "0701 建党节 香港回归纪念",
             "0801 建军节",
             "0808 父亲节",         
             "0909 毛泽东逝世纪念",
             "0910 教师节",
             "0928 孔子诞辰",
             "1001*国庆节",
             "1006 老人节",           
             "1024 联合国日",
             "1111 Veteran's / Remembrance Day",
             "1112 孙中山诞辰纪念",
             "1220 澳门回归纪念",
             "1225 Christmas Day",
             "1226 毛泽东诞辰纪念"};
  //农历节日 *表示放假日
  string[]  lunarTerms = new string[]{
              "0101*春节",
              "0115 元宵节",
              "0505 端午节",
              "0707 七夕情人节",
              "0715 中元节",
              "0815 中秋节",
              "0909 重阳节",
              "1208 腊八节",
              "1224 小年",
              "0100*除夕"};
  //某月的第几个星期几
  string[] solarTermsByWeekDay = new string[]{
                "0131 Martin Luther King Day",
                "0231 President's Day",
                "0520 母亲节",
                "0530 Armed Forces Day",
                "0531 Victoria Day",
                "0716 合作节",
                "0730 被奴役国家周",
                "0811 Civic Holiday",
                "0911 Labor Holiday",
                "1021 Columbus Day",
                "1144 Thanksgiving"};
  //星期

  private string[] weekDayName = new string[]{"日","一","二","三","四","五","六"};
  public string weekDay
  {
   get
   {
    if ( this.Source == 0 )
     return "星期" + weekDayName[this.Weekday];
    else
     return string.Empty ;
   }
  }


  //农历干支
  public string LunarKanZhi
  {
  
   get
   {
   
    return KanDetails[this.Kan] + ZhiDetail[this.Chih ];

   }
  }

  //农历节气
  public string LunarTerm
  {
   get
   {
   
    return this.GetLunarTerm( this.LunarMonth , this.LunarDate );
   }
  }

  //阳历节气
  public string SolarTerm
  {
   get
   {
   
    return this.GetSolarTerm( );
   }
  }

  ///农历的完整日期形式
  public string LunarWholeDate
  {
   get
   {
    return this.LunarYear.ToString() + "-" + this.LunarMonth.ToString() + "-" + this.LunarDate.ToString();
   }
  }
  ///阳历的完整日期形式
  public string SolarWholeDate
  {
   get
   {
    return this.SolarYear .ToString() + "-" + this.SolarMonth .ToString() + "-" + this.SolarDate .ToString();
   }
  }
 


  ///农历节气
  private string  GetLunarTerm( int month , int day)
  {

   string find = ( month < 10 ? "0" + month.ToString() : month.ToString()) + ( day < 10 ? "0" + day.ToString() : day.ToString());
   
  
   for ( int i = 0 ; i <= lunarTerms.GetUpperBound(0) ;i ++)
   {
   
    if ( lunarTerms[i].Substring( 0 , 4).CompareTo( find) == 0 )
     return lunarTerms[i].Trim().Substring( 5 ,lunarTerms[i].Trim().Length - 5 )  ;
   }
   return string.Empty ;
  
  }
  
  private string  GetSolarTerm( )
  {


   int month = this.SolarMonth ;
   int day =this.SolarDate ;
   int weekday = this.Weekday ;

   string find = ( month < 10 ? "0" + month.ToString() : month.ToString()) + ( day < 10 ? "0" + day.ToString() : day.ToString());

   string terms = string.Empty ;

   
  
   for ( int i = 0 ; i <= solarTerms.GetUpperBound(0) ;i ++)
   {
   
    if ( solarTerms[i].Substring( 0 , 4).CompareTo( find) == 0 )
    {
     terms = solarTerms[i].Trim().Substring( 5 ,solarTerms[i].Trim().Length - 5 )  ;
     break;
    }
 

   
   }

  
   return terms ;
  
  
  }
  ///构造函数。fromSolarToLunar =true表示由阳历转化为农历;=false表示用农历转化为阳历.
  public ConvDate( bool fromSolarToLunar , int year , int month , int day)
  {
  

   if ( fromSolarToLunar )
   {
    this.Source = 0 ;
    this.SolarYear  = year;
    this.SolarMonth = month;
    this.SolarDate = day;
   }
   else
   {

    this.Source = 1 ;
    this.LunarYear   = year;
    this.LunarMonth = month;
    this.LunarDate = day;
   }

  
  }
 }
 public class TagLunarCal
 {
  public int BaseDays;        /* 到西历 1 月 1 日到农历正月初一的累积日数 */
  public int Intercalation;    /* 闰月月份. 0==此年没有闰月 */
  public int BaseWeekday;      /* 此年西历 1 月 1 日为星期几再减 1 */
  public int BaseKanChih;      /* 此年西历 1 月 1 日之干支序号减 1 */
  public int[] MonthDays = new int[13] ;    /* 此农历年每月之大小, 0==小月(29日), 1==大月(30日) */

  public TagLunarCal( int[] arr)
  {
  
   this.BaseDays = arr[0];
   this.Intercalation = arr[1];
   this.BaseWeekday = arr[2];
   this.BaseKanChih =arr[3];
   for ( int i = 0 ;  i < 13 ; i ++ )
    this.MonthDays[i] = arr[ i + 4 ] ;


  
  }
 }

}

原创粉丝点击