【发布时间】:2015-08-23 12:16:45
【问题描述】:
我正在制作一个驱动程序来计算给定时间跨度内的各种假期。所以,我需要找到所有中国假期(农历新年、清明节、端午节等)的公历日期。我使用著名的“复活节算法”来计算耶稣受难日、复活节星期一、升天节和惠特星期一;但是,我对它的理解还不够好,无法适应中国历法。
我发现了类似的问题,但他们经常从公历到中文:
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);
}
}
【问题讨论】:
-
你不需要自己做这个,.NET有一个内置的
ChineseLunisolarCalendar类。 -
复活节算法不太可能适用于任何其他阴历:“因为日期是基于日历的春分而不是天文的,所以根据朱利安进行的计算之间存在差异日历和现代公历。”关键词:“而不是天文数字”链接:en.wikipedia.org/wiki/Computus
标签: c# algorithm date calendar gregorian-calendar