Zeller公式推导及C#代码示例(待完善)

Zeller公式用于计算给定日期是星期几。

  该方法可以用数论知识进行证明。

  假设给定日期Date为Year-Month-Day,求解该日期是星期几的问题实际上就是以之前某个确定星期几的日期作为参考点,计算它们之间的总天数,然后再除以7取余数,即可算出指定日期是星期几。

  这里,我们将选择公元0年12月31日作为参考点。原则原因是:1、公元1年1月1日是周1;2、选择一年的末尾,便于以整年计算日期,防止过多分段计算带来的麻烦。

  由上可得,参考日期到给定日期的将分成两部分:PastYearsDays(过去的整数年天数)和PresentYearDays(指定年已流逝的天数);

  则可以得到,WholeDays = PastYearsDays + PresentYearDays。

  参考日期到指定日期经过的闰年数为:LeapYearsNum = [(Year - 1) / 4] - [(Year - 1) / 100] + [(Year - 1) / 400]。

  由上可得到,PastYearsDays =  (Year - 1) * 365 + LeapYearsNum。

  又因为(Year - 1) * 365 = (Year - 1) * ( 7 * 52 + 1) ≡ Year - 1 (mod 7),故可得到,PastYearsDays ≡Year - 1 + LeapYearsNum。

  因此,可得到WholeDays = PastYearsDays + PresentYearDays ≡ Year - 1 + LeapYearsNum + PresentYearDays  (mod 7)。

  接来下,我们开始计算PresentYearDays:

  下表分别展示了每个月的天数:

 
Month Jan. Feb. Mar. Fri. May. Jun. Jul. Aug. Sep Oct. Nov. Dec.
Days 31 28(29) 31 30 31 30 31 31 30 31 30 31

  又因为离28、29、30、31最近的7的倍数是28 = 4 * 7。故,当每个月的天数都减去28后可得如下表格:

 
Month Jan. Feb. Mar. Fri. May. Jun. Jul. Aug. Sep Oct. Nov. Dec.
LeftDays 3 0(1) 3 2 3 2 3 3 2 3 2 3
CommonYearAcc 3 3 6 8 11 13 16 19 21 24 26 29
LeapYearAcc 3 4 7 9 12 14 17 20 22 25 27 30

  除却1月和2月,3月-7月的剩余天数为:3,2,3,2,3;而8月-12月同样为:3,2,3,2,3。根据循环数列的规律(该理论将在另一博文中描述),可得到如下表述:

  当Month = 1时,PresentYearDays = Day;

  当Month = 2时,PresentYearDays = 31 + Day;

  当Month ≥ 3时,[13 * (Month  + 1) / 5] - 7 + (Month  - 1) * 28 + Day + i,其中,当闰年时i = 1,当平年时i = 0.

  

  将1月和2月当做上一年的“13月”和“14月”,最终上边三个公式将合并为以下公式:

  PresentYearDays  = [13 * (Month + 1) / 5] - 7 + (Month - 1) * 28 + Day         (3 ≤ Month ≤ 14)

  最终,WholeDays将变成:

  WholeDays ≡ (Year - 1) + [(Year - 1) / 4] - [(Year - 1) / 100] + [(Year - 1) / 400] + [13 * (Month +1) / 5 ] - 7 + (Month-1) * 28 + Day     (mod 7)

        ≡  (Year - 1) + [(Year - 1) / 4] - [(Year - 1) / 100] + [(Year - 1) / 400] + [13 * (Month +1) / 5 ] + Day                                     (mod 7)

  注意:当将1月和2月当做13月和14月计算的时候,年份也得减一。

  又因为每隔四个世纪,星期就重复一次。

  故我们可容易得到每个世纪的第一个月(3月1日)的计算公式:

  PastYearsDays‘ ≡ (4 - (Century - 1) mod 4) * 2 - 4            (mod 7)

  其中Century为给定时期所在的世纪。

  最终得到计算每隔世纪第一年的星期几的公式:

  WholeDays’ ≡  (4 - (Century - 1)  mod 4) * 2 - 1 + [13 * (Month + 1) / 5] + Day

  接下来,我们就要求在一个世纪内的任意天的星期。显然在一个世纪内,只需考虑能被4整除的年份即是闰年LeapYear,根据此规则,最终我们得到计算任意一天星期几的公式:

  WholeDays ≡ (4 - (Century - 1) mod 4) * 2 - 1 + (Year - 1) + [Year / 4] + [13 * (Month + 1) / 5] + Day

  假设C = Century - 1,根据数论知识,我们又可以得到如下公式:
  4q + r = C,其中q为商,r为余数。

  ⇒      r = C - 4 * [C / 4]

  ⇒      (4 - C mod 4) * 2 = (4 - C + 4 * [C / 4]) * 2 
                                            = 8 - 2C + 8 * [C / 4] 
                                            ≡ [C / 4] - 2C + 1         (mod 7)

  最终,由上式,我们可以得到

  WholeDay ≡ [C/4] - 2C + Year + [Year / 4] + [13 * (Month + 1) / 5] + Day - 1   (mod 7) 

  代码示例:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace TestOne
{
/// <summary>
/// Used to record Date.
/// </summary>
class LocalDate
{
public int m_nYear;
public int m_nMonth;
public int m_nDay; public LocalDate(int year, int month, int day)
{
m_nYear = year;
m_nMonth = month;
m_nDay = day;
} public LocalDate()
{
m_nYear = ;
m_nMonth = ;
m_nDay = ;
} /// <summary>
/// Used to calculate what day it is.
/// </summary>
/// <param name="localDate"></param>
/// <returns></returns>
public static int CalWhatDay(ref LocalDate localDate)
{
int year = localDate.m_nYear;
int month = localDate.m_nMonth;
int day = localDate.m_nDay;
if (month < )
{
year -= ;
month += ;
} int c = year / , y = year - * c;
int w = (c / ) - * c + y + (y / ) + ( * (month + ) / ) + day - ;
w = (w % + ) % ;
return w;
}
}
class Program
{
static void Main(string[] args)
{
LocalDate localDate = new LocalDate(, , );
int whatDay = LocalDate.CalWhatDay(ref localDate);
Console.WriteLine("Date: {0}-{1}-{2} is {3}", localDate.m_nYear, localDate.m_nMonth, localDate.m_nDay, PrintWahtDay(whatDay));
Console.ReadKey();
} private static string PrintWahtDay(int whatDay)
{
switch(whatDay)
{
case : return "Sunday";
case : return "Monday";
case : return "Tuesday";
case : return "Wednesday";
case : return "Thursday";
case : return "Friday";
case : return "Saturday";
default: return "Invalid number";
}
}
}
}

Code Here

上一篇:搭建LNAMP环境(五)- PHP7源码安装Redis和Redis拓展


下一篇:centos 7.2 安装mongodb 3.4.4免编译