Lunar2.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "stdafx.h"
  2. #include "sys/timeb.h"
  3. int GetYearCode(int Year)
  4. {
  5. switch(Year)
  6. {
  7. case 1900:return 0x04bd8;
  8. case 1902:return 0x0a570;
  9. case 1904:return 0x0d260;
  10. case 1906:return 0x16554;
  11. case 1908:return 0x09ad0;
  12. case 1910:return 0x04ae0;
  13. case 1912:return 0x0a4d0;
  14. case 1914:return 0x1d255;
  15. case 1916:return 0x0d6a0;
  16. case 1918:return 0x095b0;
  17. case 1920:return 0x04970;
  18. case 1922:return 0x0b4b5;
  19. case 1924:return 0x06d40;
  20. case 1926:return 0x02b60;
  21. case 1928:return 0x052f2;
  22. case 1930:return 0x06566;
  23. case 1932:return 0x0ea50;
  24. case 1934:return 0x05ad0;
  25. case 1936:return 0x186e3;
  26. case 1938:return 0x1c8d7;
  27. case 1940:return 0x0d4a0;
  28. case 1942:return 0x0b550;
  29. case 1944:return 0x1a5b4;
  30. case 1946:return 0x092d0;
  31. case 1948:return 0x0a950;
  32. case 1950:return 0x06ca0;
  33. case 1952:return 0x15355;
  34. case 1954:return 0x0a5d0;
  35. case 1956:return 0x052d0;
  36. case 1958:return 0x0e950;
  37. case 1960:return 0x0aea6;
  38. case 1962:return 0x04b60;
  39. case 1964:return 0x0a570;
  40. case 1966:return 0x0f263;
  41. case 1968:return 0x05b57;
  42. case 1970:return 0x096d0;
  43. case 1972:return 0x04ad0;
  44. case 1974:return 0x0d4d4;
  45. case 1976:return 0x0d558;
  46. case 1978:return 0x0b5a0;
  47. case 1980:return 0x095b0;
  48. case 1982:return 0x0a974;
  49. case 1984:return 0x0b27a;
  50. case 1986:return 0x06d40;
  51. case 1988:return 0x0ab60;
  52. case 1990:return 0x04af5;
  53. case 1992:return 0x064b0;
  54. case 1994:return 0x0ea50;
  55. case 1996:return 0x055c0;
  56. case 1998:return 0x096d5;
  57. case 2000:return 0x0c960;
  58. case 2002:return 0x0d4a0;
  59. case 2004:return 0x07552;
  60. case 2006:return 0x0abb7;
  61. case 2008:return 0x092d0;
  62. case 2010:return 0x0a950;
  63. case 2012:return 0x0baa4;
  64. case 2014:return 0x055d9;
  65. case 2016:return 0x0a5b0;
  66. case 2018:return 0x052b0;
  67. case 2020:return 0x07954;
  68. case 2022:return 0x0ad50;
  69. case 2024:return 0x04b60;
  70. case 2026:return 0x0a4e0;
  71. case 2028:return 0x0ea65;
  72. case 2030:return 0x05aa0;
  73. case 2032:return 0x096d0;
  74. case 2034:return 0x04ad0;
  75. case 2036:return 0x1d0b6;
  76. case 2038:return 0x0d520;
  77. case 2040:return 0x0b5a0;
  78. case 2042:return 0x055b2;
  79. case 2044:return 0x0a577;
  80. case 2046:return 0x0aa50;
  81. case 2048:return 0x06d20;
  82. case 1901:return 0x04ae0;
  83. case 1903:return 0x054d5;
  84. case 1905:return 0x0d950;
  85. case 1907:return 0x056a0;
  86. case 1909:return 0x055d2;
  87. case 1911:return 0x0a5b6;
  88. case 1913:return 0x0d250;
  89. case 1915:return 0x0b540;
  90. case 1917:return 0x0ada2;
  91. case 1919:return 0x14977;
  92. case 1921:return 0x0a4b0;
  93. case 1923:return 0x06a50;
  94. case 1925:return 0x1ab54;
  95. case 1927:return 0x09570;
  96. case 1929:return 0x04970;
  97. case 1931:return 0x0d4a0;
  98. case 1933:return 0x06e95;
  99. case 1935:return 0x02b60;
  100. case 1937:return 0x092e0;
  101. case 1939:return 0x0c950;
  102. case 1941:return 0x1d8a6;
  103. case 1943:return 0x056a0;
  104. case 1945:return 0x025d0;
  105. case 1947:return 0x0d2b2;
  106. case 1949:return 0x0b557;
  107. case 1951:return 0x0b550;
  108. case 1953:return 0x04da0;
  109. case 1955:return 0x14573;
  110. case 1957:return 0x0a9a8;
  111. case 1959:return 0x06aa0;
  112. case 1961:return 0x0ab50;
  113. case 1963:return 0x0aae4;
  114. case 1965:return 0x05260;
  115. case 1967:return 0x0d950;
  116. case 1969:return 0x056a0;
  117. case 1971:return 0x04dd5;
  118. case 1973:return 0x0a4d0;
  119. case 1975:return 0x0d250;
  120. case 1977:return 0x0b540;
  121. case 1979:return 0x195a6;
  122. case 1981:return 0x049b0;
  123. case 1983:return 0x0a4b0;
  124. case 1985:return 0x06a50;
  125. case 1987:return 0x0af46;
  126. case 1989:return 0x09570;
  127. case 1991:return 0x04970;
  128. case 1993:return 0x074a3;
  129. case 1995:return 0x06b58;
  130. case 1997:return 0x0ab60;
  131. case 1999:return 0x092e0;
  132. case 2001:return 0x0d954;
  133. case 2003:return 0x0da50;
  134. case 2005:return 0x056a0;
  135. case 2007:return 0x025d0;
  136. case 2009:return 0x0cab5;
  137. case 2011:return 0x0b4a0;
  138. case 2013:return 0x0ad50;
  139. case 2015:return 0x04ba0;
  140. case 2017:return 0x15176;
  141. case 2019:return 0x0a930;
  142. case 2021:return 0x06aa0;
  143. case 2023:return 0x05b52;
  144. case 2025:return 0x0a6e6;
  145. case 2027:return 0x0d260;
  146. case 2029:return 0x0d530;
  147. case 2031:return 0x076a3;
  148. case 2033:return 0x04bd7;
  149. case 2035:return 0x0a4d0;
  150. case 2037:return 0x0d250;
  151. case 2039:return 0x0dd45;
  152. case 2041:return 0x056d0;
  153. case 2043:return 0x049b0;
  154. case 2045:return 0x0a4b0;
  155. case 2047:return 0x1b255;
  156. case 2049:return 0x0ada0;
  157. default:return 0;
  158. }
  159. }
  160. int GetDaysOfMonth(int _year, int _month)
  161. {
  162. switch(_month)
  163. {
  164. case 1:
  165. case 3:
  166. case 5:
  167. case 7:
  168. case 8:
  169. case 10:
  170. case 12:
  171. return 31;
  172. case 4:
  173. case 6:
  174. case 9:
  175. case 11:
  176. return 30;
  177. case 2:
  178. if((_year % 4==0&&_year % 100 !=0)|| _year % 400==0)return 29;
  179. else return 28;
  180. default: return 0;
  181. }
  182. }
  183. int YearLunar=0,MonthLunar=0,DayLunar=0;
  184. int GetDaySpan(int curYear,int curMonth,int curDay,int orgYear,int orgMonth,int orgDay)
  185. {
  186. int i;
  187. int TotalMilli=0;
  188. //计算时间内年份时间
  189. int _syear=orgYear;
  190. int _eyear=curYear-1;
  191. for(;_syear<=_eyear;_syear++)
  192. {
  193. if((_syear % 4==0&&_syear % 100 !=0)|| _syear % 400==0)
  194. TotalMilli+=366;
  195. else TotalMilli+=365;
  196. }
  197. //计算当年已过去的时间
  198. _syear=orgYear;
  199. int _smonth=orgMonth;
  200. int _sday=orgDay;
  201. for(i=1;i<_smonth;i++)
  202. {
  203. TotalMilli-=GetDaysOfMonth(_syear, i);
  204. }
  205. TotalMilli-=(_sday-1);
  206. //计算最后的时间
  207. _syear=curYear;
  208. _smonth=curMonth;
  209. _sday=curDay;
  210. for(i=1;i<_smonth;i++)
  211. {
  212. TotalMilli+=GetDaysOfMonth(_syear, i);
  213. }
  214. TotalMilli+=(_sday-1);
  215. return TotalMilli;
  216. }
  217. void fn_GetLunar(int Year,int Month,int Day)
  218. {
  219. int offset;
  220. int iLunar;
  221. int i;
  222. int j;
  223. int yDays;
  224. int mDays;
  225. int mLeap;
  226. int mLeapNum;
  227. int bLeap;
  228. int temp;
  229. //保证传进来的日期是不带时间 1900-01-30
  230. offset=GetDaySpan(Year,Month,Day,1900,1,30);
  231. //a确定农历年开始
  232. i=1900;
  233. //aoffset=@solData
  234. while(i<2050&&offset>0)
  235. {
  236. yDays=348;
  237. mLeapNum=0;
  238. iLunar=GetYearCode(i);
  239. //a传回农历年的总天数
  240. j=32768;
  241. while(j>8)
  242. {
  243. if((iLunar&j)>0)
  244. yDays=yDays+1;
  245. j=j/2;
  246. }
  247. //a传回农历年闰哪个月 1-12 , 没闰传回 0
  248. mLeap=(iLunar&15);
  249. //a传回农历年闰月的天数 ,加在年的总天数上
  250. if(mLeap>0)
  251. {
  252. if((iLunar&65536)>0)
  253. mLeapNum=30;
  254. else
  255. mLeapNum=29;
  256. yDays=yDays+mLeapNum;
  257. }
  258. offset=offset-yDays;
  259. i=i+1;
  260. }
  261. if(offset<=0)
  262. {
  263. offset=offset+yDays;
  264. i=i-1;
  265. }
  266. //a确定农历年结束
  267. YearLunar=i;
  268. //a确定农历月开始
  269. i=1;
  270. iLunar=GetYearCode(YearLunar);
  271. //a判断那个月是润月
  272. mLeap=(iLunar&15);
  273. bLeap=0;
  274. while(i<13&&offset>0)
  275. {
  276. //a判断润月
  277. mDays=0;
  278. if(mLeap>0&&i==(mLeap+1)&&bLeap==0)
  279. {//a是润月
  280. i=i-1;
  281. bLeap=1;
  282. //a传回农历年闰月的天数
  283. if((iLunar&65536)>0)
  284. mDays=30;
  285. else
  286. mDays=29;
  287. }
  288. else
  289. //a不是润月
  290. {
  291. j=1;
  292. temp=65536;
  293. while(j<=i)
  294. {
  295. temp=temp/2;
  296. j=j+1;
  297. }
  298. if((iLunar&temp)>0)
  299. mDays=30;
  300. else
  301. mDays=29;
  302. }
  303. //a解除闰月
  304. if(bLeap==1&&i==mLeap+1)
  305. bLeap=0;
  306. offset=offset-mDays;
  307. i=i+1;
  308. }
  309. if(offset<=0)
  310. {
  311. offset=offset+mDays;
  312. i=i-1;
  313. }
  314. //a确定农历月结束
  315. MonthLunar=i;
  316. //a确定农历日结束
  317. DayLunar=offset;
  318. }
  319. void GongToLong(CString &date)
  320. {
  321. fn_GetLunar( atoi(date.Mid (0,4)), atoi(date.Mid (5,2)), atoi(date.Mid (8,2)));
  322. date.Format ("%04d-%02d-%02d", YearLunar, MonthLunar, DayLunar);
  323. }