#include "stdafx.h" #include "sys/timeb.h" int GetYearCode(int Year) { switch(Year) { case 1900:return 0x04bd8; case 1902:return 0x0a570; case 1904:return 0x0d260; case 1906:return 0x16554; case 1908:return 0x09ad0; case 1910:return 0x04ae0; case 1912:return 0x0a4d0; case 1914:return 0x1d255; case 1916:return 0x0d6a0; case 1918:return 0x095b0; case 1920:return 0x04970; case 1922:return 0x0b4b5; case 1924:return 0x06d40; case 1926:return 0x02b60; case 1928:return 0x052f2; case 1930:return 0x06566; case 1932:return 0x0ea50; case 1934:return 0x05ad0; case 1936:return 0x186e3; case 1938:return 0x1c8d7; case 1940:return 0x0d4a0; case 1942:return 0x0b550; case 1944:return 0x1a5b4; case 1946:return 0x092d0; case 1948:return 0x0a950; case 1950:return 0x06ca0; case 1952:return 0x15355; case 1954:return 0x0a5d0; case 1956:return 0x052d0; case 1958:return 0x0e950; case 1960:return 0x0aea6; case 1962:return 0x04b60; case 1964:return 0x0a570; case 1966:return 0x0f263; case 1968:return 0x05b57; case 1970:return 0x096d0; case 1972:return 0x04ad0; case 1974:return 0x0d4d4; case 1976:return 0x0d558; case 1978:return 0x0b5a0; case 1980:return 0x095b0; case 1982:return 0x0a974; case 1984:return 0x0b27a; case 1986:return 0x06d40; case 1988:return 0x0ab60; case 1990:return 0x04af5; case 1992:return 0x064b0; case 1994:return 0x0ea50; case 1996:return 0x055c0; case 1998:return 0x096d5; case 2000:return 0x0c960; case 2002:return 0x0d4a0; case 2004:return 0x07552; case 2006:return 0x0abb7; case 2008:return 0x092d0; case 2010:return 0x0a950; case 2012:return 0x0baa4; case 2014:return 0x055d9; case 2016:return 0x0a5b0; case 2018:return 0x052b0; case 2020:return 0x07954; case 2022:return 0x0ad50; case 2024:return 0x04b60; case 2026:return 0x0a4e0; case 2028:return 0x0ea65; case 2030:return 0x05aa0; case 2032:return 0x096d0; case 2034:return 0x04ad0; case 2036:return 0x1d0b6; case 2038:return 0x0d520; case 2040:return 0x0b5a0; case 2042:return 0x055b2; case 2044:return 0x0a577; case 2046:return 0x0aa50; case 2048:return 0x06d20; case 1901:return 0x04ae0; case 1903:return 0x054d5; case 1905:return 0x0d950; case 1907:return 0x056a0; case 1909:return 0x055d2; case 1911:return 0x0a5b6; case 1913:return 0x0d250; case 1915:return 0x0b540; case 1917:return 0x0ada2; case 1919:return 0x14977; case 1921:return 0x0a4b0; case 1923:return 0x06a50; case 1925:return 0x1ab54; case 1927:return 0x09570; case 1929:return 0x04970; case 1931:return 0x0d4a0; case 1933:return 0x06e95; case 1935:return 0x02b60; case 1937:return 0x092e0; case 1939:return 0x0c950; case 1941:return 0x1d8a6; case 1943:return 0x056a0; case 1945:return 0x025d0; case 1947:return 0x0d2b2; case 1949:return 0x0b557; case 1951:return 0x0b550; case 1953:return 0x04da0; case 1955:return 0x14573; case 1957:return 0x0a9a8; case 1959:return 0x06aa0; case 1961:return 0x0ab50; case 1963:return 0x0aae4; case 1965:return 0x05260; case 1967:return 0x0d950; case 1969:return 0x056a0; case 1971:return 0x04dd5; case 1973:return 0x0a4d0; case 1975:return 0x0d250; case 1977:return 0x0b540; case 1979:return 0x195a6; case 1981:return 0x049b0; case 1983:return 0x0a4b0; case 1985:return 0x06a50; case 1987:return 0x0af46; case 1989:return 0x09570; case 1991:return 0x04970; case 1993:return 0x074a3; case 1995:return 0x06b58; case 1997:return 0x0ab60; case 1999:return 0x092e0; case 2001:return 0x0d954; case 2003:return 0x0da50; case 2005:return 0x056a0; case 2007:return 0x025d0; case 2009:return 0x0cab5; case 2011:return 0x0b4a0; case 2013:return 0x0ad50; case 2015:return 0x04ba0; case 2017:return 0x15176; case 2019:return 0x0a930; case 2021:return 0x06aa0; case 2023:return 0x05b52; case 2025:return 0x0a6e6; case 2027:return 0x0d260; case 2029:return 0x0d530; case 2031:return 0x076a3; case 2033:return 0x04bd7; case 2035:return 0x0a4d0; case 2037:return 0x0d250; case 2039:return 0x0dd45; case 2041:return 0x056d0; case 2043:return 0x049b0; case 2045:return 0x0a4b0; case 2047:return 0x1b255; case 2049:return 0x0ada0; default:return 0; } } int GetDaysOfMonth(int _year, int _month) { switch(_month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if((_year % 4==0&&_year % 100 !=0)|| _year % 400==0)return 29; else return 28; default: return 0; } } int YearLunar=0,MonthLunar=0,DayLunar=0; int GetDaySpan(int curYear,int curMonth,int curDay,int orgYear,int orgMonth,int orgDay) { int i; int TotalMilli=0; //计算时间内年份时间 int _syear=orgYear; int _eyear=curYear-1; for(;_syear<=_eyear;_syear++) { if((_syear % 4==0&&_syear % 100 !=0)|| _syear % 400==0) TotalMilli+=366; else TotalMilli+=365; } //计算当年已过去的时间 _syear=orgYear; int _smonth=orgMonth; int _sday=orgDay; for(i=1;i<_smonth;i++) { TotalMilli-=GetDaysOfMonth(_syear, i); } TotalMilli-=(_sday-1); //计算最后的时间 _syear=curYear; _smonth=curMonth; _sday=curDay; for(i=1;i<_smonth;i++) { TotalMilli+=GetDaysOfMonth(_syear, i); } TotalMilli+=(_sday-1); return TotalMilli; } void fn_GetLunar(int Year,int Month,int Day) { int offset; int iLunar; int i; int j; int yDays; int mDays; int mLeap; int mLeapNum; int bLeap; int temp; //保证传进来的日期是不带时间 1900-01-30 offset=GetDaySpan(Year,Month,Day,1900,1,30); //a确定农历年开始 i=1900; //aoffset=@solData while(i<2050&&offset>0) { yDays=348; mLeapNum=0; iLunar=GetYearCode(i); //a传回农历年的总天数 j=32768; while(j>8) { if((iLunar&j)>0) yDays=yDays+1; j=j/2; } //a传回农历年闰哪个月 1-12 , 没闰传回 0 mLeap=(iLunar&15); //a传回农历年闰月的天数 ,加在年的总天数上 if(mLeap>0) { if((iLunar&65536)>0) mLeapNum=30; else mLeapNum=29; yDays=yDays+mLeapNum; } offset=offset-yDays; i=i+1; } if(offset<=0) { offset=offset+yDays; i=i-1; } //a确定农历年结束 YearLunar=i; //a确定农历月开始 i=1; iLunar=GetYearCode(YearLunar); //a判断那个月是润月 mLeap=(iLunar&15); bLeap=0; while(i<13&&offset>0) { //a判断润月 mDays=0; if(mLeap>0&&i==(mLeap+1)&&bLeap==0) {//a是润月 i=i-1; bLeap=1; //a传回农历年闰月的天数 if((iLunar&65536)>0) mDays=30; else mDays=29; } else //a不是润月 { j=1; temp=65536; while(j<=i) { temp=temp/2; j=j+1; } if((iLunar&temp)>0) mDays=30; else mDays=29; } //a解除闰月 if(bLeap==1&&i==mLeap+1) bLeap=0; offset=offset-mDays; i=i+1; } if(offset<=0) { offset=offset+mDays; i=i-1; } //a确定农历月结束 MonthLunar=i; //a确定农历日结束 DayLunar=offset; } void GongToLong(CString &date) { fn_GetLunar( atoi(date.Mid (0,4)), atoi(date.Mid (5,2)), atoi(date.Mid (8,2))); date.Format ("%04d-%02d-%02d", YearLunar, MonthLunar, DayLunar); }