Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15183189
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: C/C++

2008-05-31 14:48:21

部分代码如下:

Lunar.cs 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace System..Forms
...{
    public class Lunar
    ...{
        private int[] LunarInfo = new int[200];
        private LunarInfo info;
        private int lunaryear;
        private int lunarmonth;
        private int lunarday;
        private bool isleap;
        private string lunarAnimal;
        private string lunarTerm;
        private bool isFixedMonth;

        构造函数#region 构造函数
        public Lunar()
        ...{
            info = new LunarInfo();
            DateTime dt = DateTime.Today;
            this.GetLunarDate(dt.Year, dt.Month, dt.Day);
            this.lunarAnimal = this.GetLunarAnimal(dt.Year);
        }

        public Lunar(int year, int month, int day)
        ...{
            info = new LunarInfo();
            this.GetLunarDate(year, month, day);
            this.lunarAnimal = this.GetLunarAnimal(year);
        }

        public Lunar(DateTime dt)
        ...{
            info = new LunarInfo();
            this.GetLunarDate(dt.Year, dt.Month, dt.Day);
            this.lunarAnimal = this.GetLunarAnimal(dt.Year);
        }

        public Lunar(string s) [Page]
        ...{
            info = new LunarInfo();
            DateTime dt = DateTime.Parse(s);
            this.GetLunarDate(dt.Year, dt.Month, dt.Day);
            this.lunarAnimal = this.GetLunarAnimal(dt.Year);
        }
        #endregion

        属性#region 属性
        public string LunarYear
        ...{
            get
            ...{
                return this.GetLunarYearName(this.lunaryear - 1900 + 36);
                //return this.lunaryear.ToString();
            }
        }

        public string LunarMonth
        ...{
            get
            ...{
                if (this.isleap)
                    return \"闰\" + GetLunarMonthName(this.lunarmonth);
                else
                    return this.GetLunarMonthName(this.lunarmonth);
            }
        }

      public string LunarDay
        ...{
            get
            ...{
                if (this.isFixedMonth) [Page]
                ...{
                    if (this.lunarday == 1)
                        return this.LunarMonth;
                    else
                        return GetLunarDayName(this.lunarday);
                }
                else
                    return GetLunarDayName(this.lunarday);
                //return this.lunarday.ToString();
            }
        }

        public string LunarDate
        ...{
            get
            ...{
                return this.LunarYear + \"年\" + this.LunarMonth + this.LunarDay;
            }
        }

        public bool IsLeap
        ...{
            get
            ...{
                return this.isleap;
            }
        }

        public string LunarAnimal
        ...{
            get
            ...{ [Page]
                return this.lunarAnimal;
            }
        }

        public bool IsFixedMonth
        ...{
            set
            ...{
                this.isFixedMonth = value;
            }
        }

#endregion
        /**////
        /// 取指定年份的阴历年天数
        ///

        /// 指定年份,在1900-2050之间
        /// -1:表示指定的年份不在接受的范围之内
        public int GetLunarYearDays(int year)
        ...{
            if (this.IsValidateYear(year))
                return -1;
            int sum = 348;//基准天数29*12=348
            //1-12月的天数
            for (int i = 0x8000; i > 0x8; i >>= 1)
            ...{
                sum += ((info.lunarInfo[year - 1900] & i) != 0) ? 1 : 0;

       }
            //加上闰月的天数
            return sum + this.GetLeapMonthDays(year);

        }
        /**////


        /// 取得指定年份的闰月天数 [Page]
        ///

        /// 指定年份,取值范围在1900-2050
        /// 如果无效日期,返回0
        public int GetLeapMonthDays(int year)
        ...{
            if (this.IsValidateYear(year))
                return 0;
            if (this.GetLeapMonth(year) > 0)
                return ((info.lunarInfo[year - 1900] & 0x10000) == 0) ? 29 : 30;
            else
                return 0;
        }
        /**////
        /// 取得指定年份的闰月的月份数,如果没有闰月就返回0
        ///

        /// 指定的年份,取值范围1900-2500
        /// 如果没有闰月返回0
        public int GetLeapMonth(int year)
        ...{
            if (this.IsValidateYear(year))
                return 0;
            return info.lunarInfo[year - 1900] & 0xF;

        }
        /**////


        /// 取得指定年份和月份的天数,不含闰月
        ///

        /// 指定的年份
        /// 月份
        /// 天数
        public int GetMonthDays(int year, int month) [Page]
        ...{
            if (this.IsValidateYear(year) || this.IsValidateMonth(month))
                return 0;
            return ((info.lunarInfo[year - 1900] & (0x10000 >> month)) == 0) ? 29 : 30;
        }

        private bool IsValidateYear(int year)
        ...{
            if (year > 2050 || year < 1900)
                return true;
            else
                return false;
        }

        private bool IsValidateMonth(int month)
        ...{
            if (month > 12 || month < 1)
                return true;
            else
                return false;
        }

        private bool IsValidateDate(int year, int month, int day)
        ...{
            bool flag = true;

  try
            ...{
                DateTime dt = new DateTime(year, month, day);
                DateTime dt0 = new DateTime(1900, 1, 31, 0, 0, 0);
                DateTime dt1 = new DateTime(2050, 12, 31, 23, 59, 59);
                if (dt.ToFileTimeUtc() < dt0.ToFileTimeUtc() || dt.ToFileTimeUtc() > dt1.ToFileTimeUtc()) [Page]
                    flag = false;
            }
            catch (Exception ex)
            ...{
                flag = false;
            }

            return flag;
        }

        public string GetLunarYearName(int Num)
        ...{
            return info.Gan[Num % 10] + info.Zhi[Num % 12];
        }

        public string GetLunarMonthName(int month)
        ...{
            return info.nStr3[month - 1];
        }

        private string GetLunarDayName(int day)
        ...{
            return (day == 10) ? info.nStr2[0] + info.nStr1[10] : info.nStr2[day / 10] + info.nStr1[day - (day - 1) / 10 * 10];
        }

        public string GetLunarAnimal(int year)
        ...{
            return info.Animals[(year - 4) % 12];
        }

        public void GetLunarDate(int year, int month, int day)
        ...{
            if (!this.IsValidateDate(year, month, day))
                return;
            int sYear, sMonth, sDay;
            int i, leap = 0, temp = 0;
            bool flagLeap = false; [Page]
            DateTime dt = new DateTime(year, month, day);
            DateTime dt0 = new DateTime(1900, 1, 30);
            int offset = Convert.ToInt32((dt.ToFileTimeUtc() - dt0.ToFileTimeUtc()) / 864000000000L);
            //求得年份
            for (i = 1900; i <= 2050 && offset > 0; i++)
            ...{
                temp = this.GetLunarYearDays(i);
                offset -= temp;
            }
            if (offset <= 0)
            ...{
                sYear = i - 1;
                offset += temp;
            }
            else
            ...{
                sYear = i;

      }

            //求得月份
            leap = this.GetLeapMonth(sYear);

            for (i = 1; i <= 12 && offset > 0; i++)
            ...{
                if (leap > 0 && i == (leap + 1) && !flagLeap)
                ...{
                    i -= 1;
                    temp = this.GetLeapMonthDays(sYear); [Page]
                    flagLeap = true;
                }
                else
                ...{
                    temp = this.GetMonthDays(sYear, i);
                }
                if (flagLeap && i == (leap + 1))
                    flagLeap = false;
                offset -= temp;
            }
            if ((offset == 0) && i == (leap + 1) && leap > 0)
            ...{
                if (flagLeap) //为闰月的第0天 就是不是闰月的最后一天
                    flagLeap = false;
                else   //为闰月的最后一天 ...{
                    flagLeap = true;
                    i -= 1;
                }
            }
            if (offset <= 0)
            ...{
                offset += temp;
                i -= 1; [Page]
            }
            sMonth = i;
            sDay = offset;
            this.lunaryear = sYear;
            this.lunarmonth = sMonth;
            this.lunarday = sDay;
            this.isleap = flagLeap;
        }

        private DateTime solarTerm(int year, int n)
        ...{
            int offSet;
            DateTime dt0 = new DateTime(1900, 1, 6, 2, 5, 0);
            offSet = Convert.ToInt32((315569259747000L * (year - 1900) + info.solarTermInfo[n] * 600000000L) / 864000000000L);
            return dt0.AddDays(offSet);
        }

        public string GetPrevSolarInfo(int year, int month)

    ...{
            string solarinfo;
            if (this.IsValidateYear(year) || this.IsValidateMonth(month))
                return \"\";
            DateTime dt = this.solarTerm(year, month * 2 - 2);
            this.GetLunarDate(dt.Year, dt.Month, dt.Day);
            solarinfo = this.LunarDay + \":\" + info.solarTerm[month * 2 - 2];
            return solarinfo;
        }

        public string GetNextSolarInfo(int year, int month)
        ...{
            string solarinfo;
            if (this.IsValidateYear(year) || this.IsValidateMonth(month)) [Page]
                return \"\";
            DateTime dt = this.solarTerm(year, month * 2 - 1);
            this.GetLunarDate(dt.Year, dt.Month, dt.Day);
            solarinfo = this.LunarDay + \":\" + info.solarTerm[month * 2 - 1];
            return solarinfo;
        }

        public int GetSolarDays(int year, int month)
        ...{
            int y = year;
            int m = month;
            while (m < 1)
            ...{
                y -= 1;
                m += 12;
            }
            if (DateTime.IsLeapYear(y))
                return 29;
            else
                return info.solarMonth[m - 1];
        }
    }
}
LunarInfo.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace System..Forms.LunarCalendar
...{
    public class LunarInfo
    ...{
        public int[] lunarInfo =...{
            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, [Page]
            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
        };

        public int[] solarMonth =...{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        public string[] Gan =...{ \"甲\", \"乙\", \"丙\", \"丁\", \"戊\", \"己\", \"庚\", \"辛\", \"壬\", \"癸\" };
        public string[] Zhi =...{ \"子\", \"丑\", \"寅\", \"卯\", \"辰\", \"巳\", \"午\", \"未\", \"申\", \"酉\", \"戌\", \"亥\" };
        public string[] Animals =...{ \"鼠\", \"牛\", \"虎\", \"兔\", \"龙\", \"蛇\", \"马\", \"羊\", \"猴\", \"鸡\", \"狗\", \"猪\" };
        public string[] solarTerm =...{ \"小寒\", \"大寒\", \"立春\", \"雨水\", \"惊蛰\", \"春分\", \"清明\", \"谷雨\", \"立夏\", \"小满\", \"芒种\", \"夏至\", \"小暑\", \"大暑\", \"立秋\", \"处暑\", \"白露\", \"秋分\", \"寒露\", \"霜降\", \"立冬\", \"小雪\", \"大雪\", \"冬至\" }; [Page]
        public int[] solarTermInfo =...{ 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
        public string[] nStr1 =...{ \"日\", \"一\", \"二\", \"三\", \"四\", \"五\", \"六\", \"七\", \"八\", \"九\", \"十\" };
        public string[] nStr2 =...{ \"初\", \"十\", \"廿\", \"卅\", \"□\" };
        public string[] nStr3 =...{ \"正月\", \"二月\", \"三月\", \"四月\", \"五月\", \"六月\", \"七月\", \"八月\", \"九月\", \"十月\", \"冬月\", \"腊月\" };
    }
}

阅读(1154) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~