c implements the lunar calendar example to share the lunar calendar query

  • 2020-06-07 05:13:37
  • OfStack


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

using System;
namespace yangliToyinli
{
    #region ChineseCalendarException
    /// <summary>
    ///  Chinese calendar exception handling 
    /// </summary>
    public class ChineseCalendarException : System.Exception
    {
        public ChineseCalendarException(string msg)
            : base(msg)
        {
        }
    }
    #endregion
    /// <summary>
    ///  Chinese lunar calendar   version V1.0  support  1900.1.31 The date to  2049.12.31 Day to day data 
    /// </summary>
    /// <remarks>
    ///  This procedure USES the data to come from the perpetual calendar inquiry on the net, and integrated 1 Some other data 
    /// </remarks>
    public class ChineseCalendar
    {
        #region  The internal structure 
        private struct SolarHolidayStruct
        {
            public int Month;
            public int Day;
            public int Recess; // The length of the holiday 
            public string HolidayName;
            public SolarHolidayStruct(int month, int day, int recess, string name)
            {
                Month = month;
                Day = day;
                Recess = recess;
                HolidayName = name;
            }
        }
        private struct LunarHolidayStruct
        {
            public int Month;
            public int Day;
            public int Recess;
            public string HolidayName;
            public LunarHolidayStruct(int month, int day, int recess, string name)
            {
                Month = month;
                Day = day;
                Recess = recess;
                HolidayName = name;
            }
        }
        private struct WeekHolidayStruct
        {
            public int Month;
            public int WeekAtMonth;
            public int WeekDay;
            public string HolidayName;
            public WeekHolidayStruct(int month, int weekAtMonth, int weekDay, string name)
            {
                Month = month;
                WeekAtMonth = weekAtMonth;
                WeekDay = weekDay;
                HolidayName = name;
            }
        }
        #endregion
        #region  Internal variables 
        private DateTime _date;
        private DateTime _datetime;
        private int _cYear;
        private int _cMonth;
        private int _cDay;
        private bool _cIsLeapMonth; // Leap month or not 
        private bool _cIsLeapYear; // Is there a leap month 
        #endregion
        #region  Basic data 
        #region  Basic constants 
        private const int MinYear = 1900;
        private const int MaxYear = 2050;
        private static DateTime MinDay = new DateTime(1900, 1, 30);
        private static DateTime MaxDay = new DateTime(2049, 12, 31);
        private const int GanZhiStartYear = 1864; // Dry branch calculation of the beginning of the year 
        private static DateTime GanZhiStartDay = new DateTime(1899, 12, 22);// The starting date 
        private const string HZNum = " zero 123456789";
        private const int AnimalStartYear = 1900; //1900 For the year of the rat 
        private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13);//28 Star reference value , This day is a Angle 
        #endregion
        #region  The lunar data 
        /// <summary>
        ///  Lunar calendar data from the Internet 
        /// </summary>
        /// <remarks>
        ///  The data structure is as follows, used together 17 A data 
        ///  The first 17 Bit: represents the number of leap month days, 0 said 29 day    1 said 30 day 
        ///  The first 16 position - The first 5 A (total 12 A) said 12 Months, one of the first 16 Who said the first 1 Month, if the month is 30 Day is 1 . 29 Day for 0
        ///  The first 4 position - The first 1 A (total 4 Bit represents which month a leap month is, and if there is no leap month in the current year, it is 0
        ///</remarks>
        private static int[] LunarDateArray = 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        
                };
        #endregion
        #region  constellations 
        private static string[] _constellationName = 
                { 
                    " Aries ", " Taurus ", " Gemini ", 
                    " cancer ", " Leo ", " virgo ", 
                    " libra ", " Scorpio ", " Sagittarius ", 
                    " Capricorn ", " Aquarius: ", " Pisces "
                };
        #endregion
        #region 2104 Solar terms 
        private static string[] _lunarHolidayName = 
                    { 
                    " Slight cold ", " Severe cold ", " Spring begins ", " The rain ", 
                    " Insects awaken ", " The vernal equinox ", " The tomb-sweeping day ", " Grain rain ", 
                    " Summer begins ", " Grain full ", " Grain in ear ", " The summer solstice ", 
                    " Slight heat ", " Great heat ", " The beginning of autumn ", " The limit of heat ", 
                    " The millennium ", " equinox ", " Cold dew ", " frost ", 
                    " The beginning of winter ", " Light snow ", " The heavy snow ", " Winter Solstice Festival "
                    };
        #endregion
        #region 2108 The stars 
        private static string[] _chineseConstellationName =
            {
                  //4        5      6          day         1      2      3  
                " Angle of wood dumpling "," Golden dragon of neck "," Ladies' bats "," The day on rabbit "," Heart on fox "," The tail fire tiger "," Leopard of white water ",
                " Dou wood xie "," Bull Taurus "," Among soil like raccoon "," Xu day rat "," Dangerous month yan "," Room fire pig "," Water wall � ",
                " KuiMu Wolf "," LouJin dog "," Stomach soil zhan "," The day of a chicken "," Never put off till tomorrow what you can on black "," Wishing fire monkey "," And water ape ",
                " Well wooden An "," Ghost golden sheep eye "," Willow soil roe "," Star day horse "," A monthly deer "," Wing salamanders; "," Chiu water loop " 
            };
        #endregion
        #region  Solar term data 
        private static string[] SolarTerm = new string[] { " Slight cold ", " Severe cold ", " Spring begins ", " The rain ", " Insects awaken ", " The vernal equinox ", " The tomb-sweeping day ", " Grain rain ", " Summer begins ", " Grain full ", " Grain in ear ", " The summer solstice ", " Slight heat ", " Great heat ", " The beginning of autumn ", " The limit of heat ", " The millennium ", " equinox ", " Cold dew ", " frost ", " The beginning of winter ", " Light snow ", " The heavy snow ", " Winter Solstice Festival " };
        private static int[] sTermInfo = 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 };
        #endregion
        #region  Lunar calendar related data 
        private static string ganStr = " A, B, C, D, p, p, p, p, p, p, p, p, p ";
        private static string zhiStr = " Zi Chou Yin MAO Chen Si wu did not declare you Xu Hai ";
        private static string animalStr = " Ox, tiger, rabbit, dragon, snake, horse, sheep, monkey, rooster, dog, pig ";
        private static string nStr1 = " day 123456789";
        private static string nStr2 = " In the early 10 20 30 ";
        private static string[] _monthString =
                {
                    " error "," In the first month ","2 month ","3 month ","4 month ","5 month ","6 month ","7 month ","8 month ","9 month ","10 month ","101 month "," lunar "
                };
        #endregion
        #region  A festival in the Gregorian calendar 
        private static SolarHolidayStruct[] sHolidayInfo = new SolarHolidayStruct[]{
            new SolarHolidayStruct(1, 1, 1, " New Year's Day "),
            new SolarHolidayStruct(2, 2, 0, " World Wetlands Day "),
            new SolarHolidayStruct(2, 10, 0, " International Weather Festival "),
            new SolarHolidayStruct(2, 14, 0, " Valentine's Day "),
            new SolarHolidayStruct(3, 1, 0, " International Seal Day "),
            new SolarHolidayStruct(3, 5, 0, " Lei Feng Memorial Day "),
            new SolarHolidayStruct(3, 8, 0, " The international working women's day "), 
            new SolarHolidayStruct(3, 12, 0, " Arbor Day   Anniversary of Sun Yat-sen's Death "), 
            new SolarHolidayStruct(3, 14, 0, " International Police Day "),
            new SolarHolidayStruct(3, 15, 0, " Consumer Rights Day "),
            new SolarHolidayStruct(3, 17, 0, " Chinese National Medicine Festival   International Day of Navigation "),
            new SolarHolidayStruct(3, 21, 0, " World Forest Day   International Day for the Elimination of Racial Discrimination   World Children's Day "),
            new SolarHolidayStruct(3, 22, 0, " World water day "),
            new SolarHolidayStruct(3, 24, 0, " World Tuberculosis Day "),
            new SolarHolidayStruct(4, 1, 0, " April Fools' Day "),
            new SolarHolidayStruct(4, 7, 0, " World Health Day "),
            new SolarHolidayStruct(4, 22, 0, " Earth Day "),
            new SolarHolidayStruct(5, 1, 1, " International Workers' Day "), 
            new SolarHolidayStruct(5, 2, 1, " Labor holidays "),
            new SolarHolidayStruct(5, 3, 1, " Labor holidays "),
            new SolarHolidayStruct(5, 4, 0, " Youth day "), 
            new SolarHolidayStruct(5, 8, 0, " The world red 10 Words, "),
            new SolarHolidayStruct(5, 12, 0, " International Nurses Day "), 
            new SolarHolidayStruct(5, 31, 0, " World No Tobacco Day "), 
            new SolarHolidayStruct(6, 1, 0, " International Children's Day "), 
            new SolarHolidayStruct(6, 5, 0, " World Environmental Protection Day "),
            new SolarHolidayStruct(6, 26, 0, " International Day against Drugs "),
            new SolarHolidayStruct(7, 1, 0, " The founding section   Anniversary of Hong Kong's Return   World Building Day "),
            new SolarHolidayStruct(7, 11, 0, " World Population Day "),
            new SolarHolidayStruct(8, 1, 0, " The army day "), 
            new SolarHolidayStruct(8, 8, 0, " China Men's Day   Father's Day "),
            new SolarHolidayStruct(8, 15, 0, " Commemoration of victory in the War of Resistance against Japanese Aggression "),
            new SolarHolidayStruct(9, 9, 0, "   Commemorate the death "), 
            new SolarHolidayStruct(9, 10, 0, " Teacher's day "), 
            new SolarHolidayStruct(9, 18, 0, "9 ・ 18 Event Anniversary "),
            new SolarHolidayStruct(9, 20, 0, " International Day for Loving Teeth "),
            new SolarHolidayStruct(9, 27, 0, " World Tourism Day "),
            new SolarHolidayStruct(9, 28, 0, " Confucius's birthday "),
            new SolarHolidayStruct(10, 1, 1, " The National Day   International Day of Music "),
            new SolarHolidayStruct(10, 2, 1, " National Day holidays "),
            new SolarHolidayStruct(10, 3, 1, " National Day holidays "),
            new SolarHolidayStruct(10, 6, 0, " advocates "), 
            new SolarHolidayStruct(10, 24, 0, " The United Nations, "),
            new SolarHolidayStruct(11, 10, 0, " World Youth Day "),
            new SolarHolidayStruct(11, 12, 0, " The Birthday of Sun Yat-sen "), 
            new SolarHolidayStruct(12, 1, 0, " World AIDS Day "), 
            new SolarHolidayStruct(12, 3, 0, " World Day of Disabled Persons "), 
            new SolarHolidayStruct(12, 20, 0, " Commemoration of Macao's Return "), 
            new SolarHolidayStruct(12, 24, 0, " On Christmas Eve "), 
            new SolarHolidayStruct(12, 25, 0, " Christmas "), 
            new SolarHolidayStruct(12, 26, 0, "  S birthday ")
           };
        #endregion
        #region  Festivals according to the lunar calendar 
        private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{
            new LunarHolidayStruct(1, 1, 1, " Spring Festival "), 
            new LunarHolidayStruct(1, 15, 0, " Lantern Festival "), 
            new LunarHolidayStruct(5, 5, 0, " Dragon Boat Festival "), 
            new LunarHolidayStruct(7, 7, 0, "7 Valentine's day evening "),
            new LunarHolidayStruct(7, 15, 0, " Ghost Festival   obon "), 
            new LunarHolidayStruct(8, 15, 0, " Mid-Autumn Festival "), 
            new LunarHolidayStruct(9, 9, 0, " Double Ninth Festival "), 
            new LunarHolidayStruct(12, 8, 0, " la 8 section "),
            new LunarHolidayStruct(12, 23, 0, " The northern off-year ( Sweep the room )"),
            new LunarHolidayStruct(12, 24, 0, " The southern off-year ( Dusted off )"),
            //new LunarHolidayStruct(12, 30, 0, " Chinese New Year's Eve ")  // Note that New Year's Eve requires other methods of calculation 
        };
        #endregion
        #region  What is the day of the week in a certain month 
        private static WeekHolidayStruct[] wHolidayInfo = new WeekHolidayStruct[]{
            new WeekHolidayStruct(5, 2, 1, " Mother's Day "), 
            new WeekHolidayStruct(5, 3, 1, " National Day for the Disabled "), 
            new WeekHolidayStruct(6, 3, 1, " Father's Day "), 
            new WeekHolidayStruct(9, 3, 3, " International Day of Peace "), 
            new WeekHolidayStruct(9, 4, 1, " International Day of the Deaf "), 
            new WeekHolidayStruct(10, 1, 2, " International Housing Day "), 
            new WeekHolidayStruct(10, 1, 4, " International Day for Natural Disaster Mitigation "),
            new WeekHolidayStruct(11, 4, 5, " Thanksgiving Day ")
        };
        #endregion
        #endregion
        #region  The constructor 
        #region ChinaCalendar < Calendar date initialization >
        /// <summary>
        ///  with 1 Ten standard Gregorian calendar dates to be initialized 
        /// </summary>
        /// <param name="dt"></param>
        public ChineseCalendar(DateTime dt)
        {
            int i;
            int leap;
            int temp;
            int offset;
            CheckDateLimit(dt);
            _date = dt.Date;
            _datetime = dt;
            // Lunar date calculation part 
            leap = 0;
            temp = 0;
            TimeSpan ts = _date - ChineseCalendar.MinDay;// Calculate the two day basic difference 
            offset = ts.Days;
            for (i = MinYear; i <= MaxYear; i++)
            {
                temp = GetChineseYearDays(i);  // Ask for the number of days in the lunar year 
                if (offset - temp < 1)
                    break;
                else
                {
                    offset = offset - temp;
                }
            }
            _cYear = i;
            leap = GetChineseLeapMonth(_cYear);// Calculate which month of the leap year 
            // Set whether there is a leap month 
            if (leap > 0)
            {
                _cIsLeapYear = true;
            }
            else
            {
                _cIsLeapYear = false;
            }
            _cIsLeapMonth = false;
            for (i = 1; i <= 12; i++)
            {
                // Leap month 
                if ((leap > 0) && (i == leap + 1) && (_cIsLeapMonth == false))
                {
                    _cIsLeapMonth = true;
                    i = i - 1;
                    temp = GetChineseLeapMonthDays(_cYear); // Calculate the number of leap months 
                }
                else
                {
                    _cIsLeapMonth = false;
                    temp = GetChineseMonthDays(_cYear, i);// Calculate the number of non-leap months 
                }
                offset = offset - temp;
                if (offset <= 0) break;
            }
            offset = offset + temp;
            _cMonth = i;
            _cDay = offset;
        }
        #endregion
        #region ChinaCalendar < Lunar date initialization >
        /// <summary>
        ///  Use the lunar calendar for the first incarnation 
        /// </summary>
        /// <param name="cy"> The lunar New Year </param>
        /// <param name="cm"> The lunar month </param>
        /// <param name="cd"> According to the lunar calendar, </param>
        /// <param name="LeapFlag"> Leap month mark </param>
        public ChineseCalendar(int cy, int cm, int cd, bool leapMonthFlag)
        {
            int i, leap, Temp, offset;
            CheckChineseDateLimit(cy, cm, cd, leapMonthFlag);
            _cYear = cy;
            _cMonth = cm;
            _cDay = cd;
            offset = 0;
            for (i = MinYear; i < cy; i++)
            {
                Temp = GetChineseYearDays(i); // Ask for the number of days in the lunar year 
                offset = offset + Temp;
            }
            leap = GetChineseLeapMonth(cy);//  Calculate which month to leap in the year 
            if (leap != 0)
            {
                this._cIsLeapYear = true;
            }
            else
            {
                this._cIsLeapYear = false;
            }
            if (cm != leap)
            {
                _cIsLeapMonth = false;  // The current date is not a leap month 
            }
            else
            {
                _cIsLeapMonth = leapMonthFlag;  // Use the leap month month entered by the user 
            }

            if ((_cIsLeapYear == false) || // There were no leap months 
                 (cm < leap)) // Calculate a month less than a leap month      
            {
                #region ...
                for (i = 1; i < cm; i++)
                {
                    Temp = GetChineseMonthDays(cy, i);// Calculate the number of non-leap months 
                    offset = offset + Temp;
                }
                // Check if the date is greater than the maximum day 
                if (cd > GetChineseMonthDays(cy, cm))
                {
                    throw new ChineseCalendarException(" An illegal lunar date ");
                }
                offset = offset + cd; // Plus the days of the month 
                #endregion
            }
            else   // Is a leap year and calculates that the month is greater than or equal to the leap month 
            {
                #region ...
                for (i = 1; i < cm; i++)
                {
                    Temp = GetChineseMonthDays(cy, i); // Calculate the number of non-leap months 
                    offset = offset + Temp;
                }
                if (cm > leap) // Calculate a month greater than a leap month 
                {
                    Temp = GetChineseLeapMonthDays(cy);   // Calculate the number of leap months 
                    offset = offset + Temp;               // Plus the number of leap months 
                    if (cd > GetChineseMonthDays(cy, cm))
                    {
                        throw new ChineseCalendarException(" An illegal lunar date ");
                    }
                    offset = offset + cd;
                }
                else  // Calculate the month equal to leap month 
                {
                    // If a leap month is to be calculated, the number of days of the normal month corresponding to the leap month should be added first 
                    if (this._cIsLeapMonth == true) // The calculating month is a leap month 
                    {
                        Temp = GetChineseMonthDays(cy, cm); // Calculate the number of non-leap months 
                        offset = offset + Temp;
                    }
                    if (cd > GetChineseLeapMonthDays(cy))
                    {
                        throw new ChineseCalendarException(" An illegal lunar date ");
                    }
                    offset = offset + cd;
                }
                #endregion
            }

            _date = MinDay.AddDays(offset);
        }
        #endregion
        #endregion
        #region  Private functions 
        #region GetChineseMonthDays
        // Back to the lunar calendar  y years m The total number of days in a month 
        private int GetChineseMonthDays(int year, int month)
        {
            if (BitTest32((LunarDateArray[year - MinYear] & 0x0000FFFF), (16 - month)))
            {
                return 30;
            }
            else
            {
                return 29;
            }
        }
        #endregion
        #region GetChineseLeapMonth
        // Back to the lunar calendar  y Year leap month  1-12 ,  No leap back  0
        private int GetChineseLeapMonth(int year)
        {
            return LunarDateArray[year - MinYear] & 0xF;
        }
        #endregion
        #region GetChineseLeapMonthDays
        // Back to the lunar calendar  y Number of days in a leap month 
        private int GetChineseLeapMonthDays(int year)
        {
            if (GetChineseLeapMonth(year) != 0)
            {
                if ((LunarDateArray[year - MinYear] & 0x10000) != 0)
                {
                    return 30;
                }
                else
                {
                    return 29;
                }
            }
            else
            {
                return 0;
            }
        }
        #endregion
        #region GetChineseYearDays
        /// <summary>
        ///  For the lunar New Year 1 In the number of days 
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private int GetChineseYearDays(int year)
        {
            int i, f, sumDay, info;
            sumDay = 348; //29 day  X 12 months 
            i = 0x8000;
            info = LunarDateArray[year - MinYear] & 0x0FFFF;
            // To calculate 12 How many days are there in a month 30 day 
            for (int m = 0; m < 12; m++)
            {
                f = info & i;
                if (f != 0)
                {
                    sumDay++;
                }
                i = i >> 1;
            }
            return sumDay + GetChineseLeapMonthDays(year);
        }
        #endregion
        #region GetChineseHour
        /// <summary>
        ///  The hour at which the current time is obtained 
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        /// 
        private string GetChineseHour(DateTime dt)
        {
            int _hour, _minute, offset, i;
            int indexGan;
            string ganHour, zhiHour;
            string tmpGan;
            // Calculate the time of the earth branch 
            _hour = dt.Hour;    // Gets the current time hour 
            _minute = dt.Minute;  // Get the current time minutes 
            if (_minute != 0) _hour += 1;
            offset = _hour / 2;
            if (offset >= 12) offset = 0;
            //zhiHour = zhiStr[offset].ToString();
            // Calculate the air-dried 
            TimeSpan ts = this._date - GanZhiStartDay;
            i = ts.Days % 60;
            indexGan = ((i % 10 + 1) * 2 - 1) % 10 - 1; //ganStr[i % 10]  Dry for the sun ,(n*2-1) %10 The corresponding branches of the ground are obtained ,n from 1 start 
            tmpGan = ganStr.Substring(indexGan) + ganStr.Substring(0, indexGan + 2);// Come together 12 position 
            //ganHour = ganStr[((i % 10 + 1) * 2 - 1) % 10 - 1].ToString();
            return tmpGan[offset].ToString() + zhiStr[offset].ToString();
        }
        #endregion
        #region CheckDateLimit
        /// <summary>
        ///  Check that the Gregorian calendar date meets the requirements 
        /// </summary>
        /// <param name="dt"></param>
        private void CheckDateLimit(DateTime dt)
        {
            if ((dt < MinDay) || (dt > MaxDay))
            {
                throw new ChineseCalendarException(" Beyond the convertible date ");
            }
        }
        #endregion
        #region CheckChineseDateLimit
        /// <summary>
        ///  Check if the lunar date is reasonable 
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="leapMonth"></param>
        private void CheckChineseDateLimit(int year, int month, int day, bool leapMonth)
        {
            if ((year < MinYear) || (year > MaxYear))
            {
                throw new ChineseCalendarException(" Illegal lunar calendar date ");
            }
            if ((month < 1) || (month > 12))
            {
                throw new ChineseCalendarException(" Illegal lunar calendar date ");
            }
            if ((day < 1) || (day > 30)) // China has the most months 30 day 
            {
                throw new ChineseCalendarException(" Illegal lunar calendar date ");
            }
            int leap = GetChineseLeapMonth(year);//  Calculate which month to leap in the year 
            if ((leapMonth == true) && (month != leap))
            {
                throw new ChineseCalendarException(" Illegal lunar calendar date ");
            }

        }
        #endregion
        #region ConvertNumToChineseNum
        /// <summary>
        ///  will 0-9 Into Chinese characters 
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private string ConvertNumToChineseNum(char n)
        {
            if ((n < '0') || (n > '9')) return "";
            switch (n)
            {
                case '0':
                    return HZNum[0].ToString();
                case '1':
                    return HZNum[1].ToString();
                case '2':
                    return HZNum[2].ToString();
                case '3':
                    return HZNum[3].ToString();
                case '4':
                    return HZNum[4].ToString();
                case '5':
                    return HZNum[5].ToString();
                case '6':
                    return HZNum[6].ToString();
                case '7':
                    return HZNum[7].ToString();
                case '8':
                    return HZNum[8].ToString();
                case '9':
                    return HZNum[9].ToString();
                default:
                    return "";
            }
        }
        #endregion
        #region BitTest32
        /// <summary>
        ///  Tests whether something is true 
        /// </summary>
        /// <param name="num"></param>
        /// <param name="bitpostion"></param>
        /// <returns></returns>
        private bool BitTest32(int num, int bitpostion)
        {
            if ((bitpostion > 31) || (bitpostion < 0))
                throw new Exception("Error Param: bitpostion[0-31]:" + bitpostion.ToString());
            int bit = 1 << bitpostion;
            if ((num & bit) == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
        #region ConvertDayOfWeek
        /// <summary>
        ///  Turn the day of the week into a numerical representation 
        /// </summary>
        /// <param name="dayOfWeek"></param>
        /// <returns></returns>
        private int ConvertDayOfWeek(DayOfWeek dayOfWeek)
        {
            switch (dayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return 1;
                case DayOfWeek.Monday:
                    return 2;
                case DayOfWeek.Tuesday:
                    return 3;
                case DayOfWeek.Wednesday:
                    return 4;
                case DayOfWeek.Thursday:
                    return 5;
                case DayOfWeek.Friday:
                    return 6;
                case DayOfWeek.Saturday:
        &nbs
                

Related articles: