查找某公历年中国新年公历的算法

时间:2015-06-08 21:10:59

标签: c# algorithm date calendar gregorian-calendar

我正在制作一个驱动程序来计算给定时间跨度内的各种假期。所以,我需要找到所有中国假期(中国新年,清明节,端午节等)的格里高利日。我使用着名的“复活节算法”进行耶稣受难日,复活节星期一,耶稣升天节和惠特星期一计算;但是,我不太了解它以适应中国历法。

我发现了类似的问题,但他们经常从格里高利到中国:

Moon / Lunar Phase Algorithm

Calculating lunar/lunisolar holidays in python

http://www.herongyang.com/year/program.html

http://www.hermetic.ch/cal_stud/ch_year.htm

最后一个链接非常有用,但我仍然不确定如何以一种可以帮助我的方式实现该算法。任何建议或代码将不胜感激!

这是我的耶稣受难日算法:

private void GetGoodFridayOccurances(DateTime startDate, DateTime endDate,      List<ObservedHoliday> observedHolidays, StandardHoliday holiday)
    {
        for (DateTime date = startDate; date <= endDate; date = date.AddYears(1))
        {
            #region Finding the Day of Easter Algorithm
            int day, month;
            int firstTwo = date.Year / 100;
            int remainderMod = date.Year % 19;
            int pfmDate = (firstTwo - 15) / 2 + 202 - 11 * remainderMod;
            #region switches
            switch (firstTwo)
            {
                case 21:
                case 24:
                case 25:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                case 32:
                case 34:
                case 35:
                case 38:
                    pfmDate = pfmDate - 1;
                    break;
                case 33:
                case 36:
                case 37:
                case 39:
                case 40:
                    pfmDate = pfmDate - 2;
                    break;
            }
            #endregion
            pfmDate = pfmDate % 30;

            int tA = pfmDate + 21;
            if (pfmDate == 29)
                tA = tA - 1;
            if (pfmDate == 29 && remainderMod > 10)
                tA = tA - 1;
            //Find next sunday
            int tB = (tA - 19) % 7;

            int tC = (40 - firstTwo) % 4;
            if (tC == 3 || tC > 1)
                tC = tC + 1;

            pfmDate = date.Year % 100;
            int tD = (pfmDate + pfmDate / 4) % 7;
            int tE = ((20 - tB - tC - tD) % 7) + 1;
            day = tA + tE;

            if (day > 31)
            {
                day = day - 31;
                month = 4;
            }
            else
            {
                month = 3;
            }
            #endregion

            DateTime observed = new DateTime(date.Year, month, day).AddDays(-2);
            ObservedHoliday obsdate = new ObservedHoliday(holiday);
            if (startDate == endDate && startDate.Day == observed.Day)
            {
                obsdate.DateObserved = observed;
                observedHolidays.Add(obsdate);
            }
            else if (startDate != endDate && observed >= startDate)
            {
                obsdate.DateObserved = observed;
                observedHolidays.Add(obsdate);
            }
        }  

2 个答案:

答案 0 :(得分:9)

对于中国新年,我认为这会奏效:

ChineseLunisolarCalendar chinese   = new ChineseLunisolarCalendar();
GregorianCalendar        gregorian = new GregorianCalendar();

DateTime utcNow = DateTime.UtcNow;

// Get Chinese New Year of current UTC date/time
DateTime chineseNewYear = chinese.ToDateTime( utcNow.Year, 1, 1, 0, 0, 0, 0 );

// Convert back to Gregorian (you could just query properties of `chineseNewYear` directly, but I prefer to use `GregorianCalendar` for consistency:

Int32 year  = gregorian.GetYear( chineseNewYear );
Int32 month = gregorian.GetMonth( chineseNewYear );
Int32 day   = gregorian.GetDayOfMonth( chineseNewYear );

答案 1 :(得分:0)

由于中国历法非常复杂(并且基于天文学考虑因素),因此没有简单的方法来计算中国新年的阳历日期,这在所有情况下都是正确的。 (有一些&#39;经验法则总是会失败多年。)对于基础理论,请参阅here,对于Windows软件(以及其他中国日历计算),请参阅{{3 }}