No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LunarYear.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Lunar.Util;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. // ReSharper disable InconsistentNaming
  8. // ReSharper disable MemberCanBePrivate.Global
  9. // ReSharper disable IdentifierTypo
  10. namespace Lunar
  11. {
  12. /// <summary>
  13. /// 农历年
  14. /// </summary>
  15. public class LunarYear
  16. {
  17. /// <summary>
  18. /// 元
  19. /// </summary>
  20. private static readonly string[] YUAN = { "下", "上", "中" };
  21. /// <summary>
  22. /// 运
  23. /// </summary>
  24. private static readonly string[] YUN = { "七", "八", "九", "一", "二", "三", "四", "五", "六" };
  25. /// <summary>
  26. /// 闰冬月年份
  27. /// </summary>
  28. private static readonly int[] LEAP_11 = { 75, 94, 170, 265, 322, 398, 469, 553, 583, 610, 678, 735, 754, 773, 849, 887, 936, 1050, 1069, 1126, 1145, 1164, 1183, 1259, 1278, 1308, 1373, 1403, 1441, 1460, 1498, 1555, 1593, 1612, 1631, 1642, 2033, 2128, 2147, 2242, 2614, 2728, 2910, 3062, 3244, 3339, 3616, 3711, 3730, 3825, 4007, 4159, 4197, 4322, 4341, 4379, 4417, 4531, 4599, 4694, 4713, 4789, 4808, 4971, 5085, 5104, 5161, 5180, 5199, 5294, 5305, 5476, 5677, 5696, 5772, 5791, 5848, 5886, 6049, 6068, 6144, 6163, 6258, 6402, 6440, 6497, 6516, 6630, 6641, 6660, 6679, 6736, 6774, 6850, 6869, 6899, 6918, 6994, 7013, 7032, 7051, 7070, 7089, 7108, 7127, 7146, 7222, 7271, 7290, 7309, 7366, 7385, 7404, 7442, 7461, 7480, 7491, 7499, 7594, 7624, 7643, 7662, 7681, 7719, 7738, 7814, 7863, 7882, 7901, 7939, 7958, 7977, 7996, 8034, 8053, 8072, 8091, 8121, 8159, 8186, 8216, 8235, 8254, 8273, 8311, 8330, 8341, 8349, 8368, 8444, 8463, 8474, 8493, 8531, 8569, 8588, 8626, 8664, 8683, 8694, 8702, 8713, 8721, 8751, 8789, 8808, 8816, 8827, 8846, 8884, 8903, 8922, 8941, 8971, 9036, 9066, 9085, 9104, 9123, 9142, 9161, 9180, 9199, 9218, 9256, 9294, 9313, 9324, 9343, 9362, 9381, 9419, 9438, 9476, 9514, 9533, 9544, 9552, 9563, 9571, 9582, 9601, 9639, 9658, 9666, 9677, 9696, 9734, 9753, 9772, 9791, 9802, 9821, 9886, 9897, 9916, 9935, 9954, 9973, 9992 };
  29. /// <summary>
  30. /// 闰腊月年份
  31. /// </summary>
  32. private static readonly int[] LEAP_12 = { 37, 56, 113, 132, 151, 189, 208, 227, 246, 284, 303, 341, 360, 379, 417, 436, 458, 477, 496, 515, 534, 572, 591, 629, 648, 667, 697, 716, 792, 811, 830, 868, 906, 925, 944, 963, 982, 1001, 1020, 1039, 1058, 1088, 1153, 1202, 1221, 1240, 1297, 1335, 1392, 1411, 1422, 1430, 1517, 1525, 1536, 1574, 3358, 3472, 3806, 3988, 4751, 4941, 5066, 5123, 5275, 5343, 5438, 5457, 5495, 5533, 5552, 5715, 5810, 5829, 5905, 5924, 6421, 6535, 6793, 6812, 6888, 6907, 7002, 7184, 7260, 7279, 7374, 7556, 7746, 7757, 7776, 7833, 7852, 7871, 7966, 8015, 8110, 8129, 8148, 8224, 8243, 8338, 8406, 8425, 8482, 8501, 8520, 8558, 8596, 8607, 8615, 8645, 8740, 8778, 8835, 8865, 8930, 8960, 8979, 8998, 9017, 9055, 9074, 9093, 9112, 9150, 9188, 9237, 9275, 9332, 9351, 9370, 9408, 9427, 9446, 9457, 9465, 9495, 9560, 9590, 9628, 9647, 9685, 9715, 9742, 9780, 9810, 9818, 9829, 9848, 9867, 9905, 9924, 9943, 9962, 10000 };
  33. /// <summary>
  34. /// 月份
  35. /// </summary>
  36. private static readonly int[] YMC = { 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  37. /// <summary>
  38. /// 缓存年
  39. /// </summary>
  40. private static LunarYear cacheYear;
  41. private static readonly Mutex mutex = new Mutex();
  42. /// <summary>
  43. /// 年
  44. /// </summary>
  45. public int Year { get; }
  46. /// <summary>
  47. /// 天干下标
  48. /// </summary>
  49. public int GanIndex { get; }
  50. /// <summary>
  51. /// 地支下标
  52. /// </summary>
  53. public int ZhiIndex { get; }
  54. /// <summary>
  55. /// 月
  56. /// </summary>
  57. public List<LunarMonth> Months { get; } = new List<LunarMonth>();
  58. /// <summary>
  59. /// 节气儒略日
  60. /// </summary>
  61. public List<double> JieQiJulianDays { get; } = new List<double>();
  62. /// <summary>
  63. /// 通过农历年初始化
  64. /// </summary>
  65. /// <param name="lunarYear">农历年</param>
  66. public LunarYear(int lunarYear)
  67. {
  68. Year = lunarYear;
  69. var offset = lunarYear - 4;
  70. var yearGanIndex = offset % 10;
  71. var yearZhiIndex = offset % 12;
  72. if (yearGanIndex < 0)
  73. {
  74. yearGanIndex += 10;
  75. }
  76. if (yearZhiIndex < 0)
  77. {
  78. yearZhiIndex += 12;
  79. }
  80. GanIndex = yearGanIndex;
  81. ZhiIndex = yearZhiIndex;
  82. Compute();
  83. }
  84. /// <summary>
  85. /// 通过农历年初始化
  86. /// </summary>
  87. /// <param name="lunarYear">农历年</param>
  88. /// <returns>农历年</returns>
  89. public static LunarYear FromYear(int lunarYear)
  90. {
  91. mutex.WaitOne();
  92. try
  93. {
  94. LunarYear y;
  95. if (null == cacheYear || cacheYear.Year != lunarYear)
  96. {
  97. y = new LunarYear(lunarYear);
  98. cacheYear = y;
  99. }
  100. else
  101. {
  102. y = cacheYear;
  103. }
  104. return y;
  105. }
  106. finally
  107. {
  108. mutex.ReleaseMutex();
  109. }
  110. }
  111. private void Compute()
  112. {
  113. // 节气
  114. var jq = new double[27];
  115. // 合朔,即每月初一
  116. var hs = new double[16];
  117. // 每月天数
  118. var dayCounts = new int[15];
  119. var months = new int[15];
  120. var currentYear = Year;
  121. var jd = Math.Floor((currentYear - 2000) * 365.2422 + 180);
  122. // 355是2000.12冬至,得到较靠近jd的冬至估计值
  123. var w = Math.Floor((jd - 355 + 183) / 365.2422) * 365.2422 + 355;
  124. if (ShouXingUtil.CalcQi(w) > jd)
  125. {
  126. w -= 365.2422;
  127. }
  128. // 25个节气时刻(北京时间),从冬至开始到下一个冬至以后
  129. for (var i = 0; i < 26; i++)
  130. {
  131. jq[i] = ShouXingUtil.CalcQi(w + 15.2184 * i);
  132. }
  133. // 从上年的大雪到下年的立春
  134. for (int i = 0, j = Lunar.JIE_QI_IN_USE.Length; i < j; i++)
  135. {
  136. if (i == 0)
  137. {
  138. jd = ShouXingUtil.QiAccurate2(jq[0] - 15.2184);
  139. }
  140. else if (i <= 26)
  141. {
  142. jd = ShouXingUtil.QiAccurate2(jq[i - 1]);
  143. }
  144. else
  145. {
  146. jd = ShouXingUtil.QiAccurate2(jq[25] + 15.2184 * (i - 26));
  147. }
  148. JieQiJulianDays.Add(jd + Solar.J2000);
  149. }
  150. // 冬至前的初一,今年"首朔"的日月黄经差w
  151. w = ShouXingUtil.CalcShuo(jq[0]);
  152. if (w > jq[0])
  153. {
  154. w -= 29.53;
  155. }
  156. // 递推每月初一
  157. for (var i = 0; i < 16; i++)
  158. {
  159. hs[i] = ShouXingUtil.CalcShuo(w + 29.5306 * i);
  160. }
  161. // 每月
  162. for (var i = 0; i < 15; i++)
  163. {
  164. dayCounts[i] = (int) (hs[i + 1] - hs[i]);
  165. months[i] = i;
  166. }
  167. var prevYear = currentYear - 1;
  168. var leapIndex = 16;
  169. if (LEAP_11.Contains(currentYear))
  170. {
  171. leapIndex = 13;
  172. }
  173. else if (LEAP_12.Contains(currentYear))
  174. {
  175. leapIndex = 14;
  176. }
  177. else if (hs[13] <= jq[24])
  178. {
  179. var i = 1;
  180. while (hs[i + 1] > jq[2 * i] && i < 13)
  181. {
  182. i++;
  183. }
  184. leapIndex = i;
  185. }
  186. for (var i = leapIndex; i < 15; i++)
  187. {
  188. months[i] -= 1;
  189. }
  190. var fm = -1;
  191. var index = -1;
  192. var y = prevYear;
  193. for (var i = 0; i < 15; i++)
  194. {
  195. var dm = hs[i] + Solar.J2000;
  196. var v2 = months[i];
  197. var mc = YMC[v2 % 12];
  198. if (1724360 <= dm && dm < 1729794)
  199. {
  200. mc = YMC[(v2 + 1) % 12];
  201. }
  202. else if (1807724 <= dm && dm < 1808699)
  203. {
  204. mc = YMC[(v2 + 1) % 12];
  205. }
  206. else if (Math.Abs(dm - 1729794) < 0.00000000001 || Math.Abs(dm - 1808699) < 0.00000000001)
  207. {
  208. mc = 12;
  209. }
  210. if (fm == -1)
  211. {
  212. fm = mc;
  213. index = mc;
  214. }
  215. if (mc < fm)
  216. {
  217. y += 1;
  218. index = 1;
  219. }
  220. fm = mc;
  221. if (i == leapIndex)
  222. {
  223. mc = -mc;
  224. }
  225. else if (Math.Abs(dm - 1729794) < 0.00000000001 || Math.Abs(dm - 1808699) < 0.00000000001)
  226. {
  227. mc = -11;
  228. }
  229. Months.Add(new LunarMonth(y, mc, dayCounts[i], hs[i] + Solar.J2000, index));
  230. index++;
  231. }
  232. }
  233. /// <summary>
  234. /// 天干
  235. /// </summary>
  236. public string Gan => LunarUtil.GAN[GanIndex + 1];
  237. /// <summary>
  238. /// 地支
  239. /// </summary>
  240. public string Zhi => LunarUtil.ZHI[ZhiIndex + 1];
  241. /// <summary>
  242. /// 获取干支
  243. /// </summary>
  244. public string GanZhi => $"{Gan}{Zhi}";
  245. /// <summary>
  246. /// 获取月份
  247. /// </summary>
  248. /// <param name="lunarMonth">月</param>
  249. /// <returns>农历月</returns>
  250. public LunarMonth GetMonth(int lunarMonth)
  251. {
  252. return Months.FirstOrDefault(m => m.Year == Year && m.Month == lunarMonth);
  253. }
  254. /// <summary>
  255. /// 闰月
  256. /// </summary>
  257. public int LeapMonth => (from m in Months where m.Year == Year && m.Leap select Math.Abs(m.Month)).FirstOrDefault();
  258. /// <summary>
  259. /// 获取总天数
  260. /// </summary>
  261. public int DayCount => Months.Where(m => m.Year == Year).Sum(m => m.DayCount);
  262. /// <summary>
  263. /// 获取当年的农历月们
  264. /// </summary>
  265. public List<LunarMonth> MonthsInYear => Months.Where(m => m.Year == Year).ToList();
  266. /// <inheritdoc />
  267. public override string ToString()
  268. {
  269. return $"{Year}";
  270. }
  271. /// <summary>
  272. /// 完整字符串输出
  273. /// </summary>
  274. public string FullString => $"{Year}年";
  275. /// <summary>
  276. /// 按天干计算灶马头
  277. /// </summary>
  278. /// <param name="index">天干序号</param>
  279. /// <param name="name">名称</param>
  280. /// <returns>灶马头</returns>
  281. protected string GetZaoByGan(int index, string name)
  282. {
  283. var offset = index - Solar.FromJulianDay(GetMonth(1).FirstJulianDay).Lunar.DayGanIndex;
  284. if (offset < 0)
  285. {
  286. offset += 10;
  287. }
  288. return new Regex("几", RegexOptions.Singleline).Replace(name, LunarUtil.NUMBER[offset + 1], 1);
  289. }
  290. /// <summary>
  291. /// 按地支计算灶马头
  292. /// </summary>
  293. /// <param name="index">地支序号</param>
  294. /// <param name="name">名称</param>
  295. /// <returns>灶马头</returns>
  296. protected string GetZaoByZhi(int index, string name)
  297. {
  298. var offset = index - Solar.FromJulianDay(GetMonth(1).FirstJulianDay).Lunar.DayZhiIndex;
  299. if (offset < 0)
  300. {
  301. offset += 12;
  302. }
  303. return new Regex("几", RegexOptions.Singleline).Replace(name, LunarUtil.NUMBER[offset + 1], 1);
  304. }
  305. /// <summary>
  306. /// 几鼠偷粮
  307. /// </summary>
  308. /// <returns>几鼠偷粮</returns>
  309. public string TouLiang => GetZaoByZhi(0, "几鼠偷粮");
  310. /// <summary>
  311. /// 草子几分
  312. /// </summary>
  313. public string CaoZi => GetZaoByZhi(0, "草子几分");
  314. /// <summary>
  315. /// 几牛耕田(正月第一个丑日是初几,就是几牛耕田)
  316. /// </summary>
  317. public string GengTian => GetZaoByZhi(1, "几牛耕田");
  318. /// <summary>
  319. /// 花收几分
  320. /// </summary>
  321. public string HuaShou => GetZaoByZhi(3, "花收几分");
  322. /// <summary>
  323. /// 几龙治水(正月第一个辰日是初几,就是几龙治水)
  324. /// </summary>
  325. public string ZhiShui => GetZaoByZhi(4, "几龙治水");
  326. /// <summary>
  327. /// 几马驮谷
  328. /// </summary>
  329. public string TuoGu => GetZaoByZhi(6, "几马驮谷");
  330. /// <summary>
  331. /// 几鸡抢米
  332. /// </summary>
  333. public string QiangMi => GetZaoByZhi(9, "几鸡抢米");
  334. /// <summary>
  335. /// 几姑看蚕
  336. /// </summary>
  337. public string KanCan => GetZaoByZhi(9, "几姑看蚕");
  338. /// <summary>
  339. /// 几屠共猪
  340. /// </summary>
  341. public string GongZhu => GetZaoByZhi(11, "几屠共猪");
  342. /// <summary>
  343. /// 甲田几分
  344. /// </summary>
  345. public string JiaTian => GetZaoByGan(0, "甲田几分");
  346. /// <summary>
  347. /// 几人分饼(正月第一个丙日是初几,就是几人分饼)
  348. /// </summary>
  349. public string FenBing => GetZaoByGan(2, "几人分饼");
  350. /// <summary>
  351. /// 几日得金(正月第一个辛日是初几,就是几日得金)
  352. /// </summary>
  353. public string DeJin => GetZaoByGan(7, "几日得金");
  354. /// <summary>
  355. /// 几人几丙
  356. /// </summary>
  357. public string RenBing => GetZaoByGan(2, GetZaoByZhi(2, "几人几丙"));
  358. /// <summary>
  359. /// 几人几锄
  360. /// </summary>
  361. public string RenChu => GetZaoByGan(3, GetZaoByZhi(2, "几人几锄"));
  362. /// <summary>
  363. /// 三元
  364. /// </summary>
  365. public string Yuan => YUAN[(Year + 2696) / 60 % 3] + "元";
  366. /// <summary>
  367. /// 九运
  368. /// </summary>
  369. public string Yun => YUN[(Year + 2696) / 20 % 9] + "运";
  370. /// <summary>
  371. /// 九星
  372. /// </summary>
  373. public NineStar NineStar
  374. {
  375. get
  376. {
  377. var index = LunarUtil.GetJiaZiIndex(GanZhi) + 1;
  378. var yuan = (Year + 2696) / 60 % 3;
  379. var offset = (62 + yuan * 3 - index) % 9;
  380. if (0 == offset)
  381. {
  382. offset = 9;
  383. }
  384. return NineStar.FromIndex(offset - 1);
  385. }
  386. }
  387. /// <summary>
  388. /// 喜神方位,如艮
  389. /// </summary>
  390. public string PositionXi => LunarUtil.POSITION_XI[GanIndex + 1];
  391. /// <summary>
  392. /// 喜神方位描述,如东北
  393. /// </summary>
  394. public string PositionXiDesc => LunarUtil.POSITION_DESC[PositionXi];
  395. /// <summary>
  396. /// 阳贵神方位,如艮
  397. /// </summary>
  398. public string PositionYangGui => LunarUtil.POSITION_YANG_GUI[GanIndex + 1];
  399. /// <summary>
  400. /// 阳贵神方位描述,如东北
  401. /// </summary>
  402. public string PositionYangGuiDesc => LunarUtil.POSITION_DESC[PositionYangGui];
  403. /// <summary>
  404. /// 阴贵神方位,如艮
  405. /// </summary>
  406. public string PositionYinGui => LunarUtil.POSITION_YIN_GUI[GanIndex + 1];
  407. /// <summary>
  408. /// 阴贵神方位描述,如东北
  409. /// </summary>
  410. public string PositionYinGuiDesc => LunarUtil.POSITION_DESC[PositionYinGui];
  411. /// <summary>
  412. /// 福神方位,如艮(流派2)
  413. /// </summary>
  414. public string PositionFu => GetPositionFu();
  415. /// <summary>
  416. /// 福神方位描述,如东北(流派2)
  417. /// </summary>
  418. public string PositionFuDesc => GetPositionFuDesc();
  419. /// <summary>
  420. /// 获取福神方位
  421. /// </summary>
  422. /// <param name="sect">流派,1或2</param>
  423. /// <returns>方位,如艮</returns>
  424. public string GetPositionFu(int sect = 2)
  425. {
  426. return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[GanIndex + 1];
  427. }
  428. /// <summary>
  429. /// 获取福神方位描述
  430. /// </summary>
  431. /// <param name="sect">流派,1或2</param>
  432. /// <returns>方位描述,如东北</returns>
  433. public string GetPositionFuDesc(int sect = 2)
  434. {
  435. return LunarUtil.POSITION_DESC[GetPositionFu(sect)];
  436. }
  437. /// <summary>
  438. /// 财神方位,如艮
  439. /// </summary>
  440. public string PositionCai => LunarUtil.POSITION_CAI[GanIndex + 1];
  441. /// <summary>
  442. /// 财神方位描述,如东北
  443. /// </summary>
  444. public string PositionCaiDesc => LunarUtil.POSITION_DESC[PositionCai];
  445. /// <summary>
  446. /// 太岁方位,如艮
  447. /// </summary>
  448. public string PositionTaiSui => LunarUtil.POSITION_TAI_SUI_YEAR[ZhiIndex];
  449. /// <summary>
  450. /// 太岁方位描述,如东北
  451. /// </summary>
  452. public string PositionTaiSuiDesc => LunarUtil.POSITION_DESC[PositionTaiSui];
  453. /// <summary>
  454. /// 往后推几年的阴历年,如果要往前推,则年数用负数
  455. /// </summary>
  456. /// <param name="n">年数</param>
  457. /// <returns>阴历年</returns>
  458. public LunarYear Next(int n)
  459. {
  460. return FromYear(Year + n);
  461. }
  462. }
  463. }