qt实现公历转农历,C++亦可用
qt实现公历转农历,C++亦可用
coder_life 发表于2年前
qt实现公历转农历,C++亦可用
  • 发表于 2年前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

摘要: 实现阳历日期转换农历日期

#include QCoreApplication#include QDebug

// 阳历1900.1.1 //在阳历为1900.1.31 时阴历为1900.1.1 即1900年正月初一 unsignedint solar_year=0,lunar_year=0; unsigned char solar_month=0,lunar_month=0;unsigned int solar_day=0,lunar_day=0; //1900-2050年的农历数据 //数据格式说明: //位十六进制数字 例:04bd8 // 1 位: 1表示闰月30天,0表示29天 //2、3、4位: 转换二进制为:0100 1011 1101(1为30天,0为29天) // 04bd8表示为(13个月):29,30,29,29,30,29,30,30,30(闰8月),30,30,29,30; // 5 位: 如果有闰月,则为月份,没有则为0 unsigned long int lunar_info[]= {0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, //19800x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5, //2004 075520x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530, //20280x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0, 0x14b63 };
//确定是否存在农历的闰月 并返回闰月 unsigned char get_leap_month(unsigned intlunar_year) { return lunar_info[lunar_year-1900]&0xf; }
//若存在闰月,返回闰月的天数,30?29 unsigned char get_leap_month_day(unsignedint lunar_year) { if(get_leap_month(lunar_year)) return( ( (lunar_info[lunar_year-1900]) & 0x10000 ) ? 30:29 ); else return(0); } //确定农历当月天数,30?29 unsignedchar get_lunar_month_total(unsigned int lunar_year, unsigned char lunar_month) {return( (lunar_info[lunar_year-1900] & (0x10000>>lunar_month) ) ? 30:29 ); } // 农历当年总天数,354?355 384 383 unsigned int get_lunar_year_total(unsigned intlunar_year) { //12个月 29天一月 unsigned int sum=348; unsigned int i;for(i=0x8000;i>0x8; i>>=1) sum+=(lunar_info[lunar_year-1900]&i)?1:0; //把大月的1加进去 return(sum+get_leap_month_day(lunar_year)); //判断这年是否有闰月 }

//判断是否为闰年 unsigned int leap(unsigned int year) { if( (year%4==0&&year%100!=0) || year%400==0 ) return 366; else return 365; }
//判断当年当月天数 unsigned char day(unsigned int year ,unsigned char month) {if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)return 31; if(month==4||month==6||month==9||month==11) return 30;if(month==2&&leap(year)==366) return 29; else return 28; }
//计算1900.1.1  输入年月的天数 unsigned int get_solar_total(unsigned intsolar_year, unsigned char solar_month) { unsigned int total; unsigned int temp_num;total=0; for(temp_num=1900;temp_num<</SPAN>solar_year;temp_num++)total+=leap(temp_num);for(temp_num=1;temp_num<</SPAN>solar_month;temp_num++)total+=day(solar_year,temp_num); return total; }
unsigned char solar_lunar(unsigned int kp_year,unsigned char kp_month,unsignedchar kp_day) { unsigned int total_day=0; unsigned charrun_yue_flag=0,run_yue=0,year_flag=0;
if(kp_year<</SPAN>1900 || kp_year>2050 || kp_month>12 || kp_month==0 ||(kp_year==1900 && kp_month==1) ) return 0; if(kp_day>day(kp_year,kp_month) ||kp_day==0) return 0;
total_day=get_solar_total( kp_year, kp_month)+kp_day-30; lunar_year=1900;while(total_day>385) //385大于一年 留出一年多的时间用于条件计算 { total_day-=get_lunar_year_total(lunar_year); // lunar_year++; }if(total_day>get_lunar_year_total(lunar_year)) //排除lunar_year有闰月的情况 {total_day-=get_lunar_year_total(lunar_year); lunar_year++;
} run_yue=get_leap_month(lunar_year); //当前闰哪个月 if(run_yue) run_yue_flag=1;//有闰月则一年为13个月 else run_yue_flag=0; //没闰月则一年为12个月
if(total_day==0) //刚好一年 { lunar_day=get_lunar_month_total(lunar_year,12);lunar_month=12; } else { lunar_month=1; while(lunar_month<=12) { if(run_yue_flag==1 && lunar_month==(run_yue+1) ) //闰月处理 {if(total_day>get_leap_month_day(lunar_year)) { total_day-=get_leap_month_day(lunar_year); //该年闰月天数
} //lunar_month--; run_yue_flag=0; continue;
} if( total_day> get_lunar_month_total(lunar_year,lunar_month ) ) {total_day=total_day-get_lunar_month_total(lunar_year,lunar_month); //该年该月天数lunar_month++; } else { lunar_day=total_day; break; } } } }


int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); solar_year = 2015;solar_month = 1 solar_day =25; solar_lunar(solar_year,solar_month,solar_day);qDebug() << lunar_year << lunar_month << lunar_day; return a.exec();}

共有 人打赏支持
粉丝 2
博文 15
码字总数 3772
×
coder_life
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: