About Chinese calendar algorithms-from others' blogs

Source: Internet
Author: User
Calendar description
 
This article summarizes and describes the algorithm of the cncalendar unit written in recent calendar studies.
It is still a draft and lacks the lunar calendar algorithm.

========================================================== ====================================
1. cncalendar Introduction
========================================================== ====================================

Cncalendar aims to provide an open source calendar computing toolkit for programmers at home and abroad, especially those in Delphi/C ++ builder, this package covers the calendar, and sunset, however, the complexity of the calendar and the uncertainty of astronomical phenomena make the calculation not perfect. In order to give users a complete instruction and help users solve the problem by reading the cncalendar code when they discover the problem, the following describes the calendar algorithm of cncalendar.

========================================================== ====================================
2. cncalendar Algorithm Description
========================================================== ====================================

This section describes the algorithms used by cncalendar.

----------------------------------------------------------------------
2.1 Introduction to concepts and common sense
----------------------------------------------------------------------

Before calendar calculation, some concepts need to be clarified first, including the concept of calendar, some astronomical terms, the definition of the year, month, day, and hour of the Gregorian calendar, the concepts involved in the cncalendar algorithm are described in this section. It is worth mentioning that some concepts are astronomical phenomena and some are derived from calendars. The latter is generally corrected according to the former. The accuracy of the fitting between the two is the basis for judging whether the calendar is accurate or not.

2.1.1 calendars, calendars, and calendars
------------------------

The Sun and the Moon are the two most influential celestial bodies in human production. Therefore, calendars are basically calculated based on the operating rules of day and (or) month, calendars calculated purely by the laws of the sun are called calendars, and those calculated purely by the moon are called calendars. The calendar of both is a calendar. The Gregorian calendar is a typical Gregorian calendar, the Gregorian calendar is a lunar calendar (not covered in this article), and the Chinese lunar calendar is a lunar calendar.

2.1.2 Rationale
----------------

There are two functions of calendars: accurate timing (or day counting) and actual conditions. The actual situation includes many things, such as seasonal changes, farming activities, and tidal fluctuations. The actual situation creates a calendar, which in turn should be able to deduce the actual situation according to its rules. This is the ultimate goal of the calendar. Currently, the general Gregorian calendar is the Gregorian calendar developed in 1582) on the basis of the 365-day and four-year-old calendar year, the general calendar is formed. The Gregorian calendar is based on numerical calculations. In other words, according to the numerical calculation rules of the Gregorian calendar, you can push any number of days forward to calculate the date of the current year, as to whether it is in line with the actual situation, you will only be able to proceed. Like the predecessor of the Gregorian calendar, the ruru experienced an accident ten days ahead of the spring equinox due to the accumulation of excellent computing errors. However, it is reassuring that the relative activities of the Sun and the Earth are relatively stable, however, the accuracy of the modified Gregorian calendar has improved a lot, it can support at least three thousand years (tens of thousands of years if a four thousand-year-old rule is added ). This also shows that, as long as the calendar is relatively fixed based on astronomical conditions, the calendar may be deduced into a simple numerical formula. This is exactly the case for the Gregorian calendar.
The Chinese lunar calendar month is based on the operating rules of the moon. The moon is much more unstable than the sun, while the lunar calendar is an astronomical calendar, the principle stipulates that the calendar is subject to the actual situation and supplemented by numerical calculations. As a result, it leads to multiple calendars in Chinese history and the complexity of the current calculation of the lunar calendar and the conversion of the Gregorian calendar. In addition, the lunar calendar must take into account the cooperation between the Sun cycle (the year of return) and the moon cycle (the lunar calendar month). In addition, the rule of the 19th year seven-year period has been created, the confirmation of the leap month must be based on the solar term, which greatly increases the computing complexity. This topic will be described in detail later.

2.1.3 spring equinox, year of return, lunar calendar
----------------------------

