网站导航: 首页 > 设计参考 > 正文 文章搜索
ARM 万年历程序
 
文章编号:
081229223947
文章分类: 单片机 ARM
点 击:
...
关 键 词: 万年历
文章来源:
《ARM 嵌入式技术应用》
摘 要:

一、 阳历算法

具体算法见函数Void get_solar_day_date(void),这样阳历日历的星期排法就确定了。
表1:

月份 1 2 3 4 5 6 7 8 9 10 11 12
闰年 31 29 31 30 31 30 31 31 30 31 30 31
非闰年 31 28 31 30 31 30 31 31 30 31 30 31

变量定义:

 
  1. Public:   
  2. Unsigned int temp_total_day;   
  3. Unsigned char gc_solar_calendar_year;   
  4. Unsigned char gc_solar_calendar_month;   
  5. Unsigned char gc_solar_calendar_date;   
  6. Unsigned char gc_lunar_calendar_year;   
  7. Unsigned char gc_lunar_calendar_month;   
  8. Unsigned char gc_lunar_calendar_date;   
  9. Unsigned char start_day_of_week;   


说明:函数get_solar_day_date(void)的输入变量:gc_solar_calendar_year和
gc_solar_calendar_month
输出变量:start_day_of_week和temp_total_day

 
  1. Void get_solar_day_date(void)   
  2. {   
  3.     unsigned char temp01;   
  4.     /*------calculate what day is the day of the current month and year. Mon~Sun?---  
  5.     */  
  6.     /*条件初始化二次,减少运算数据量. temp_total_day 是int型变量*/  
  7.     start_day_of_week = 2; temp_total_day = 0;calculate_temp = 1;   
  8.     if(gc_solar_calendar_year > 99)   
  9.     {   
  10.         start_day_of_week = 6;calculate_temp = 100;   
  11.     }   
  12.     for(temp01 = calculate_temp; temp01<gc_solar_calendar_year; temp01 )   
  13.     {    
  14.         if(temp014 == 0)   
  15.         {   
  16.             start_day_of_week =2;temp_total_day = 366;    
  17.         }   
  18.         else    
  19.         {   
  20.             start_day_of_week =1;temp_total_day = 365;   
  21.         }   
  22.     }   
  23.     for(temp01 = 1;temp01<gc_solar_calendar_month;temp01 )   
  24.     {    
  25.         switch(temp01)   
  26.         {   
  27.         case 1,3,5,7,8,10,12:    
  28.             start_day_of_week =3;temp_total_day =31;   
  29.             break;   
  30.         case 2:    
  31.             if(((gc_solar_calendar_year4) == 0)&&(gc_solar_calendar_year != 200))   
  32.             {   
  33.                 start_day_of_week =1; temp_total_day =29;   
  34.             }   
  35.             else    
  36.             {   
  37.                 start_day_of_week =0;temp_total_day =28;   
  38.             }    
  39.             break;   
  40.         case 4,6,9,11: start_day_of_week =2; temp_total_day =30;    
  41.             break;   
  42.         }   
  43.     }   
  44.     start_day_of_week =7;   
  45.     /*-end of calculate what day is the day(Mon~Sun?) and total day --*/  
  46. }   


