Traditional Culture Encyclopedia - Almanac inquiry - How to Convert Gregorian Calendar into Lunar Calendar in c++
How to Convert Gregorian Calendar into Lunar Calendar in c++
1. Algorithm for Converting Gregorian Calendar into Lunar Calendar
The conversion between Gregorian calendar and China lunar calendar is not a simple formula, but more complicated, because the formula of the lunar calendar is quite complicated, and it is specified according to astronomical observation.
The common and simple algorithm for converting Gregorian calendar into lunar calendar is table lookup. First of all, we should save the conversion information of Gregorian calendar lunar calendar: starting from any year, we should save the lunar calendar information of several years starting from this year (starting from 1900 in my C++ class). The idea of choosing a starting point is very important, which is also reflected in the following disciplines and 24 solar terms. Think back, how do we usually convert the Gregorian calendar into the lunar calendar? It refers to the annual calendar, which includes the solar calendar and the lunar calendar every day. Just check it directly. So can we do the same thing? Sure, but it takes a lot of work to include daily information, so we should simplify this information. How to simplify?
To save information for one year, you actually only need two pieces of information: (1) the size of each month in the lunar calendar; (2) Whether there is a leap month, the month of leap month and the size of leap month this year. It is enough to use an integer to store this information. The specific method is to use a digit to represent the size of a month, where the big month is 1 and the small month is 0, so use the digit 12, and then use the lower four digits to represent the leap month, and if there is no leap month, it is 0. For example, the information data in 2000 is 0x0c960, which is converted into binary 11001100000, which is12,5,8,65400. The lunar calendar information data of 200 1 year is 0x0d954, where 4 represents leap April of that year, and the month size information is 0x0d95, specifically, 1, 2, 4, 5, 8, 10, 12 months old, and the rest months are small months. In this way, arrays can be used to store this information. In my C++ class, I use the array m_lunarInfo to store this information.
The following is the specific algorithm for converting Gregorian calendar into lunar calendar:
(1) Calculates the number of days from the required time to the first day of the first month of the starting year.
(2) From the beginning of the year, subtract the number of days in each month until there are no more days in the next month. At this point, how many years has the subscript of m_lunarInfo been subtracted? Add this subscript to the starting year to get the lunar year, and then see how many months have been subtracted. If there is no leap month this year or the leap month is still behind, you can get the lunar month directly. If it is a month after leap month, this month is leap month. The rest of the days are lunar calendar days. (The specific implementation has been improved. )
2. Taylor formula helps to calculate the week skillfully.
In applied mathematics, there is a formula to calculate the day of the week, which is Taylor formula. The formula is as follows: w = [c ÷ 4]-2c+y+[y ÷ 4]+[26 (m+1) ÷1]+d-1,where w is the number of weeks of the request date. If the number is greater than (less than) 7, subtract (add) multiples of 7 until the remainder is less than 7. Where c refers to the first two digits of the year, y refers to the last two digits, m refers to the number of months, and d refers to the number of days. The square brackets [] indicate that the figures in brackets are rounded. It is also important to note that if the month is 1 or February, it should be regarded as 13 or 14 of the previous year, that is, the range of m in the formula is from 3 to 14, not from 1 to 12.
3. Discipline of cadres and party branches
There are ten heavenly stems, namely, A, B, C, D, E, Ji, G, Xin, Man and Ghost, and twelve earthly branches, namely, Zi, Ugly, Yin, Mao, Chen, Si, Wu, Wei, Shen, You, Xu and Hai. The ancients matched them in a certain order without repetition. There were 60 pairs from Jiazi to Guihai, which were called Sixty Jiazi. The ancient people in China used these 60 pairs of branches to represent the serial numbers of years, months, days and hours, which went on and on, and this is the chronology of branches and branches.
0 jiazi
1 ugly B.
2 bingyin
3 ding Mao
4 Chen Wu
Season 5
6 Wu Geng
7 Xin Wei
8 Ren Shen
Gui You No.9 10 JOE
1 1 yihai
12 propion
13 Ding Chou
14 pentatonic
15 Trade Fair
16 Chen Geng
17 symplectic set
18 room
19 Shen Jia Guiwei 20
2 1 yiyou
22 Xu Bing
23 Dinghai
24 abital
25 ugly
26 Geng Yin
27 Xinmao
28 Renchen
Wu Jia on the 29th and 30th.
3 1 B Wei
32 Shen Bing
33 Ding You
34 reform movement
35 jihai
36 gengzi
37 Xin Chou
38 voices
39 Gui Mao 40 Chen Jia
4 1 B Yes
42 Wu Bing
43 Ding Wei
44 wushen
45 Ji You
46 Geng Xu
47 Xinhai
48 Renzi
49 guichou 50 Jiayin
5 1 Mao Yi
52 Chen Bing
53 butyl
54 55
55 years old. No.
56 Geng Shen
57 Xin you
58 Ren Xu
The representation principle of year, month and day is the same, that is, the number of years, months and days from year, month and day to the starting year, month and day, plus the serial number of the starting year, month and day in the above table, the serial number of the main branch is modulo 10, and the serial number of the main branch is modulo 12. Take the year as an example, beginning of spring after 1900 is the year of boxer. As can be seen from the above table, the year of Gengzi is the 36th year in the 1960s, so the number of years from m_year to 1900 is m_year- 1900+36- 1 (if the month is m_month, we can get the representation of month and day in the same way. Finally, according to the solar terms (beginning of spring), we should adjust February of the calendar year. According to the date of the first solar term of each month, the monthly expenditure statement is adjusted, and the specific situation is detailed in the source program.
The ancients divided a day and night into twelve hours according to the time when the sun rose, and used the twelve earthly branches as passwords. If m_hour is used to represent the current hours of the Gregorian calendar (twenty-four notation), (m _ hour+ 1)/2 will get the earthly branch representation of time (actually the serial number of earthly branches).
4. 24 solar terms
In essence, solar terms belong to the solar calendar. From the astronomical point of view, the 24 solar terms are divided into 24 equal parts according to the 360 degrees of the earth's orbit around the sun (ecliptic), with vernal equinox as the zero point. The two bisectors are separated by 15 degrees, and each bisector has a proper name, which contains the significance of climate change, phenological characteristics and crop growth. The twenty-four solar terms are beginning of spring, Rain, Surprise, Spring Equinox, Qingming, Grain Rain, Changxia, Xiaoman, Busy, Summer Solstice, Slight Summer, Great Summer, beginning of autumn, Chushu, Bailu, Autumn Equinox, Cold Dew, First Frost, beginning of winter, Light Snow, Winter Solstice, Slight Cold and Great Cold. In the above order, all singles are. Now it is generally called twenty-four solar terms.
Since ancient times, solar terms have been calculated by dividing a year into 24 equal solar terms. In the old calendar, 24 Qi is calculated by the method of leveling Qi, which divides a tropical year into 24 equal parts, and arranges solar terms and neutral Qi in turn at equal intervals from winter solstice, so the degree of the sun running in Qi is not equal. The new calendar is calculated by the method of constant air, which divides the Sunday on the ecliptic into 24 parts. When the sun moves to a certain vernal equinox, it is set as the date of a solar term. Because the apparent motion of the sun is uneven, the time interval between solar terms is also unequal. However, regardless of the new calendar or the old calendar, the winter solstice is the first of the 24 qi, so the difference between them is the biggest in the spring and autumn equinox. Why do traditional chroniclers in China know how to calculate the gas, but they are used to recording calendars with the gas? This is mainly because the average gas is easy to calculate, because under this method, the interval between each solar term and solar term or between gas and gas is fixed at 30 days, 5 hours and 2 quarters.
In this C++ class, the calculation of 24 solar terms is realized by using a simple average solar term algorithm. The array sTermInfo is used to record the minutes of each solar term from slight cold, and the function intstern (int y, int n) is used to get the date of the month in which the nth solar term is located (n counts from 0 slight cold). Because precession and nutation are not considered, the accuracy is not very high.
- Previous article:What's so special about the full moon?
- Next article:Calculation date conflicts with Gregorian calendar.
- Related articles
- 1998 65438+ Gregorian calendar February 10
- Is February 30, 2023 suitable for business? March 2, 20231Is it ok to do business?
- Can I use other software for my little love speaker?
- Authentic Lao Huang Li Wang 202 1
- What is the symbolic significance of the zodiac, the auspicious day of the zodiac and the empty Liuhe Tang Dynasty?
- The old yellow calendar is a good day to cure diseases.
- In which year is April 15th of the lunar calendar the first day of June in the solar calendar?
- What authoritative encyclopedias are there in the old almanac?
- 202 1 10 April is an auspicious day for engagement, and 202110 which day is suitable for marriage?
- Choose the wrong day to decorate, what can be done to make up?