The earth is leaning around the sun, so there is a fixed angle between the red road surface and the yellow road surface. This angle leads to direct sun points moving between the north and south regression lines during the Earth's revolution, therefore, the four seasons have been replaced and the concept of the year has been formed. Note that the concept of the year is much longer than the discovery that the earth turns around the sun. The intuitive feeling of the year for the Ancients is the replacement of the four seasons and the harvest of the Spring and Autumn. This feeling has nothing to do with the revolution of the Earth. It is assumed that if the earth's light rotation is unfair and the sun moves back and forth in the direction perpendicular to the yellow track, it can also create a feeling of the year. -- The purpose of this explanation is to differentiate the concepts described below: stellar year and return year.
The time when the Earth accurately rotates around the sun for a week is called the year of the constant star, while the precise time of four seasons is called the year of return. So how can we measure the precise time of four seasons? This requires the concept of Spring Equinox. The Spring Equinox is an astronomical concept. It is one of the two intersections of the earth's public orbit and the equator on the yellow track surface (the other is naturally the autumn equinox). When the spring equinox is the autumn equinox, the sun is directly at the equator, the direct sunlight meets the requirements of Four Seasons. Therefore, the definition of the year of return is the time when the earth returns to the spring equinox from this spring equinox.
If the spring equinox does not change, the year of the constant star equals to the year of return, because the earth turns around and returns again. Unfortunately, the ground axis is always as active and dynamic as the gyro, so the resulting year gap will make the spring equinox a little forward in a year, so the year of a constant star is a little longer than the year of return, one is the 1st day and the other is the 1st day. Remember, returning to the year is the year in our general concept.
Some people may ask, if there is a gap of more than 20 minutes between the stars and the year of return, what will happen if the accumulation continues? The answer is "It has nothing to do with calendar ". In the calendar, no matter where the Earth is in a public-to-Earth orbit, the spring equinox and the autumn equinox are equally divided. The winter solstice and the summer solstice are also irrelevant to the recent day and the distant day of the public-to-Earth orbit, therefore, it is the case for astronomy to care about the gap between the stars and the year of return. The foundation of the Sun Calendar is to return to the Year.
As for the lunar calendar year, there is no astronomical significance, just a collection of months. However, in the lunar calendar, it also has to fit the length of the year. One farmer's calendar year is from Lunar January 1 to lunar January 1 of the following year. It may contain 12 or 13 months, depending on the setting of a leap year.
There is also the concept of "Age" in the lunar calendar, which is the period from the winter solstice to the winter solstice of the following year, which is equal to a year of return. This concept is used to calculate a leap month.

2.1.4 lunar calendar month
--------------------

There is no astronomical phenomenon corresponding to the Gregorian calendar month. The setting of the Gregorian calendar month is only to facilitate the division of the year, so the date of the month can be adjusted to a smaller value in the time of the Confucian calendar, you only need to keep the days of the year unchanged. Such issues as the August S and February s are historical issues.
The lunar calendar month is different. The lunar calendar month is a lunar calendar month, reflecting the change of the moon, that is, the cycle in which the moon rotates around the Earth for one week. The Lunar Calendar specifies the day when the moon is the first day of the lunar month, and the first day of the next month when the moon is the first day of the next month. Because the cycle of the moon is almost 29.5 days, but the month must be an integer multiple of the days, there are large and small lunar months, 30 days of the big month, 29 days of the small month, there are also irregular intervals, that is, the size of a calendar month cannot be calculated without reference to the month and the year. This is different from that of the Gregorian calendar.

2.1.5, Sunday
------------------

