- unsigned char calendar_calculate_lunar_month_total_day(void)
- {
- unsigned char mc_tpumenus_temp_01;
- unsigned char mc_tpumenus_temp_02;
- if(gc_lunar_calendar_month < 9)
- {
- mc_tpumenus_temp_01 = lunar_calendar_month_table[2*gc_lunar_calendar_year];
- mc_tpumenus_temp_02 = gc_lunar_calendar_month - 1;
- }
- else
- {
- mc_tpumenus_temp_01 = lunar_calendar_month_table[2*gc_lunar_calendar_year + 1];
- mc_tpumenus_temp_02 = gc_lunar_calendar_month - 9;
- }
- if((mc_tpumenus_temp_01>> mc_tpumenus_temp_02)&0x01)
- return(29);
- else
- return(30);
- }
阳历日和农历节气的对应关系
压缩节气数据表:
根据规律可以得到四个数据表(每个阳历月有两个节气,每个节气需要两个数据表):
- const unsigned char calendar_solar_term_table_01[12][33] = {
- {7,6,6,6,6,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,5,5,5,5,5,4,5,5}, //month 1
- {5,4,5,5,5,4,4,5,5,4,4,4,4,4,4,4,4,3,4,4,4,3,3,4,4,3,3,3}, //2
- {6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5}, //3
- {5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,4,4,5,5,4,4,4,5,4,4,4,4,5}, //4
- {6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5}, //5
- {6,6,7,7,6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5},//6
- {7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,6,6,6,7,7}, //7
- {8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,7},//8
- {8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,7}, //9
- {9,9,9,9,8,9,9,9,8,8,9,9,8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,8}, //10
- {8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,7}, //11
- {7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,6,6,6,7,7}, //12
- }; //这个数据表表示了每个月第一个节气出现的规律
- const unsigned char calendar_solar_term_year_01[12][9] = {
- {13,49,85,117,149,185,201,250,250}, //month 1
- {13,45,81,117,149,185,201,250,250}, //2
- {13,48,84,112,148,184,200,201,250}, //3
- {13,45,76,108,140,172,200,201,250}, //4
- {13,44,72,104,132,168,200,201,250}, //5
- {5 ,33,68,96 ,124,152,188,200,201}, //6
- {29,57,85,120,148,176,200,201,250}, //7
- {13,48,76,104,132,168,196,200,201}, //8
- {25,60,88,120,148,184,200,201,250}, //9
- {16,44,76,108,144,172,200,201,250}, //10
- {28,60,92,124,160,192,200,201,250}, //11
- {17,53,85,124,156,188,200,201,250}, //12
- }; //这个数据表表示了每个月第一个节气出现规律对应的阳历年份范围
- const unsigned char calendar_solar_term_table_02[12][29] = {
- {21,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20,20,20,20,20,20,19,20,20,20,19,19},
- {20,19,19,20,20,19,19,19,19,19,19,19,19,18,19,19,19,18,18,19,19,18,18,18,18,18,18},
- {21,21,21,22,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20,21,20,20,20,20,19,20,20},
- {20,21,21,21,20,20,21,21,20,20,20,21,20,20,20,20,19,20,20,20,19,19,20,20,19,19,19},
- {21,22,22,22,21,21,22,22,21,21,21,22,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20},
- {22,22,22,22,21,22,22,22,21,21,22,22,21,21,21,22,21,21,21,21,20,21,21,21,20,20,21},
- {23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22,22,23,22,22,22},
- {23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22,22},
- {23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22,22},
- {24,24,24,24,23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23},
- {23,23,23,23,22,23,23,23,22,22,23,23,22,22,22,23,22,22,22,22,21,22,22,22,21,21,22},
- {22,22,23,23,22,22,22,23,22,22,22,22,21,22,22,22,21,21,22,22,21,21,21,22,21,21,21},
- }; //这个数据表表示了每个月第二个节气出现的规律
- const unsigned char calendar_solar_term_year_02[12][8] = {
- {13,45,81,113,149,185,201},{21,57,93,125,161,193,201},{21,56,88,120,152,188,200,20},
- {21,49,81,116,144,176,200,201},{17,49,77,112,140,168,200,201},
- {28,60,88,116,148,180,200,201},{25,53,84,112,144,172,200,201},
- {29,57,89,120,148,180,200,201},{17,45,73,108,140,168,200,201},
- {28,60,92,124,160,192,200,201},{16,44,80,112,148,180,200,201},
- {17,53,88,120,156,188,200,201},
- };
- //这个数据表表示了每个月第二个节气出现规律对应的阳历年份范围
每个阳历月对应的两个农历节气出现的日期,可根据条件规律算法,分别由以下两个函数实现
- unsigned char calendar_calculate_solar_term_1(void)
- {
- zpage unsigned char done_index;
- zpage unsigned char solar_term;
- done_index = 0;
- while(gc_solar_calendar_year >= calendar_solar_term_year_01[gc_solar_calendar_mon
- [done_index])
- {
- done_index++;
- }
- solar_term = calendar_solar_term_table_01[gc_solar_calendar_month - 1][4*done_ind
- gc_solar_calendar_year%4];
- if((gc_solar_calendar_year == 121)&&(gc_solar_calendar_month == 4))
- solar_term = 5;
- if((gc_solar_calendar_year == 132)&&(gc_solar_calendar_month == 4))
- solar_term = 5;
- if((gc_solar_calendar_year == 194)&&(gc_solar_calendar_month == 6))
- solar_term = 6;
- return(solar_term);
- } //计算阳历月对应的第一个节气
- unsigned char calendar_calculate_solar_term_2(void)
- {
- zpage unsigned char done_index;
- zpage unsigned char solar_term;
- done_index = 0;
- while(gc_solar_calendar_year >= calendar_solar_term_year_02[gc_solar_calendar_mon
- [done_index]){done_index++;}
- solar_term = calendar_solar_term_table_02[gc_solar_calendar_month - 1][4*done_ind
- gc_solar_calendar_year%4];
- if((gc_solar_calendar_year == 171)&&(gc_solar_calendar_month == 3))
- solar_term = 2;
- if((gc_solar_calendar_year == 181)&&(gc_solar_calendar_month == 5))
- solar_term = 2;
- return(solar_term);
- } //计算阳历月对应的第二个节气
以上就是万年历的完整算法。它首先计算出对应阳历月第一天对应是星期几,然后根据数据压定,确定对应的阴历日期;而阴历节气,则有条件规律算法实现。