二、 阴历算法
200年需要200 × 2 = 400个字节,构成阴历压缩数据表lunar_calendar_month_table[]如下:

 
  1. const char lunar_calendar_month_table[]={ //从阴历年1900年到2100年   
  2.     /*the total day of each month pointer */  
  3.     /* from 1901~2100*/  
  4.     /* (0110)110000001001 (0110)leap month,110000001001  
  5.     lunar month total day: 1:29 0:30*/  
  6.     0x00,0x04,0xad,0x08,0x5a,0x01,0xd5,0x54,0xb4,0x09,0x64,0x05,0x59,0x45,   
  7.     0x95,0x0a,0xa6,0x04,0x55,0x24,0xad,0x08,0x5a,0x62,0xda,0x04,0xb4,0x05,   
  8.     0xb4,0x55,0x52,0x0d,0x94,0x0a,0x4a,0x2a,0x56,0x02,0x6d,0x71,0x6d,0x01,   
  9.     0xda,0x02,0xd2,0x52,0xa9,0x05,0x49,0x0d,0x2a,0x45,0x2b,0x09,0x56,0x01,   
  10.     0xb5,0x20,0x6d,0x01,0x59,0x69,0xd4,0x0a,0xa8,0x05,0xa9,0x56,0xa5,0x04,   
  11.     0x2b,0x09,0x9e,0x38,0xb6,0x08,0xec,0x74,0x6c,0x05,0xd4,0x0a,0xe4,0x6a,   
  12.     0x52,0x05,0x95,0x0a,0x5a,0x42,0x5b,0x04,0xb6,0x04,0xb4,0x22,0x6a,0x05,   
  13.     0x52,0x75,0xc9,0x0a,0x52,0x05,0x35,0x55,0x4d,0x0a,0x5a,0x02,0x5d,0x31,   
  14.     0xb5,0x02,0x6a,0x8a,0x68,0x05,0xa9,0x0a,0x8a,0x6a,0x2a,0x05,0x2d,0x09,   
  15.     0xaa,0x48,0x5a,0x01,0xb5,0x09,0xb0,0x39,0x64,0x05,0x25,0x75,0x95,0x0a,   
  16.     0x96,0x04,0x4d,0x54,0xad,0x04,0xda,0x04,0xd4,0x44,0xb4,0x05,0x54,0x85,   
  17.     0x52,0x0d,0x92,0x0a,0x56,0x6a,0x56,0x02,0x6d,0x02,0x6a,0x41,0xda,0x02,   
  18.     0xb2,0xa1,0xa9,0x05,0x49,0x0d,0x0a,0x6d,0x2a,0x09,0x56,0x01,0xad,0x50,   
  19.     0x6d,0x01,0xd9,0x02,0xd1,0x3a,0xa8,0x05,0x29,0x85,0xa5,0x0c,0x2a,0x09,   
  20.     0x96,0x54,0xb6,0x08,0x6c,0x09,0x64,0x45,0xd4,0x0a,0xa4,0x05,0x51,0x25,   
  21.     0x95,0x0a,0x2a,0x72,0x5b,0x04,0xb6,0x04,0xac,0x52,0x6a,0x05,0xd2,0x0a,   
  22.     0xa2,0x4a,0x4a,0x05,0x55,0x94,0x2d,0x0a,0x5a,0x02,0x75,0x61,0xb5,0x02,   
  23.     0x6a,0x03,0x61,0x45,0xa9,0x0a,0x4a,0x05,0x25,0x25,0x2d,0x09,0x9a,0x68,   
  24.     0xda,0x08,0xb4,0x09,0xa8,0x59,0x54,0x03,0xa5,0x0a,0x91,0x3a,0x96,0x04,   
  25.     0xad,0xb0,0xad,0x04,0xda,0x04,0xf4,0x62,0xb4,0x05,0x54,0x0b,0x44,0x5d,   
  26.     0x52,0x0a,0x95,0x04,0x55,0x22,0x6d,0x02,0x5a,0x71,0xda,0x02,0xaa,0x05,   
  27.     0xb2,0x55,0x49,0x0b,0x4a,0x0a,0x2d,0x39,0x36,0x01,0x6d,0x80,0x6d,0x01,   
  28.     0xd9,0x02,0xe9,0x6a,0xa8,0x05,0x29,0x0b,0x9a,0x4c,0xaa,0x08,0xb6,0x08,   
  29.     0xb4,0x38,0x6c,0x09,0x54,0x75,0xd4,0x0a,0xa4,0x05,0x45,0x55,0x95,0x0a,   
  30.     0x9a,0x04,0x55,0x44,0xb5,0x04,0x6a,0x82,0x6a,0x05,0xd2,0x0a,0x92,0x6a,   
  31.     0x4a,0x05,0x55,0x0a,0x2a,0x4a,0x5a,0x02,0xb5,0x02,0xb2,0x31,0x69,0x03,   
  32.     0x31,0x73,0xa9,0x0a,0x4a,0x05,0x2d,0x55,0x2d,0x09,0x5a,0x01,0xd5,0x48,   
  33.     0xb4,0x09,0x68,0x89,0x54,0x0b,0xa4,0x0a,0xa5,0x6a,0x95,0x04,0xad,0x08,   
  34.     0x6a,0x44,0xda,0x04,0x74,0x05,0xb0,0x25,0x54,0x03,   
  35. };   

 


