Calendars play a very important role in our lives, and work, school and date are all inseparable from calendars. Every year the new year begins, people have to change the calendar, you want to know how many days in the next year is how to be determined? Why last year's National Day is Friday and this year's National day is Wednesday? Let's look at the calendar algorithm. This article describes the scheduling rules for calendars, determines how a day is calculated for the days of the week, and how to print a year's almanac on your computer.
To study the calendar algorithm, we must first know the calendar layout rules, that is, the calendar. The so-called calendar, refers to the calculation of the length of the year, month, day and the relationship between them, specify the law of Time series. China's official calendar is the Chinese Gregorian calendar, that is, the world's general Gregorian (Gregorian calendar), the Chinese Gregorian calendar year is divided into flat perennial and leap years, the usual year is 365 days, leap year is 366 days. The rules for determining whether a year is normal or leap years are as follows:
1, if the year is a multiple of 4, and is not a multiple of 100, it is leap years;
2. If the year is a multiple of 400, it is leap years;
3, not meet the 1, 2 conditions is the usual year.
summed up into a sentence is: Four years a leap, hundred years not a leap, 400 year again leap.
The rules of the Chinese Gregorian calendar for the month are such that the year is divided into 12 months, of which January, March, May, July, August, October and December are semi-rotary, and one months have 31 days. April, June, September and November are Xiao Yue, one months have 30 days. The February days are based on whether it is a leap year, if it is a leap years, February is 29 days, if it is normal, February is 28 days.
In addition to day and month, people in their daily life also defined a date another attribute, that is, the days of the week. Week is not a Gregorian calendar, but people have been accustomed to using weeks to manage and plan time, such as one weeks to work five days, a two-day break, and so on, the week's rules have completely changed people's living habits, so the week has become a part of the calendar. The name of the week originated in the ancient Babylonian culture. In the 7-6 century BC, the Babylonians used the week system, and every day for one weeks there was a god in charge. This rule later spread to ancient Rome, and gradually evolved into the present week system.
How do you know what day of the week? Besides looking up the calendar, is there a way to figure out what day of the week it is? The answer is yes, the week is not like years and months as a fixed calendar rules, but the calculation of the week also has its own rules. The week is a fixed 7-day cycle with a fixed order that does not affect the number of days of a leap year, a flat perennial, or a size month. So, as long as you know exactly what day of the week it is, you can figure out what the other date is. The calculation method is very simple, that is to calculate the number of days between two dates, with the difference of days to 7 to the remainder, the remainder is two days of the number of weeks difference. For example, if you already know that March 27, 1977 is Sunday, how do you find out March 27, 1978 is the day of the week? According to the previous method, calculated from March 27, 1977 to March 27, 1978 between the difference of 365 days, 365 divided by 7 is 1, so March 27, 1978 is Monday.
The key to calculating the week of the above method is to find the number of days between two dates. There are two commonly used methods for calculating the number of days between two dates, one using the rules of the Gregorian calendar for the month and year, and the other by using the Julian day calculation. Use the Gregorian rule to directly calculate the number of days between two dates. Simply put, the number of days separated by two dates is divided into three parts: the number of days remaining in the year before the previous date, the number of days that are included in the integer year between the two dates, and the number of days past the year in which the last date is located. If two dates are adjacent to two years, the second part of the year is 0. For example, from March 27, 1977 to May 31, 2005, the number of days remaining in 1977 was 279 days, the middle integer year was from 1978 to 2005 (excluding 2005), totalling 26 years, including 7 leap years and 20 ordinary year, totalling 9,862 days, The last 151 days were passed from January 1 to May 31 in 2005. Three summed up 10,292 days. The algorithm that uses the Gregorian rule to calculate the date difference days is implemented as follows (in order to simplify algorithmic complexity, this implementation assumes that the date used to locate the week is always before the date on which the day of the week is needed):
the int calculatedays (int ys, int ms, int ds, int ye, int me, int de) m = calcy
Earrestdays (Ys, MS, DS); 102 an if (Ys!= ye)/* Not the same year's date//The (Ye-ys) >= 2)/* The interval is more than one year,
To calculate the interval of the whole year * * * * days + = Calcyearsdays (ys + 1, ye);
108} 109 Days + = Calcyearpasseddays (Ye, Me, de);
112 {113 days = Days-calcyearrestdays (Ye, Me, de);
114} 116 return days; 117} 43/* Calculates the number of days in the past of the year, including the specified day/int calcyearpasseddays (int years, int month, int day) 45
{Passeddays int = 0;
the int i; for (i = 0; i < month-1 i++) (i = = passeddays + = Daysofmonth[i];
Passeddays = day;
if (Month > 2) && isleapyear (year) passeddays++;
Passeddays return;
59} 60 61/* Calculates the number of days remaining in a year, excluding the specified day/+ int calcyearrestdays (int year, int month, int day)
leftdays int = daysofmonth[month-1]-day;
the int i;
for (i = month I < monthes_for_year; i++) [leftdays = = Daysofmonth[i]; (Month <= 2) && isleapyear (year)) 73
leftdays++;
Leftdays return; 76} 77 78/* 79 calculates the number of days between January 1 of years year January 1 and Yeare year, 80 includes years year January 1, excluding yeare year January 1 Day I/km int calcyearsdays (int years, int yeare)
{0 int days =
int i; 90 for (i = years I < yeare i++) (Isleapyear (i))
Days + + days_of_leap_year;
Or else the days + = Days_of_normal_year;
The return of the days; 96}