Day is the day-and-night alternation of the Earth's rotation. Time, minute, and second are born of it. In the past, it was defined as a real sun day, that is, the time interval between the Sun's point of view measured on the ground and a week round the earth and returning to the top of the globe, this interval is a little different from the exact one-week cycle of the Earth (stellar Day), because the earth is still in public rotation and its location is a little changed. Its principle is similar to the regression year and the stellar year gap caused by advances in the spring equinox. In addition, the yellow-red angle of the Earth and the uneven public rotation speed on the elliptical orbit also lead to a difference in the length of the real sun in a year. To eliminate this difference, the concept of flat sun is introduced in the calendar. In the calendar, "flat" refers to the mean of uniform time distribution, while the flat sun is an imaginary sun that moves around the Earth at an even speed on the celestial sphere equator, the exact value of its velocity is the average speed of the real sun in a regression year, and the real sun goes through the near-Earth distant location at the same time. The time when the normal sun returns to the top of the sky in a week is defined as a solar day, that is, the concept of day. In other words, the rotation speed distribution and public rotation of the earth within one year jointly determine the actual size of a solar day.
The hour, minute, and second are derived from the definition of the normal sun day: the 1 normal sun day is divided into 24 hours, the 1 hour is divided into 60 minutes, and the 1 minute is divided into 60 seconds. Later, due to the uneven rotation of the earth, the accuracy of the normal sun is not high, and it is the future to set the length of the second by other more precise means.

2.1.6 solar term
----------

The solar term is a description of 24 seasonal equal points on the Earth's public transit orbit during the year. The 24 solar terms will not be repeated here, there is also a saying in this "Equality": in ancient China, pingqi was adopted, and the time was equivalent to one year, with a fixed interval between two adjacent gases. After the Qing Dynasty, change to dingqi, and the period of the week is 24, every 15 ° is a solar term, so that the time interval of the adjacent solar term will be different because of the change of the earth's public revolution speed.
24 solar terms include 12 solar terms and 12 medium gas, odd number is solar terms, exist in the first day of each calendar month, even number is moderate, exist in the next day of the calendar month. Zhongqi plays a decisive role in the calculation of the lunar month.
The solar term calculation in this article is the same as that in the Chinese modern lunar calendar. However, in ancient times, the intersection time and the time pushed by the gas calculation have a large error, so pay attention to the calculation.

2.1.7
------------------------

China uses the daily dry land support method to calculate the daily, ten days of 12 days of dry land support cycle with 60 flowers and then round-robin, there is a corresponding dry support with the day of the year. The "eight words" are commonly used to calculate the annual and daily support. Of course, the eight words in this Article refer to the calculation of the number of dry support based on the year, month, and day, rather than the calculation of the four funds based on the calculation of the lucky fortune, that is no longer part of the scope discussed in this article.

----------------------------------------------------------------------
3.1 calendar algorithm Classification
----------------------------------------------------------------------

Before describing an algorithm, we need to explain some conventional statements:

* Year n bc indicates year-N, and year 0 does not exist.
* The 10 days from January 1, October 5-14, 1582 AD do not exist.
(Removed by Gregory), but the week and dry support are still continuous.
* The calculated digital descriptions of trunk, Zodiac, constellation, and solar term start with 0.
* Return to the nth day of the year, when the nth day of the current year is the day when the ninth day of the current year represents Xiao Han,
There are no 0th statements.

The current calendar algorithms of cncalendar include:

* Week
* Calendar year, month, and day
* Solar calendar interchange time
* Constellation
* Computing of sunrise and sunset time

Next we will briefly describe each algorithm separately. For more information about the implementation, see the code in cncalendar.

3.1.1 computing week
--------------

Cncalendar provides a method for calculating the day of the week based on the calendar year, month, and day. The algorithm is derived from the remainder method of the number of days in the new calendar book (with slight modifications). The principle is very simple: the seven-day arrangement of a week is unrelated to the specific calendar, and is calculated cyclically based on the number of days. Therefore, you only need to know the number of days of a calendar year, month, and day from a reference day and the number of weeks of the reference day, you can calculate the day of the week in the calendar year, month, and day by using the 7 remainder method.
In the algorithm, the number of days from January 1, January 0 (January 1, December 31, BC) is equal to the sum of the two parts: Base Year Number of days and leap year Corrected number of days. The former is equal to the year difference of the first year of the current year multiplied by 365 plus the number of days from the current day of the current year to the second day of the current month (including the day itself, the additional day of each leap year is also included ). If the latter is equal to the year difference, the remainder is obtained for 4 and then a correction value is subtracted.
The year difference is the actual year gap between the current year and the first year of the current year. When n is the right time, the year difference between N and the first year of the current year is n-1, the year difference in the n-year BC is n (because there is no 0-year AD ).
The modified value is the sum of the date adjustment during the switching period of the rualli and the gregalli calendars, and the adjustment of the rules for four hundred years and years without renewal. The value rules are as follows:

