公历和农历转换算法详解
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
公历和农历转换算法详解
//C51写的公历转农历和星期
#define uchar unsigned char
#define uint unsigned int
#include <intrins.h>
/*
公历年对应的农历数据,每年三字节,
格式第一字节BIT7-4 位表示闰月月份,值为0 为无闰月,BIT3-0 对应农历第1-4 月的大小第二字节BIT7-0 对应农历第5-12 月大小,第三字节BIT7 表示农历第13 个月大小
月份对应的位为1 表示本农历月大(30 天),为0 表示小(29 天)
第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期
*/
code uchar year_code[597] = {
0x04,0xAe,0x53, //1901 0
0x0A,0x57,0x48, //1902 3
0x55,0x26,0xBd, //1903 6
0x0d,0x26,0x50, //1904 9
0x0d,0x95,0x44, //1905 12
0x46,0xAA,0xB9, //1906 15
0x05,0x6A,0x4d, //1907 18
0x09,0xAd,0x42, //1908 21
0x24,0xAe,0xB6, //1909
0x04,0xAe,0x4A, //1910
0x6A,0x4d,0xBe, //1911
0x0A,0x4d,0x52, //1912
0x0d,0x25,0x46, //1913
0x5d,0x52,0xBA, //1914
0x0B,0x54,0x4e, //1915
0x0d,0x6A,0x43, //1916
0x29,0x6d,0x37, //1917
0x09,0x5B,0x4B, //1918
0x74,0x9B,0xC1, //1919
0x04,0x97,0x54, //1920
0x0A,0x4B,0x48, //1921
0x5B,0x25,0xBC, //1922
0x06,0xA5,0x50, //1923
0x06,0xd4,0x45, //1924
0x4A,0xdA,0xB8, //1925
0x02,0xB6,0x4d, //1926
0x09,0x57,0x42, //1927
0x24,0x97,0xB7, //1928
0x66,0x4B,0x3e, //1930 0x0d,0x4A,0x51, //1931 0x0e,0xA5,0x46, //1932 0x56,0xd4,0xBA, //1933 0x05,0xAd,0x4e, //1934 0x02,0xB6,0x44, //1935 0x39,0x37,0x38, //1936 0x09,0x2e,0x4B, //1937 0x7C,0x96,0xBf, //1938 0x0C,0x95,0x53, //1939 0x0d,0x4A,0x48, //1940 0x6d,0xA5,0x3B, //1941 0x0B,0x55,0x4f, //1942 0x05,0x6A,0x45, //1943 0x4A,0xAd,0xB9, //1944 0x02,0x5d,0x4d, //1945 0x09,0x2d,0x42, //1946 0x2C,0x95,0xB6, //1947 0x0A,0x95,0x4A, //1948 0x7B,0x4A,0xBd, //1949 0x06,0xCA,0x51, //1950 0x0B,0x55,0x46, //1951 0x55,0x5A,0xBB, //1952 0x04,0xdA,0x4e, //1953 0x0A,0x5B,0x43, //1954 0x35,0x2B,0xB8, //1955 0x05,0x2B,0x4C, //1956 0x8A,0x95,0x3f, //1957 0x0e,0x95,0x52, //1958 0x06,0xAA,0x48, //1959 0x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //1961 0x04,0xB6,0x45, //1962 0x4A,0x57,0x39, //1963 0x0A,0x57,0x4d, //1964 0x05,0x26,0x42, //1965 0x3e,0x93,0x35, //1966 0x0d,0x95,0x49, //1967 0x75,0xAA,0xBe, //1968 0x05,0x6A,0x51, //1969 0x09,0x6d,0x46, //1970 0x54,0xAe,0xBB, //1971 0x04,0xAd,0x4f, //1972
0x4d,0x26,0xB7, //1974
0x0d,0x25,0x4B, //1975
0x8d,0x52,0xBf, //1976
0x0B,0x54,0x52, //1977
0x0B,0x6A,0x47, //1978
0x69,0x6d,0x3C, //1979
0x09,0x5B,0x50, //1980
0x04,0x9B,0x45, //1981
0x4A,0x4B,0xB9, //1982
0x0A,0x4B,0x4d, //1983
0xAB,0x25,0xC2, //1984
0x06,0xA5,0x54, //1985
0x06,0xd4,0x49, //1986
0x6A,0xdA,0x3d, //1987
0x0A,0xB6,0x51, //1988
0x09,0x37,0x46, //1989
0x54,0x97,0xBB, //1990
0x04,0x97,0x4f, //1991
0x06,0x4B,0x44, //1992
0x36,0xA5,0x37, //1993
0x0e,0xA5,0x4A, //1994
0x86,0xB2,0xBf, //1995
0x05,0xAC,0x53, //1996
0x0A,0xB6,0x47, //1997
0x59,0x36,0xBC, //1998
0x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //2001
0x0d,0x4A,0x4C, //2002
0x0d,0xA5,0x41, //2003
0x25,0xAA,0xB6, //2004
0x05,0x6A,0x49, //2005
0x7A,0xAd,0xBd, //2006
0x02,0x5d,0x52, //2007
0x09,0x2d,0x47, //2008
0x5C,0x95,0xBA, //2009
0x0A,0x95,0x4e, //2010
0x0B,0x4A,0x43, //2011
0x4B,0x55,0x37, //2012
0x0A,0xd5,0x4A, //2013
0x95,0x5A,0xBf, //2014
0x04,0xBA,0x53, //2015
0x0A,0x5B,0x48, //2016
0x05,0x2B,0x50, //2018 0x0A,0x93,0x45, //2019 0x47,0x4A,0xB9, //2020 0x06,0xAA,0x4C, //2021 0x0A,0xd5,0x41, //2022 0x24,0xdA,0xB6, //2023 0x04,0xB6,0x4A, //2024 0x69,0x57,0x3d, //2025 0x0A,0x4e,0x51, //2026 0x0d,0x26,0x46, //2027 0x5e,0x93,0x3A, //2028 0x0d,0x53,0x4d, //2029 0x05,0xAA,0x43, //2030 0x36,0xB5,0x37, //2031 0x09,0x6d,0x4B, //2032 0xB4,0xAe,0xBf, //2033 0x04,0xAd,0x53, //2034 0x0A,0x4d,0x48, //2035 0x6d,0x25,0xBC, //2036 0x0d,0x25,0x4f, //2037 0x0d,0x52,0x44, //2038 0x5d,0xAA,0x38, //2039 0x0B,0x5A,0x4C, //2040 0x05,0x6d,0x41, //2041 0x24,0xAd,0xB6, //2042 0x04,0x9B,0x4A, //2043 0x7A,0x4B,0xBe, //2044 0x0A,0x4B,0x51, //2045 0x0A,0xA5,0x46, //2046 0x5B,0x52,0xBA, //2047 0x06,0xd2,0x4e, //2048 0x0A,0xdA,0x42, //2049 0x35,0x5B,0x37, //2050 0x09,0x37,0x4B, //2051 0x84,0x97,0xC1, //2052 0x04,0x97,0x53, //2053 0x06,0x4B,0x48, //2054 0x66,0xA5,0x3C, //2055 0x0e,0xA5,0x4f, //2056 0x06,0xB2,0x44, //2057 0x4A,0xB6,0x38, //2058 0x0A,0xAe,0x4C, //2059 0x09,0x2e,0x42, //2060
0x0C,0x96,0x49, //2062
0x7d,0x4A,0xBd, //2063
0x0d,0x4A,0x51, //2064
0x0d,0xA5,0x45, //2065
0x55,0xAA,0xBA, //2066
0x05,0x6A,0x4e, //2067
0x0A,0x6d,0x43, //2068
0x45,0x2e,0xB7, //2069
0x05,0x2d,0x4B, //2070
0x8A,0x95,0xBf, //2071
0x0A,0x95,0x53, //2072
0x0B,0x4A,0x47, //2073
0x6B,0x55,0x3B, //2074
0x0A,0xd5,0x4f, //2075
0x05,0x5A,0x45, //2076
0x4A,0x5d,0x38, //2077
0x0A,0x5B,0x4C, //2078
0x05,0x2B,0x42, //2079
0x3A,0x93,0xB6, //2080
0x06,0x93,0x49, //2081
0x77,0x29,0xBd, //2082
0x06,0xAA,0x51, //2083
0x0A,0xd5,0x46, //2084
0x54,0xdA,0xBA, //2085
0x04,0xB6,0x4e, //2086
0x0A,0x57,0x43, //2087
0x45,0x27,0x38, //2088
0x0d,0x26,0x4A, //2089
0x8e,0x93,0x3e, //2090
0x0d,0x52,0x52, //2091
0x0d,0xAA,0x47, //2092
0x66,0xB5,0x3B, //2093
0x05,0x6d,0x4f, //2094
0x04,0xAe,0x45, //2095
0x4A,0x4e,0xB9, //2096
0x0A,0x4d,0x4C, //2097
0x0d,0x15,0x41, //2098
0x2d,0x92,0xB5, //2099
};
///月份数据表
code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3}; code uint day_code2[3]={0x111,0x130,0x14e};
/*
调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun)
如:计算2004年10月16日Conversion(0,0x4,0x10,0x16);
c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世纪,c_sun=1为19世纪
调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据*/
bit c_moon;
data uchar year_moon,month_moon,day_moon,week;
/*子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0*/
bit get_moon_day(uchar month_p,uint table_addr)
{
uchar temp;
switch (month_p)
{
case 1:{temp=year_code[table_addr]&0x08;
if (temp==0)return(0);else return(1);}
case 2:{temp=year_code[table_addr]&0x04;
if (temp==0)return(0);else return(1);}
case 3:{temp=year_code[table_addr]&0x02;
if (temp==0)return(0);else return(1);}
case 4:{temp=year_code[table_addr]&0x01;
if (temp==0)return(0);else return(1);}
case 5:{temp=year_code[table_addr+1]&0x80;
if (temp==0) return(0);else return(1);}
case 6:{temp=year_code[table_addr+1]&0x40;
if (temp==0)return(0);else return(1);}
case 7:{temp=year_code[table_addr+1]&0x20;
if (temp==0)return(0);else return(1);}
case 8:{temp=year_code[table_addr+1]&0x10;
if (temp==0)return(0);else return(1);}
case 9:{temp=year_code[table_addr+1]&0x08;
if (temp==0)return(0);else return(1);}
case 10:{temp=year_code[table_addr+1]&0x04;
if (temp==0)return(0);else return(1);}
case 11:{temp=year_code[table_addr+1]&0x02;
if (temp==0)return(0);else return(1);}
case 12:{temp=year_code[table_addr+1]&0x01;
if (temp==0)return(0);else return(1);}
case 13:{temp=year_code[table_addr+2]&0x80;
if (temp==0)return(0);else return(1);}
}
}
/*
调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun)
如:计算2004年10月16日Conversion(0,0x4,0x10,0x16);
c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世
纪,c_sun=1为19世纪
调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据*/
void Conversion(bit c,uchar year,uchar month,uchar day)
{ //c=0 为21世纪,c=1 为19世纪输入输出数据均为BCD数据uchar temp1,temp2,temp3,month_p;
uint temp4,table_addr;
bit flag2,flag_y;
temp1=year/16; //BCD->hex 先把数据转换为十六进制
temp2=year%16;
year=temp1*10+temp2;
temp1=month/16;
temp2=month%16;
month=temp1*10+temp2;
temp1=day/16;
temp2=day%16;
day=temp1*10+temp2;
//定位数据表地址
if(c==0)
{
table_addr=(year+0x64-1)*0x3;
}
else
{
table_addr=(year-1)*0x3;
}
//定位数据表地址完成
//取当年春节所在的公历月份
temp1=year_code[table_addr+2]&0x60;
temp1=_cror_(temp1,5);
//取当年春节所在的公历月份完成
//取当年春节所在的公历日
temp2=year_code[table_addr+2]&0x1f;
//取当年春节所在的公历日完成
// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月
if(temp1==0x1)
{
temp3=temp2-1;
}
else
{
temp3=temp2+0x1f-1;
}
// 计算当年春年离当年元旦的天数完成
//计算公历日离当年元旦的天数,为了减少运算,用了两个表
//day_code1[9],day_code2[3]
//如果公历月在九月或前,天数会少于0xff,用表day_code1[9],
//在九月后,天数大于0xff,用表day_code2[3]
//如输入公历日为8月10日,则公历日离元旦天数为day_code1[8-1]+10-1
//如输入公历日为11月10日,则公历日离元旦天数为day_code2[11-10]+10-1
if (month<10)
{
temp4=day_code1[month-1]+day-1;
}
else
{
temp4=day_code2[month-10]+day-1;
}
if ((month>0x2)&&(year%0x4==0))
{ //如果公历月大于2月并且该年的2月为闰月,天数加1
temp4+=1;
}
//计算公历日离当年元旦的天数完成
//判断公历日在春节前还是春节后
if (temp4>=temp3)
{ //公历日在春节后或就是春节当日使用下面代码进行运算
temp4-=temp3;
month=0x1;
month_p=0x1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月flag2=get_moon_day(month_p,table_addr);
//检查该农历月为大小还是小月,大月返回1,小月返回0
flag_y=0;
if(flag2==0)temp1=0x1d; //小月29天
else temp1=0x1e; //大小30天
temp2=year_code[table_addr]&0xf0;
temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为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;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
}
day=temp4+1;
}
else
{ //公历日在春节前使用下面代码进行运算
temp3-=temp4;
if (year==0x0)
{
year=0x63;c=1;
}
else year-=1;
table_addr-=0x3;
month=0xc;
temp2=year_code[table_addr]&0xf0;
temp2=_cror_(temp2,4);
if (temp2==0)
month_p=0xc;
else
month_p=0xd; //
/*month_p为月份指向,如果当年有闰月,一年有十三个月,月指向13,无闰月指向12*/ flag_y=0;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
while(temp3>temp1)
{
temp3-=temp1;
month_p-=1;
if(flag_y==0)month-=1;
if(month==temp2)flag_y=~flag_y;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
}
day=temp1-temp3+1;
}
c_moon=c; //HEX->BCD ,运算结束后,把数据转换为BCD数据
temp1=year/10;
temp1=_crol_(temp1,4);
temp2=year%10;
year_moon=temp1|temp2;
temp1=month/10;
temp1=_crol_(temp1,4);
temp2=month%10;
month_moon=temp1|temp2;
temp1=day/10;
temp1=_crol_(temp1,4);
temp2=day%10;
day_moon=temp1|temp2;
}
/*函数功能:输入BCD阳历数据,输出BCD星期数据(只允许1901-2099年)
调用函数示例:Conver_week(c_sun,year_sun,month_sun,day_sun)
如:计算2004年10月16日Conversion(0,0x4,0x10,0x16);
c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世纪,c_sun=1为19世纪
调用函数后,原有数据不变,读week得出阴历BCD数据
*/
code uchar table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表
/*
算法:日期+年份+所过闰年数+月较正数之和除7 的余数就是星期但如果是在
闰年又不到3 月份上述之和要减一天再除7
星期数为0
*/
void Conver_week(bit c,uchar year,uchar month,uchar day)
{//c=0 为21世纪,c=1 为19世纪输入输出数据均为BCD数据
uchar temp1,temp2;
temp1=year/16; //BCD->hex 先把数据转换为十六进制
temp2=year%16;
year=temp1*10+temp2;
temp1=month/16;
temp2=month%16;
month=temp1*10+temp2;
temp1=day/16;
temp2=day%16;
day=temp1*10+temp2;
if (c==0){year+=0x64;} //如果为21世纪,年份数加100
temp1=year/0x4; //所过闰年数只算1900年之后的
temp2=year+temp1;
temp2=temp2%0x7; //为节省资源,先进行一次取余,避免数大于0xff,避免使用整型数据temp2=temp2+day+table_week[month-1];
if (year%0x4==0&&month<3)temp2-=1;
week=temp2%0x7;
}
//test
uchar c_sun,year_sun,month_sun,day_sun;
void main()
{
c_sun=1;
year_sun=0x2;
month_sun=0x11;
day_sun=0x3;
Conver_week(c_sun,year_sun,month_sun,day_sun);
Conversion(c_sun,year_sun,month_sun,day_sun);
while(1);
}
算法一:公历转农历
============================================================================ /*------------农历转换函数-----------*/
char *GetDayOf(PSYSTEMTIME pSt)
{
/*天干名称*/
const char *cTianGan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
/*地支名称*/
const char *cDiZhi[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
/*属相名称*/
const char *cShuXiang[] = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
/*农历日期名*/
const char *cDayName[] = {"*","初一","初二","初三","初四","初五",
"初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
/*农历月份名*/
const char *cMonName[] = {"*","正","二","三","四","五","六","七","八","九","十","十一","腊"};
/*公历每月前面的天数*/
const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
/*农历数据*/
const int wNongliData[100] = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438 ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};
static int wCurYear,wCurMonth,wCurDay;
static int nTheDate,nIsEnd,m,k,n,i,nBit;
TCHAR szNongli[30], szNongliDay[10],szShuXiang[10];
/*---取当前公历年、月、日---*/
wCurYear = pSt->wYear;
wCurMonth = pSt->wMonth;
wCurDay = pSt->wDay;
/*---计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)---*/
nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd[wCurMonth - 1] - 38;
if((!(wCurYear % 4)) && (wCurMonth > 2))
nTheDate = nTheDate + 1;
/*--计算农历天干、地支、月、日---*/
nIsEnd = 0;
m = 0;
while(nIsEnd != 1)
{
if(wNongliData[m] < 4095)
k = 11;
else
k = 12;
n = k;
while(n>=0)
{
//获取wNongliData(m)的第n个二进制位的值
nBit = wNongliData[m];
for(i=1;i<n+1;i++)
nBit = nBit/2;
nBit = nBit % 2;
if (nTheDate <= (29 + nBit))
{
nIsEnd = 1;
break;
}
nTheDate = nTheDate - 29 - nBit;
n = n - 1;
}
if(nIsEnd)
break;
m = m + 1;
}
wCurYear = 1921 + m;
wCurMonth = k - n + 1;
wCurDay = nTheDate;
if (k == 12)
{
if (wCurMonth == wNongliData[m] / 65536 + 1)
wCurMonth = 1 - wCurMonth;
else if (wCurMonth > wNongliData[m] / 65536 + 1)
wCurMonth = wCurMonth - 1;
}
/*--生成农历天干、地支、属相==> wNongli--*/
wsprintf(szShuXiang,"%s",cShuXiang[((wCurYear - 4) % 60) % 12]);
wsprintf(szNongli,"%s(%s%s)年",szShuXiang,cTianGan[((wCurYear - 4) % 60) % 10],cDiZhi[((wCurYear - 4) % 60) % 12]);
/*--生成农历月、日==> wNongliDay--*/
if (wCurMonth < 1)
wsprintf(szNongliDay,"闰%s",cMonName[-1 * wCurMonth]);
else
strcpy(szNongliDay,cMonName[wCurMonth]);
strcat(szNongliDay,"月");
strcat(szNongliDay,cDayName[wCurDay]);
return strcat(szNongli,szNongliDay);
}
============================================================================
首先,我们要确定一个时刻,作为一天的起点(包括这个时刻)。
然后我们以月亮通过朔望交界点的那一天作为每个月的第一天,以太阳通过雨水点的那个月作为正月,依次的,以太阳通过各中气点的那个月作为二月、三月等等。
经过长期的观察之后,我们就会发现每个月不是29天就是30天,每年不是12个月就是13个月,每个中气点唯一对应一个月,但是有的月却没有中气。
为了制定我们的历法,我们至少要知道足够精度的朔望月长度,记为ML,和两个中气的间隔时间记为YL,这两个值经过前人长期不懈的观测和计算,我们知道分别为29.5306日和30.4377日。
接下来,我们要随便找到某一年,以太阳通过雨水点的时刻到那一年开始的那个时刻的时间,记为DY,以日为单位,还要找出这一年正月里,月亮通过朔望交界点的时刻到那一月开始的那个时刻的时间,记为DM,以日为单位。
这两个值是要通过实地观测得出来的。
这个时候,我们就可以通过递推来制订我们的历法,以预测未来月球和太阳的运行情况,并进一步预测月相的变化和气候的变化。
在本算法中我们使用了三个近似处理:
每天的长度总是一样的;
每个朔望月的长度总是一样的;
每两个相邻中气的时间总是一样的。
接下来,我们就逐月推算每月的大小:
Select Case Fix(DM + ML)
Case is = 29
The month have 29 days.
The DM of next month is DM + ML - 29.
Case is = 30
The month have 30 days.
The DM of next month is DM + ML - 30.
End Select
每年中各月的月份、是否为闰月
For i = 1 to 12
This month has A days.
If DY>=A Then
This month is an Extra Month.
DY = DY - A
Else
This month is the i Month.
DY = DY + YL - A
i = i + 1
End If
Next
这就是我们的核心算法。
通过它,我们可以从任何一个包含中气的月份开始向后推算各月各年的情况。
由于我们所用的DM、DY、ML、YL都是近似值,若干年后,我们的历法必有偏差,我们只要重新观测,更新DM、DY、ML、YL的值就可以了。
事实上,诸朝气数最多不过400年,在这些值精确到万分位的时候,通行一朝是完全可以的。
下面对大小月的排列和闰月的出现作一些定性分析
显然0≤DM<1,那么
DM<0.4694时,当月为小月,下月为大月
DM≥0.4694时,当月为大月
DM<0.4694 * 2时,下月为小月,下下月为大月
DM≥0.4694 * 2时,下月亦为大月,下下月为小月
所以,大小月一般是交替出现,有时会出现连续两个月是大月的情况,但不会出现连续三个月或更多的是大月的情况,也不会出现连续两个月更多的是小月的情况。
也就是说,一般是小月大月相继出现一段时间后,接着就出现一个大月,然后又是小月大月相继出现,具体的间隔也不定,大家可以通过计算机推算后,找点规律出来。
一年中最少有十二个月,因为YL * 12 (代表十二个中气循环一次的时间)>ML * 12 (代表十二个朔望月的时间)+ 1 (第一个月的DM总小于1),最多有十三个月,因为30(第一个月的DY 总小于30)+ YL * 12 <ML * 14。
两个相邻中气的间隔时间大于30,所以一个月不可能有两个中气,那么一年有十二个月的时候,12个中气必然刚好依次各居一个月,这一年就称为平年,一年有十三个月的时候,必然有且仅有一个月没有中气,那个月就时闰月,这一年就称为闰年。
平年的时候,根据大小月的排列规则,一年最多有8个大月,最少有6个大月,因为ML * 12 + 1 <30 * 8 + 29 * (12 - 8),所以不可能有8个大月,最多为7个大月,那么一年就为354或355天。
闰年的时候,根据大小月的排列规则,一年最多有9个大月,最少有6个大月,因为ML * 13 + 1 <30 * 8 + 29 * (13 - 8),所以不可能有8个或更多的大月,最多为7个大月,那么一年就为383或384天。
因为每月的天数不固定,所以闰月的出现规律也不好讨论,连续十二个月的天数也不固定,所以闰年的出现规律也不好讨论。
不过可以肯定是,每年的第一个月肯定不是闰月,因为我们是以包含中气雨水的月份作为正月,也就是每年的第一个月的。
所以,闰月必定出现在某个有名字的月份后面,出现在哪个后面就叫闰哪月。
由于有闰月,所以函数形式:
int toSolar(int ly, int lm, int ld, int *sy, int *sm, int *sd, int *sy2, int *sm2, int *sd2);
引理:公历y-m-d一定在农历y-m-d之前
由引理,我们的探测可以从公历y-m-d开始,步骤如下:
1)toLunar(y, m, d, &ly0, &lm0, &ld0);
2)估算ly0-lm0-ld0和ly-lm-ld的日子差N = (ly-ly0)*12*29 + (lm-lm0)*29 + (ld-ld0);
3)如果N > 0,把探测公历y-m-d加上这个天数,dateAddDays(&y, &m, &d, N);
4)如果N = 0,如果(ly==ly0)&&(lm==lm0)&&(ld==ld0)得到一个解sy=y;sm=m;sd=d; (如果此时sy已经被赋值过,则sy2=y;sm2=m;sd2=d;返回退出)
否则表示出现误差,修正N = 1;dateAddDays(&y, &m, &d, N);
从而开始探测闰月,令N = 30;dateAddDays(&y, &m, &d, N);
5)如果N < 0,且N >= -30,肯定是上次探测N+29不够(该月大),应该+30,
所以令N = 1; dateAddDays(&y, &m, &d, N);
经过测试,最多需要3次可以命中,还是比较理想的算法了:)
PS:经过完全测试,公元1500-2100年没有问,最多尝试次数为3次
#define LUNAR_TO_SOLAR_STEPS 3 // 公历转公历搜索最长步数
// 农历返回公历
// 注意:这是我在掌心万年历中写的代码,版权属于我所有,仅供个人学习使用,严禁在任何商业软件中使用本代码
// 输入农历年-月-日ly-lm-ld (lm < 0表示闰月)
// 输出公历年-月-日y-m-d
/*
公元1500年前的情况太特殊,比如公元9-1月,缺少农历11月,直接导致计算ldiff错误...
所以toSolar只支持1500年以后的转换. --ZV
*/
int toSolar(int LunarYear, int LunarMonth, int LunarDay)
{
if (LunarYear < 1500) { // 非常复杂的问题
return CALENDAR_ERROR;
}
int ret;
int y = LunarYear;
int m = abs(LunarMonth);
int d = LunarDay;
int i, ldiff = 0, lunDate;
int ly, lm, ld;
validateDate(&y, &m, &d);
for (i = 0; i < LUNAR_TO_SOLAR_STEPS; i++) {
lunDate = toLunar(y, m, d);
DATE_TO_YMD(lunDate, ly, lm, ld);
if (abs(lm) == abs(LunarMonth) && ld == LunarDay) {
if (lm == LunarMonth) {
ret = YMD_TO_DATE(y, m, d);
return ret;
}
// 如果找到一个,则开始探测闰月
ldiff = 30; // 要么直接转到,要么多了一天而已
} else {
ldiff = (LunarYear - ly) * 12 * 29 + (abs(LunarMonth) - abs(lm)) * 29 + (LunarDay - ld);
if (ldiff == 0) { // 稍做修正
ldiff = 1;
}
}
dateAddDays(&y, &m, &d, ldiff); // 注意可能出现负数}
return CALENDAR_ERROR;
}。