确定阳历日和阴历日的对应关系的算法:
对于其他任何一个阳历日和阴历日的对应关系,都可以通过以下算法求得结果。具体算法由如下函数get_lunar_day(void)实现:
说明:函数get_lunar_day(void)的输入变量:gc_solar_calendar_year和
gc_solar_calendar_month
输出变量:gc_lunar_calendar_year、gc_lunar_calendar_month和
gc_lunar_calendar_date

 
  1. void get_lunar_day(void)/*计算出输入阳历年、阳历月,对应该阳历月第一天对应阴历时间,即阴历年、月、日*/  
  2. {   
  3.     unsigned char temp_leap_month;   
  4.     unsigned char temp_flag;   
  5.     unsigned char calculate_temp;   
  6.     unsigned char mc_tpumenus_temp_loop;   
  7.     unsigned char mc_tpumenus_temp_01;   
  8.     temp_leap_month = 0;temp_flag = 1;   
  9.     //条件初始化二次,减少运算数据量.   
  10.     if(gc_solar_calendar_year > 99)   
  11.     {   
  12.         gc_lunar_calendar_year = 99;gc_lunar_calendar_month = 11;   
  13.         gc_lunar_calendar_date = 25;temp_total_day = 25;calculate_temp = 100;   
  14.     }   
  15.     else  
  16.     {   
  17.         gc_lunar_calendar_year = 0;gc_lunar_calendar_month = 11;   
  18.         gc_lunar_calendar_date = 11;temp_total_day = 11;calculate_temp = 1;   
  19.     }   
  20.     if(gc_solar_calendar_year >calculate_temp||gc_solar_calendar_month>1)   
  21.     {    
  22.         for(mc_tpumenus_temp_loop = 1;mc_tpumenus_temp_loop>0;)   
  23.         {   
  24.             temp_total_day -=calendar_calculate_lunar_month_total_day();   
  25.             temp_leap_month = tpumenus_lunar_calendar_month_table[2*gc_lunar_calendar_year   
  26.                 1];   
  27.             temp_leap_month = (temp_leap_month>>4)&0x0F;   
  28.             if(gc_lunar_calendar_month == temp_leap_month)   
  29.             {   
  30.                 switch(gc_lunar_calendar_year)   
  31.                 {   
  32.                     case 6,14,19,25,33,36,38,41,44,52,55,79,117,   
  33.                         136,147,150,155,158,185,193:   
  34.                         if(temp_total_day<31)   
  35.                         {   
  36.                             gc_lunar_calendar_date = temp_total_day;   
  37.                             mc_tpumenus_temp_loop = 0;temp_flag = 0;   
  38.                         }   
  39.                         else    
  40.                             temp_total_day -= 30;   
  41.                         break//current month:temp_leap_month   
  42.                     default:   
  43.                         if(temp_total_day < 30)   
  44.                         {   
  45.                             gc_lunar_calendar_date = temp_total_day;mc_tpumenus_temp_loop = 0;   
  46.                             temp_flag = 0; /*current month:temp_leap_month*/    
  47.                         }   
  48.                         else temp_total_day -= 29;    
  49.                         break;   
  50.                 }   
  51.             }   
  52.             if(temp_flag)   
  53.             {   
  54.                 gc_lunar_calendar_month ;   
  55.                 if(gc_lunar_calendar_month == 13)   
  56.                 {   
  57.                     gc_lunar_calendar_month = 1;   
  58.                     gc_lunar_calendar_year ;   
  59.                 }   
  60.                 if(temp_total_day < 61) //if temp_total_day>60,ignore compare   
  61.                 {   
  62.                     mc_tpumenus_temp_01 = calendar_calculate_lunar_month_total_day();   
  63.                     if(temp_total_day < (mc_tpumenus_temp_01 1))   
  64.                     {   
  65.                         mc_tpumenus_temp_loop = 0;gc_lunar_calendar_date = temp_total_day;   
  66.                     }    
  67.                 }   
  68.             }    
  69.         }    
  70.     }   
  71.     gc_lunar_leap_month = (temp_flag<<4)|temp_leap_month;/*set leap_month flag*/  
  72. }   

 

