C# 农历类[下]

  /// <summary>
  /// 取得指定阴历年的阴历闰月月份。
  /// </summary>
  /// <param name="iLunarYear">年份。</param>
  /// <returns>返回指定年的闰月月份。</returns>
  private int GetLeapMonth(ushort iLunarYear)
  {
   byte Flag;
   if (iLunarYear < START_YEAR || iLunarYear > END_YEAR)
   {
    return 0;
   }
   Flag = gLunarMonth[(iLunarYear - START_YEAR)/2];
   if ((iLunarYear - START_YEAR)%2 == 0)
   {
    return Flag >> 4;
   }
   else
   {
    return Flag & 0x0F;
   }
  }

  /// <summary>
  /// 计算指定阴历年月的总天数。
  /// </summary>
  /// <param name="iLunarYear">年份。</param>
  /// <param name="iLunarMonth">月份。</param>
  /// <returns>
  /// 返回阴历阴历年月的天数,如果该月为闰月,高字为第二个该月的天数,否则高字为0。
  /// </returns>
  /// <remarks>
  /// 指定年月范围在1901年1月---2050年12月之间。
  /// </remarks>
  private uint GetLunarMonthDays(ushort iLunarYear, ushort iLunarMonth)
  {
   int Height, Low;
   int iBit;
   if (iLunarYear < START_YEAR || iLunarYear > END_YEAR)
   {
    return 30;
   }
   Height = 0;
   Low = 29;
   iBit = 16 - iLunarMonth;
   if (iLunarMonth > GetLeapMonth(iLunarYear) && GetLeapMonth(iLunarYear) > 0)
   {
    iBit--;
   }
   if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << iBit)) > 0)
   {
    Low++;
   }
   if (iLunarMonth == GetLeapMonth(iLunarYear))
   {
    if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << (iBit - 1))) > 0)
    {
     Height = 30;
    }
    else
    {
     Height = 29;
    }
   }
   return (uint) (Low) | (uint) (Height) << 16; //合成为uint
  }

  /// <summary>
  /// 计算指定阴历年总天数。
  /// </summary>
  /// <param name="iLunarYear">指定阴历年,范围1901-2050。</param>
  /// <returns>返指定阴历年的总天数。</returns>
  private int GetLunarYearDays(ushort iLunarYear)
  {
   int Days;
   uint tmp;
   if (iLunarYear < START_YEAR || iLunarYear > END_YEAR)
   {
    return 0;
   }
   Days = 0;
   for (ushort i = 1; i <= 12; i++)
   {
    tmp = GetLunarMonthDays(iLunarYear, i);
    Days = Days + ((ushort) (tmp >> 16) & 0xFFFF); //取高位
    Days = Days + (ushort) (tmp); //取低位
   }
   return Days;
  }

  /// <summary>
  /// 计算从1901年1月1日过iSpanDays天后的阴历日期
  /// </summary>
  /// <param name="iYear">返回的年份。</param>
  /// <param name="iMonth">返回的月份。</param>
  /// <param name="iDay">返回的日子。</param>
  /// <param name="iSpanDays">天数。</param>
  private void CalcLunarDate(out ushort iYear, out ushort iMonth, out ushort iDay, uint iSpanDays)
  {
   uint tmp;
   //阳历1901年2月19日为阴历1901年正月初一
   //阳历1901年1月1日到2月19日共有49天
   if (iSpanDays < 49)
   {
    iYear = START_YEAR - 1;
    if (iSpanDays < 19)
    {
     iMonth = 11;
     iDay = (ushort) (11 + iSpanDays);
    }
    else
    {
     iMonth = 12;
     iDay = (ushort) (iSpanDays - 18);
    }
    return;
   }
   //下面从阴历1901年正月初一算起
   iSpanDays = iSpanDays - 49;
   iYear = START_YEAR;
   iMonth = 1;
   iDay = 1;
   //计算年
   tmp = (uint) GetLunarYearDays(iYear);
   while (iSpanDays >= tmp)
   {
    iSpanDays = iSpanDays - tmp;
    iYear++;
    tmp = (uint) GetLunarYearDays(iYear);
   }
   //计算月
   tmp = GetLunarMonthDays(iYear, iMonth); //取低位
   while (iSpanDays >= tmp)
   {
    iSpanDays = iSpanDays - tmp;
    if (iMonth == GetLeapMonth(iYear))
    {
     tmp = (GetLunarMonthDays(iYear, iMonth) >> 16) & 0xFFFF; //取高位
     if (iSpanDays < tmp)
     {
      break;
     }
     iSpanDays = iSpanDays - tmp;
    }
    iMonth++;
    tmp = GetLunarMonthDays(iYear, iMonth); //取低位
   }
   //计算日
   iDay = (ushort) (iDay + iSpanDays);
  }
  #endregion
  #region 星座
  /// <summary>
  /// 计算指定当前日期的星座序号。
  /// </summary>
  /// <returns>星座序号。</returns>
  private int GetConstellationIndex()
  {
   int Y, M, D;
   Y = m_Date.Year;
   M = m_Date.Month;
   D = m_Date.Day;
   Y = M*100 + D;
   if (Y >= 321 && Y <= 419)
   {
    return 0;
   }
   else if (Y >= 420 && Y <= 520)
   {
    return 1;
   }
   else if (Y >= 521 && Y <= 620)
   {
    return 2;
   }
   else if (Y >= 621 && Y <= 722)
   {
    return 3;
   }
   else if (Y >= 723 && Y <= 822)
   {
    return 4;
   }
   else if (Y >= 823 && Y <= 922)
   {
    return 5;
   }
   else if (Y >= 923 && Y <= 1022)
   {
    return 6;
   }
   else if (Y >= 1023 && Y <= 1121)
   {
    return 7;
   }
   else if (Y >= 1122 && Y <= 1221)
   {
    return 8;
   }
   else if (Y >= 1222 || Y <= 119)
   {
    return 9;
   }
   else if (Y >= 120 && Y <= 218)
   {
    return 10;
   }
   else if (Y >= 219 && Y <= 320)
   {
    return 11;
   }
   else
   {
    return -1;
   }
  }

  /// <summary>
  /// 格式化星座序号为星座名称。
  /// </summary>
  /// <param name="ConstellationIndex">星座序号。</param>
  /// <returns>星座名称。</returns>
  private string FormatConstellation(int ConstellationIndex)
  {
   if (ConstellationIndex >= 0 && ConstellationIndex <= 11)
   {
    return ConstellationName[ConstellationIndex];
   }
   else
   {
    return "";
   }
  }
  #endregion
  #region 节气
  /// <summary>
  /// 计算公历当天对应的节气序号。
  /// </summary>
  /// <returns>返回值0-23为节气序号,-1表示不是节气。</returns>
  private int GetSolarTermIndex()
  {
   byte Flag;
   int Day, iYear, iMonth, iDay;
   iYear = m_Date.Year;
   if (iYear < START_YEAR || iYear > END_YEAR)
   {
    return -1;
   }
   iMonth = m_Date.Month;
   iDay = m_Date.Day;
   Flag = gLunarHolDay[(iYear - START_YEAR)*12 + iMonth - 1];
   if (iDay < 15)
   {
    Day = 15 - ((Flag >> 4) & 0x0f);
   }
   else
   {
    Day = (Flag & 0x0f) + 15;
   }
   if (iDay == Day)
   {
    if (iDay > 15)
    {
     return (iMonth - 1)*2 + 1;
    }
    else
    {
     return (iMonth - 1)*2;
    }
   }
   else
   {
    return -1;
   }
  }

  /// <summary>
  /// 格式化节气序号为节气名称。
  /// </summary>
  /// <param name="SolarTermIndex">节气序号。</param>
  /// <returns>节气名称。</returns>
  private string FormatSolarTerm(int SolarTermIndex)
  {
   //string[] stroe = {"小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"};
   if (SolarTermIndex <= this.LunarHolDayName.Length && SolarTermIndex >= 0)
    return this.LunarHolDayName[SolarTermIndex];
   return "";
  }
  #endregion
  #region 年月日
  /// <summary>
  /// 格式化阴历月份。
  /// </summary>
  /// <param name="iYear">年份。</param>
  /// <returns>干支记年。</returns>
  private string FormatLunarYear(int iYear)
  {
   string strG = "甲乙丙丁戊己庚辛壬癸";
   string strZ = "子丑寅卯辰巳午未申酉戌亥";
   return strG.Substring((iYear - 4)%10, 1) + strZ.Substring((iYear - 4)%12, 1);
  }

  /// <summary>
  /// 格式化阴历年份。
  /// </summary>
  /// <param name="iYear">年份。</param>
  /// <returns>生肖。</returns>
  private string FormatAnimalYear(int iYear)
  {
   string strSX = "鼠牛虎免龙蛇马羊猴鸡狗猪";
   return strSX.Substring((iYear - 4)%12, 1);
  }

  /// <summary>
  /// 格式化阴历月份。
  /// </summary>
  /// <param name="iMonth">月份。</param>
  /// <returns>中文月份。</returns>
  private string FormatLunarMonth(int iMonth)
  {
   string szText = "正二三四五六七八九十";
   if (iMonth <= 10) return szText.Substring(iMonth - 1, 1) + "月";
   if (iMonth == 11) return "十一月";
   if (iMonth == 12) return "十二月";
   return "";
  }

  /// <summary>
  /// 格式化阴历日子。
  /// </summary>
  /// <param name="iDay">日子。</param>
  /// <returns>中文日子。</returns>
  private string FormatLunarDay(int iDay)
  {
   string szText1 = "初十廿三";
   string szText2 = "一二三四五六七八九十";
   string strDay = "";
   if (iDay != 20 && iDay != 30)
   {
    try
    {
     strDay = szText1.Substring((iDay - 1)/10, 1);
     strDay = strDay + szText2.Substring((iDay - 1)%10, 1);
    }
    catch(Exception)
    {
    
    }
   }
   else
   {
    strDay = szText1.Substring((iDay/10), 1);
    strDay = strDay + "十";
   }
   return strDay;
  }
  #endregion
  #region OutPut
  /// <summary>阴历日期,以LunarDate(年日月)形式表示。</summary>
  public LunarDate LunarDate
  {
   get
   {
    if (this.m_LunarDate == null)
    {
     ushort iYear, iMonth, iDay;
     TimeSpan ts = m_Date - (new DateTime(START_YEAR, 1, 1));
     this.CalcLunarDate(out iYear, out iMonth, out iDay, (uint) (ts.Days));
     this.m_LunarDate = new LunarDate(iYear, iMonth, iDay);
    }
    return this.m_LunarDate;
   }
  }

  /// <summary>阴历干支记年。</summary>
  public string LunarYear
  {
   get
   {
    if (m_LunarYear == "")
     this.m_LunarYear = this.FormatLunarYear(this.LunarDate.Year);
    return this.m_LunarYear;
   }
  }

  /// <summary>阴历生肖。</summary>
  public string Animal
  {
   get
   {
    if (m_Animal == "")
     this.m_Animal = this.FormatAnimalYear(this.LunarDate.Year);
    return this.m_Animal;
   }
  }

  /// <summary>格式化后的阴历月份。</summary>
  public string LunarMonth
  {
   get
   {
    if (this.m_LunarMonth == "")
     this.m_LunarMonth = this.FormatLunarMonth(ushort.Parse(this.LunarDate.Month.ToString()));
    return this.m_LunarMonth;
   }
  }

  /// <summary>格式化后的阴历日子。</summary>
  public string LunarDay
  {
   get
   {
    if (this.m_LunarDay == "")
     this.m_LunarDay = this.FormatLunarDay(ushort.Parse(this.LunarDate.Day.ToString()));
    return this.m_LunarDay;
   }
  }

  /// <summary>格式化后的阴历节气。</summary>
  public string SolarTerm
  {
   get
   {
    if (this.m_SolarTerm == "")
     this.m_SolarTerm = this.FormatSolarTerm(this.GetSolarTermIndex());
    return this.m_SolarTerm;
   }
  }

  /// <summary>格式化后的星座。</summary>
  public string Constellation
  {
   get
   {
    if (this.m_Constellation == "")
     this.m_Constellation = this.FormatConstellation(this.GetConstellationIndex());
    return this.m_Constellation;
   }
  }
  #endregion
}
public class LunarDate
{
  private int _y,_m,_d;
  private string lunardate = "";
  private string lunarmonth = "",lunarday = "",lunaryear = "";
  private string solarterm = "",animal = "";