* If the date is before January 1, October 5, 1582 (including the 5th day), it is 0.
* The date is 10 on January 1.
* Increase by 1 since January 1, 1701, and increase by 1 for every century,
However, the century that can be divisible by 400 does not accumulate. For example, the value after New Year's Day in 1801 is 12,
The value after the New Year's Day in is 13, and the value after the New Year's Day in is still 13.
It was only 14 after New Year's Day in 2101.

After calculating the actual date, subtract 2 and then perform the remainder operation on 7. The resulting result is the day of the week. 0 corresponds to Sunday, 1 corresponds to Monday, and so on. Here, we subtract 2 because January 2 is Sunday (Friday on day 0, so-2 can also be replaced by + 5 ).

3.1.2 Calculation
--------------

The principle of calculating the annual, monthly, and daily support is similar to that of the computing week. Because of its continuity, the remainder method can be used for calculation. However, the concept of year and month used for calculation in China is different from that in the Gregorian calendar. The year is based on spring, and the month is based on 12 solar terms. The year and month of the previous Gregorian calendar must be converted to the year of the traditional Chinese calendar.

3.1.2.1 annual support

The calculation of the annual support requires three parameters: calendar year, month, and day. If the parameter is not set, the calculation is regarded as a date after the beginning of the spring. Therefore, the year does not need to be adjusted. If the month and day parameters are available, determine whether they are before the time of the Spring Festival of this year. The previous time is last year, and the year is reduced by one. After the year is converted, if it is year, if it is greater than 0, the number of dry branches is (ayear-1-3) mod 60, where year-1 is the year difference from year 1, if year is negative, it is adjusted to (ayear + 1-1-3) mod 60 Based on 0 years without a. D., This-3 is derived from the year of, however, Xin you's 0 ~ Ranking 57 in 59.

3.1.2.2 monthly support

Although the number of dry branches in a calendar month can be calculated using the remainder method, the relationship between the number of months in a year and the number of dry ends in a day is also relatively simple, therefore, there is a simpler algorithm (derived from the book "New Calendar" and similar articles on the Internet ). First, you still need to adjust the number of months and months based on the current spring and the time of delivery of various festivals, and then calculate the number of jobs in the current year. The number of jobs in the year corresponds to the number of jobs in the first month of the current year, here, the lengthy statement is simplified and written in comments. The code is described as follows:

Case Gan of // calculate the number of jobs in the first month of the current year based on the formula
0, 5: // beginning with,
Result: = 2;
: // The Head of the EIP,
Result: = 4;
2, 7: // start with Bing xinxun Geng,
Result: = 6;
3, 8: // The Ding Yi bit stream,
Result: = 8;
4, 9: // wujijia
Result: = 0;
End;

The current lunar calendar year lunar January is called Yin. After obtaining the number of dry branches in the first month, you can roll back the number of dry branches in the current month.

3.1.2.2 daily support

There is a similarity between the daily and weekly calculation. After calculating the number of days in the week, add 12 to 60 to get the remaining number of the day, 12 because in, it was the day of the birth, and in, the birth of the child was 0 ~ Ranking 12 in 59.

3.1.2.3

24 hours a day corresponds to 12 hours, and the hour corresponds to the dry branch, but the hour Division does not strictly correspond to the day division. When the first day is 23 hours to the current day, when the first day to the third day is ugly, and so on.
The calculation of the hourly and monthly daily support is similar. First, the hourly cost is converted to the hourly rate. If the calculation is 23 hours later, the date is adjusted to the next day. Then, calculate the number of dry jobs in the current day. The number of dry jobs in the current day has a simple correspondence with the number of dry jobs in the current day. However, this time, we did not find a remark, probably because the correspondence is relatively simple. The code for this correspondence is described as follows:

