Algorithm series (17) Calendar generation algorithm-China Gregorian calendar (Gregorian) (next)

Source: Internet
Author: User
Tags time and date

"Take the Last Piece"

Although the method of calculating the week is simple, it is not very convenient to calculate the time difference of two dates. Would it be better if there was a formula that could calculate the corresponding week directly from the date? Fortunately, such a formula exists. The derivation principle of this kind of formula still is to calculate the week by the time difference of two dates, only by choosing a special date to simplify the derivation of the formula. This so-called special date refers to the day of December 31 of the year, which coincides with the Sunday. There are two advantages to choosing a day like this, one of which is to omit the remaining days of the year from the calculation of the standard date, and the other is to calculate the date difference for the remainder of the week and not to calculate the difference of the week. People know the year of the Year of January 1 is Monday, then BC 1 December 31 is Sunday, with this day as a standard date, you can only calculate the integer years of time and date of the cumulative number of days, this week formula is:

W = (L * 366 + N * 365 + D)% 7 (Formula 2)

The L in the formula is the number of leap years between the year from year one to the Y and D days, and N is the average number of years, and D is the cumulative number of days in y years. Bring the whole year y-1 = L + n into the upper type, you can get:

W = ((y-1) * 365 + L + D)% 7 (Formula 3)

According to the Law of Leap Year, the number of leap years from BC to Y can be calculated, namely:

Take L into Formula 2 and get the final formula for the week W:

Also taking May 31, 2005 as an example, the value of W is calculated using Formula 5:

The May 31, 2005 was Tuesday, consistent with the results of the previous calculation. Based on the above analysis, we can write the algorithm using Formula 5 to compute the week:

146 int Totalweek (int month, int day)   
       
147 {   
       
148     int d = calcyearpasseddays (year, month, day);   
       
149     int y = year-1;     int w = y * days_of_normal_year + Y/4-y/100 + y/400 + D;   
       
151    
       
152 return     W% 7;   
       
153}

The problem with formula 5 is that the amount of computation is not conducive to Multi-digit week results. So people continue to deduce simpler formulas on the basis of formula 5. The German mathematician Christian Caille (Christian Zeller, 1822-1899) deduced the famous formula for Caille (Zeller) in 1886:

By dividing the calculated W value by 7, the remainder is the day of the week, or Sunday if the remainder is 0. The meanings of the symbols in the Caille formula are as follows:

W: Week;

C: Century number of values of –1, such as 21st century, then = 20;

M: The number of months, the value is greater than or equal to 3, less than or equal to 14. In the Caille formula, January and February of a certain year are considered as the 1 March and 1 April of the previous year, for example, February 1, 2001 is counted as 1 April 1 of 2000 years;

Y: Year, after the two-digit anniversary, such as 1998, = 98, 2001, = 1;

D: Number of days in a month

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.