  public LunarDate(DateTime dt)
  {
   LunarDateClass ldc = new LunarDateClass(dt) ;
   this.lunarday = ldc.LunarDay;
   this.lunarmonth = ldc.LunarMonth;
   this.lunaryear = ldc.LunarYear;
   this.solarterm = ldc.SolarTerm;
   this.animal = ldc.Animal;
   this.lunardate = this.lunaryear +"("+ this.animal +")年"+ this.lunarmonth + this.lunarday + (this.solarterm==""?"":" "+this.solarterm);
   this._y = ldc.LunarDate.Year;
   this._m = ldc.LunarDate.Month;
   this._d = ldc.LunarDate.Day;
  }
  public LunarDate(int y, int m, int d)
  {
   this._y = y;
   this._m = m;
   this._d = d;
  }
  public int Year
  {
   get{return this._y;}
  }

  public int Month
  {
   get{return this._m;}
  }

  public int Day
  {
   get{return this._d;}
  }

  public string LunarDay
  {
   get
   {
    return this.lunarday;
   }
  }

  public string LunarMonth
  {
   get
   {
    return this.lunarmonth;
   }
  }

  public string LunarYear
  {
   get
   {
    return this.lunaryear;
   }
  }

  public string SolarTerm
  {
   get
   {
    return this.solarterm;
   }
  }

  public string Animal
  {
   get
   {
    return this.animal;
   }
  }

  public new string ToString()
  {
   return this.lunardate;
  }
  
}

#endregion
}
posted @ 2008-02-14 17:55  龍峸.大卫  阅读(401)  评论(0编辑  收藏  举报