说明:函数calendar_calculate_lunar_month_total_day(void)根据输入变量
gc_lunar_calendar_year和gc_lunar_calendar_month,结合压缩数据表
lunar_calendar_month_table计算出对应阴历月的总的天数。

 
  1. unsigned char calendar_calculate_lunar_month_total_day(void)   
  2. {   
  3.     unsigned char mc_tpumenus_temp_01;   
  4.     unsigned char mc_tpumenus_temp_02;   
  5.     if(gc_lunar_calendar_month < 9)   
  6.     {   
  7.         mc_tpumenus_temp_01 = lunar_calendar_month_table[2*gc_lunar_calendar_year];   
  8.         mc_tpumenus_temp_02 = gc_lunar_calendar_month - 1;   
  9.     }   
  10.     else  
  11.     {   
  12.         mc_tpumenus_temp_01 = lunar_calendar_month_table[2*gc_lunar_calendar_year 1];   
  13.         mc_tpumenus_temp_02 = gc_lunar_calendar_month - 9;   
  14.     }   
  15.     if((mc_tpumenus_temp_01>> mc_tpumenus_temp_02)&0x01)   
  16.         return(29);   
  17.     else    
  18.         return(30);   
  19. }   

 

阳历日和农历节气的对应关系

压缩节气数据表:
根据规律可以得到四个数据表(每个阳历月有两个节气,每个节气需要两个数据表):

 
  1. const unsigned char calendar_solar_term_table_01[12][33] = {   
  2.     {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   
  3.     {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   
  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}, //3   
  5.     {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,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   
  7.     {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   
  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,6,6,6,7,7}, //7   
  9.     {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   
  10.     {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   
  11.     {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   
  12.     {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   
  13.     {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   
  14. }; //这个数据表表示了每个月第一个节气出现的规律   
  15. const unsigned char calendar_solar_term_year_01[12][9] = {   
  16.     {13,49,85,117,149,185,201,250,250}, //month 1   
  17.     {13,45,81,117,149,185,201,250,250}, //2   
  18.     {13,48,84,112,148,184,200,201,250}, //3   
  19.     {13,45,76,108,140,172,200,201,250}, //4   
  20.     {13,44,72,104,132,168,200,201,250}, //5   
  21.     {5 ,33,68,96 ,124,152,188,200,201}, //6   
  22.     {29,57,85,120,148,176,200,201,250}, //7   
  23.     {13,48,76,104,132,168,196,200,201}, //8   
  24.     {25,60,88,120,148,184,200,201,250}, //9   
  25.     {16,44,76,108,144,172,200,201,250}, //10   
  26.     {28,60,92,124,160,192,200,201,250}, //11   
  27.     {17,53,85,124,156,188,200,201,250}, //12   
  28. }; //这个数据表表示了每个月第一个节气出现规律对应的阳历年份范围   
  29. const unsigned char calendar_solar_term_table_02[12][29] = {   
  30.     {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,1   
  31.         9,20},   
  32.     {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,1   
  33.     8,18},   
  34.     {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,2   
  35.     0,20,20},   
  36.     {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,1   
  37.     9,20,20},   
  38.     {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,2   
  39.     0,21,21},   
  40.     {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,2   
  41.     1,21,21},   
  42.     {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,2   
  43.     2,22,23},   
  44.     {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,2   
  45.     2,23,23},   
  46.     {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,2   
  47.     2,23,23},   
  48.     {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,2   
  49.     3,23,23},   
  50.     {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,2   
  51.     2,22,22},   
  52.     {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,2   
  53.     1,21,22},   
  54. }; //这个数据表表示了每个月第二个节气出现的规律   
  55. const unsigned char calendar_solar_term_year_02[12][8] = {   
  56.     {13,45,81,113,149,185,201},{21,57,93,125,161,193,201},   
  57.     {21,56,88,120,152,188,200,201},   
  58.     {21,49,81,116,144,176,200,201},{17,49,77,112,140,168,200,201},   
  59.     {28,60,88,116,148,180,200,201},{25,53,84,112,144,172,200,201},   
  60.     {29,57,89,120,148,180,200,201},{17,45,73,108,140,168,200,201},   
  61.     {28,60,92,124,160,192,200,201},{16,44,80,112,148,180,200,201},   
  62.     {17,53,88,120,156,188,200,201},};   
  63.     //这个数据表表示了每个月第二个节气出现规律对应的阳历年份范围   

 

每个阳历月对应的两个农历节气出现的日期,可根据条件规律算法,分别由以下两个函数实现:

 
  1. unsigned char calendar_calculate_solar_term_1(void)   
  2. {   
  3.     zpage unsigned char done_index;   
  4.     zpage unsigned char solar_term;   
  5.     done_index = 0;   
  6.     while(gc_solar_calendar_year >= calendar_solar_term_year_01   
  7.         [gc_solar_calendar_month - 1][done_index])    
  8.     {   
  9.         one_index ;   
  10.     }   
  11.     solar_term = calendar_solar_term_table_01[gc_solar_calendar_month - 1]   
  12.         [4*done_index gc_solar_calendar_year4];   
  13.     if((gc_solar_calendar_year == 121)&&(gc_solar_calendar_month == 4))   
  14.         solar_term = 5;   
  15.     if((gc_solar_calendar_year == 132)&&(gc_solar_calendar_month == 4))   
  16.         solar_term = 5;   
  17.     if((gc_solar_calendar_year == 194)&&(gc_solar_calendar_month == 6))   
  18.         solar_term = 6;   
  19.     return(solar_term);   
  20. //计算阳历月对应的第一个节气   
  21. unsigned char calendar_calculate_solar_term_2(void)   
  22. {   
  23.     zpage unsigned char done_index;   
  24.     zpage unsigned char solar_term;   
  25.     done_index = 0;   
  26.     while(gc_solar_calendar_year >= calendar_solar_term_year_02   
  27.         [gc_solar_calendar_month - 1][done_index])   
  28.     {   
  29.         done_index ;   
  30.     }   
  31.     solar_term = calendar_solar_term_table_02[gc_solar_calendar_month - 1]   
  32.         [4*done_index gc_solar_calendar_year4];   
  33.     if((gc_solar_calendar_year == 171)&&(gc_solar_calendar_month == 3))   
  34.         solar_term = 21;   
  35.     if((gc_solar_calendar_year == 181)&&(gc_solar_calendar_month == 5))   
  36.         solar_term = 21;   
  37.     return(solar_term);   
  38. //计算阳历月对应的第二个节气   

 

以上就是万年历的完整算法。它首先计算出对应阳历月第一天对应是星期几,然后根据数据压缩算法确定,确定对应的阴历日期;而阴历节气,则有条件规律算法实现。

 
相关文章:

 
最新开源项目
 
 
  查看更多...  
 
本站相关产品   淘宝网店
 




 
  查看更多...  

 

本站程序由百合电子工作室开发和维护
Copyright @ baihe electric studio
渝ICP备09006681号-4