农历转换问题请教
本转换程序1-9月农历显示都是正确的,就是10-12月农历显示不正确,请教各位,谢谢先。
void Conversion(void) /*==转换当前农历信息====================================*/ { unsigned char year,month,day,temp1,temp2,temp3,month_p; unsigned int temp4,code_addr; bit flag_y; // flag2, year =((nian/16)*10+nian%16)&0x7f; //加载年月日数据,如为BCD则需转为十进制 month=((yue/16)*10+yue%16); day=((ri/16)*10+ri%16); code_addr=year-1; //定位数据表地址 if(((nian/16)*10+nian%16)>>7==0) code_addr+=100; code_addr*=3;
temp1=(year_code[code_addr+2]&0x60)>>5; //取当年春节所在的公历月份 temp2=year_code[code_addr+2]&0x1f; //取当年春节所在的公历日 temp3=temp2-1; //计算当年春节离当年元旦的天数,春节只会在公历1月或2月 if(temp1!=1) temp3+=31; //如果不在1月则天数加上31天(1月) if(month<10) {temp4=day_code1[month-1]+day;} else {temp4=day_code2[month-10]+day;}
if((month<=2)||(year%0x04!=0)) temp4-=1; //如果公历月小于等于2月或者该年的2月非闰月,天数减1 temp2=(year_code[code_addr]&0xf0)>>4; //从数据表中取该年的闰月月份,如为0则该年无闰月 if (temp4>=temp3) //判断公历日在春节前还是春节后 { //公历日在春节后或就是春节当日使用下面代码进行运算 temp4 -=temp3; month = 1; flag_y = 0; month_p= 1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月 temp1=get_moon_day(month_p,code_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0 while(temp4>=temp1) { temp4-=temp1; month_p+=1; if(month==temp2) { flag_y=~flag_y; if(flag_y==0) month+=1; } else {month+=1;}
temp1=get_moon_day(month_p,code_addr); } day=temp4+1; } else { //公历日在春节前使用下面代码进行运算 temp3-=temp4; if(year==0) {year=0xe3;} else {year-=1;}
code_addr-=3; month = 12; flag_y = 0; if(temp2==0) {month_p=12;} else {month_p=13;} //如果当年有闰月,一年有十三个月,月指向13,无闰月指向12 temp1=get_moon_day(month_p,code_addr); while(temp3>temp1) { temp3-=temp1; month_p-=1; if(flag_y==0) month-=1; if(month==temp2) flag_y=~flag_y; temp1=get_moon_day(month_p,code_addr); } day=temp1-temp3+1; }
Tim[0] = year|(((nian/16)*10+nian%16)&0x80); //将农历信息写进指定变量 Tim[1] = month; Tim[2] = day; // Conver_week(); //最后进行星期转换(根据需要自行选用) }
unsigned char get_moon_day(unsigned char month_p,unsigned int code_addr)/*读取数据表中农历月的大月或小月,如果该月大返回1,小返回0*/ { unsigned char temp,temp5;
temp=0x80>>((month_p+3)%8); temp5=(month_p+3)/8; temp=year_code[code_addr+temp5]&temp; if(temp==0) return(29); else return(30);
}
|