Case Gan
0, 5:
Result: = 0;
1, 6:
Result: = 2;
2, 7:
Result: = 4;
3, 8:
Result: = 6;
4, 9:
Result: = 8;
End;

It can be simplified:

If Gan> 4 then
Dec (Gan, 4 );
Gan: = 2 * Gan;

So you don't have to worry about it.
After obtaining the number of workers, You Can sequentially calculate the number of workers in the current day.

3.1.3 computing solar terms
--------------

Although the definition of the solar term is simple, it is not easy to calculate a more accurate submission time, because the spring equinox will be slightly moved during a regression year, therefore, the time between the two solar points is not exactly a year, so it is impossible to directly calculate the time of each solar cycle by 365.2422 days. The cncalendar algorithm is transplanted from the solar saving algorithm that the author fits based on the previous bright book in the Chinese calendar class, taking into account the inching And inching of the Earth's rotation, therefore, the accuracy of calm air can reach about 10 minutes, according to the author.

3.1.4 computing Constellation
--------------

Constellation calculation is relatively simple. The corresponding values of the Zodiac are determined based on the day of the month. The rules are as follows:

01.20-02.18: Aquarius
02.19-03.20: Pisces
03.21-04.20: aries
04.21-05.20: Taurus
05.21-06.21: Gemini
06.22-07.22: Cancer
07.23-08.22: Leo
08.23-09.22: Virgo
09.23-10.22: Libra
10.23-11.22: Scorpio
11.23-12.21: Sagittarius
12.22-01.19: Capricorn

However, the Zodiac palace begins with Aries, So 0 in the constellation number returned in the Code corresponds to Aries, and water bottle Pisces are 10 and 11 respectively.
In addition, there are different sayings about constellation names, such as Capricorn, goat, water bottle, and baby bottle, and Sagittarius, as well as Libra, which are commonly used here.

3.1.5 calculate the lunar calendar
--------------

3.1.5.1 detailed lunar calendar rules

The Chinese lunar calendar was calculated based on the longitude of 1929 ° 25' in Beijing before December 31, 116, and then based on the Beijing time, that is, the UTC + 8 standard time of 120 °. There are four basic rules for the lunar calendar:

1. The date of the time when the month ends is the first day of the lunar calendar month.
2. For example, if a certain gas Festival falls within the first day of the first day (0 to 24 hours ago ),
Even if the submission time is earlier than the time of the month, it is included in this month.
3. the winter solstice must appear within lunar November.
4. If there are 13 months in the adjacent period of the winter solstice (within the age), 12 are complete.
The year of the lunar month, the year of the year is called the lunar age, and one month in the year needs to be set to a leap month.
Because there must be 12 moderate gases during the period, at least one lunar month does not fall into its
In, the first month in the middle of the age is set to a leap month, and the number of months is the same as that in the previous month.

For the fourth rule, it should be emphasized that the concepts of the year and the year are different. If two lunar months in the age are not stingy, the first one is a leap month, this leap month may not fall into this year. However, if the year old has a lunar month, but the year old has only 12 months, that is, 11 full lunar months, that month will not be left empty. A typical example is that in August 2033, November: 2033 lunar calendar years, 8th lunar calendar months and 11th lunar calendar months are not in the air, but because of the 2032 lunar calendar year winter solstice (November) to the 2033 lunar calendar year winter solstice (November) there are only 11 full lunar months between, so it is not in the middle of the year, and you do not need to join the leap month. There are 12 full lunar months between the winter solstice (2033) and the winter solstice (November) in. Therefore, it is in the middle of the year, and the first non-moderate lunar month that appears after the winter solstice is a leap month, it is exactly the next lunar month in November, so it will begin in November.
In a word, "no" is just a required condition for a leap month, not a sufficient condition. Only when there are too many months in the Age of years that need to be renewed for a month, will the first "no" Middle "month be searched. The old calendar uses the 8th lunar month of the first calendar year of the year as the lunar month of the first calendar year of the year. It does not take into account whether the calendar year is the year old. The new version has been changed to April November.
In addition, due to the current solar term, the earth's movement speed is slow at a distance from Sunday, resulting in a delay in the winter than in the summer, so most of the nose Months fall before and after the summer, in the past few hundred years, the year December has rarely appeared, which basically guarantees the uniqueness of the Spring Festival. The essence of the lunar calendar is evident.
Note: The Chinese modern calendar is based on the content published by the Nanjing Zijinshan Observatory. If this article conflicts with this Authority, the Authority shall prevail.

3.1.5.2 lunar calendar computing

According to the above lunar calendar rules, the lunar calendar is completely an astronomical calendar, and the moon movement is unstable, making it difficult to accurately calculate it. At present, the authoritative calculation method is to establish mathematical models for the operation of the Moon, Earth, and Sun Based on astronomical observation data to accurately calculate the time periods between the sun and the gas and then calculate the calendar.
(To be supplemented)

3.1.6 calculate the sum of nine and three volts
--------------------

The ninth day, also known as the ninth day, is the 81 days starting from the winter solstice. every nine days is called the ninth day, and the ninth day is called the first day. When calculating the 9th digit of a calendar year, month, and day in cncalendar, the winter solstice day of the current year is first calculated. If the day is within the 81 days after the start of the winter solstice, it indicates that within the 9th day, the remainder of the Division can be used to find the ninth day. If this day falls before the winter solstice day of the current day, check whether it is in the 81 days after the winter solstice day of the previous year. If yes, it is calculated again, because the 9th day usually spans the junction of two Gregorian years.
The day-to-day computing is a little complicated. Traditionally, the first day of the third day after the summer solstice (tiangan) is the first day, and the fourth day after the summer solstice is the middle day, if the Fifth Geng sunset falls before the beginning of autumn, it will be considered 20 days. Otherwise, the 10 days from the first Geng day after the beginning of autumn will be the last day. In addition, it is not included in the calculation of the period from the summer to the beginning of autumn. That is to say, if the period from the summer to the Japanese Day is the day, the last 10 days will be counted as the first day.

3.1.7 Calculation of meitaomei

The period of plum blossom and plum blossom indicates the beginning and end of the period of plum blossom and plum blossom in the south of the Yangtze River ), the sunrise refers to the first day before Sunday (Geographic Support) after slight heat. Similar to the above, when calculating the daily number of dry earth branches, the mountain and slight heat are not counted.

3.1.8 calculate sunrise and sunset
------------------

This part of the algorithm is transplanted from the "time science website" and cannot be elaborated here.

========================================================== ====================================
3. References and acknowledgment
========================================================== ====================================

The development of cncalendar mainly draws on the following content and pays tribute to the author's selfless dedication.

* The New Calendar is about a hundred years old, including calendar instructions and calculations.
* "" Calendar source code and "Chinese calendar class" source code, written in Javascript,
Since then, the solar saving algorithm has been transplanted.
* For more information about the calendar JavaScript source code, see.
* The "Sunshine Calendar" 5.2 Beta version is used to check some of the calculation results.
* "Time science site": http://www.time.ac.cn,
The sunrise and sunset computing code has been transplanted since then.

 
I used to think that the algorithm for converting the Gregorian calendar to the lunar calendar may not be difficult. Today, a netizen gave me a piece of code to show it, and I was dizzy, So that I realized the complexity of the lunar calendar algorithm, then I added the code from this user to my favorites. For future use
It's really scary. There's no deep mathematical knowledge, and there's no way to write complete algorithms. Chinese culture is really profound and profound.
Http://www.ainlife.cn/uploads/200605/10_233851_.rar

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.