calendar.uts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * 农历1900-2100的润大小信息表
  3. * @Array Of Property
  4. * @return Hex
  5. */
  6. /* 源数据说明:
  7. * lunarYear数据来自香港天文台提供的源数据反向推导得出,其中201项数据分别对应1900-2100年。
  8. * 示例: 2021年 -- 0x06aa0
  9. * ╭-------┰-------┰-------┰-------┰--------╮
  10. * ┆ 0000 ┆ 0110 ┆ 1010 ┆ 1010 ┆ 0000 ┆
  11. * ┠-------╊-------╊-------╊-------╊--------┨
  12. * ┆ 20-17 ┆ 16-12 ┆ 12-9 ┆ 8-5 ┆ 4-1 ┆
  13. * ╰-------┸-------┸-------┸-------┸--------╯
  14. * 1-4: 表示当年有无闰年,有的话,为闰月的月份,没有的话,为0。 2021年无闰月
  15. * 5-16:为除了闰月外的正常月份是大月还是小月,1为30天,0为29天。从1月到12月对应的是第16位到第5位,2021年各月天数[29,30,30,29,30,29,30,29,30,29,30,29]
  16. * 17-20: 表示闰月是大月还是小月,仅当存在闰月的情况下有意义。(0/1,即闰大/小月)
  17. */
  18. const lunarYears = [
  19. 0x04bd8,
  20. // 1901-2000
  21. 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0,
  22. 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970,
  23. 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
  24. 0x0d4a0, 0x0ea50, 0x16a95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0,
  25. 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0,
  26. 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6,
  27. 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0,
  28. 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, 0x095b0,
  29. 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5,
  30. 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x05ac0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960,
  31. // 2001-2100
  32. 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950,
  33. 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954,
  34. 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0,
  35. 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0,
  36. 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63,
  37. 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, 0x092e0,
  38. 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, 0x052d0,
  39. 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0, 0x0b273,
  40. 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160, 0x0e968,
  41. 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, 0x0d520
  42. ]
  43. // ['月','正','一','二','三','四','五','六','七','八','九','十','冬','腊'];
  44. const N_STR_3 = ["\u6708", "\u6b63", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u51ac", "\u814a"]
  45. // ['日','一','二','三','四','五','六','七','八','九','十']
  46. const N_STR_1 = ["\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341"]
  47. // ['初','十','廿','卅','闰']
  48. const N_STR_2 = ["\u521d", "\u5341", "\u5eff", "\u5345", "\u95f0"]
  49. export type InfoType = {
  50. lunarY : number;
  51. lunarM : number;
  52. lunarD : number;
  53. isLeap : boolean;
  54. }
  55. export type LunarInfoType = {
  56. lYear : number;
  57. lMonth : number;
  58. lDay : number;
  59. IMonthCn : string;
  60. IDayCn : string;
  61. cYear : number;
  62. cMonth : number;
  63. cDay : number;
  64. gzYear ?: string;
  65. gzMonth ?: string;
  66. gzDay ?: string;
  67. isToday : boolean;
  68. isLeap : boolean;
  69. nWeek ?: number;
  70. ncWeek ?: string;
  71. isTerm ?: boolean;
  72. Term ?: string;
  73. astro ?: string
  74. }
  75. export class Lunar {
  76. private lunarYearDaysMap = new Map<number, number>()
  77. private lunarMonthDaysMap = new Map<number, number[]>()
  78. constructor() { }
  79. /**
  80. * 传入农历数字月份返回汉语通俗表示法
  81. * @param lunar month
  82. * @return Cn string
  83. * @eg:let cnMonth = calendar.toChinaMonth(12) ;//cnMonth='腊月'
  84. */
  85. toChinaMonth(m : number, leap : boolean = false) : string { // 月 => \u6708
  86. return leap ? (N_STR_3[4] + N_STR_3[m] + N_STR_3[0]) : (N_STR_3[m] + N_STR_3[0]);
  87. }
  88. /**
  89. * 传入农历日期数字返回汉字表示法
  90. * @param lunar day
  91. * @return Cn string
  92. * @eg:let cnDay = calendar.toChinaDay(21) ;//cnMonth='廿一'
  93. */
  94. toChinaDay(d : number) : string { // 日 => \u65e5
  95. let s : string
  96. switch (d) {
  97. case 10:
  98. s = '\u521d\u5341';
  99. break
  100. case 20:
  101. s = '\u4e8c\u5341';
  102. break
  103. case 30:
  104. s = '\u4e09\u5341';
  105. break
  106. default:
  107. s = N_STR_2[Math.floor(d / 10)]
  108. s += N_STR_1[d % 10]
  109. }
  110. return (s)
  111. }
  112. /**
  113. * 返回农历y年闰月是哪个月;若y年没有闰月 则返回0
  114. * @param lunar Year
  115. * @return Number (0-12)
  116. * @eg:let leapMonth = calendar.leapMonth(1987) ;//leapMonth=6
  117. */
  118. leapMonth(year : number) : number {
  119. return lunarYears[year - 1900] & 0xF;
  120. }
  121. /**
  122. * 返回农历y年闰月的天数 若该年没有闰月则返回0
  123. * @param lunar Year
  124. * @return Number (0、29、30)
  125. * @eg:let leapMonthDay = calendar.leapDays(1987) ;//leapMonthDay=29
  126. */
  127. leapDays(year : number) : number {
  128. if (this.leapMonth(year) > 0) {
  129. return (lunarYears[year - 1900] & 0x10000) != 0 ? 30 : 29;
  130. }
  131. return 0;
  132. }
  133. // 某年份农历各月天数
  134. lunarMonthDays(year : number) : number[] {
  135. let monthDays = this.lunarMonthDaysMap.get(year)
  136. if (monthDays != null) {
  137. return monthDays
  138. }
  139. monthDays = [];
  140. let lunarYear = lunarYears[year - 1900];
  141. for (let i = 15; i >= 4; i--) {
  142. let monthDay = (lunarYear >> i & 0x1) != 0 ? 30 : 29;
  143. monthDays.push(monthDay);
  144. }
  145. // 添加闰月
  146. let leapM = this.leapMonth(year);
  147. if (leapM > 0) monthDays.splice(leapM, 0, this.leapDays(year));
  148. this.lunarMonthDaysMap.set(year, monthDays)
  149. return monthDays;
  150. }
  151. // 某年农历天数
  152. lunarYearDays(year : number) : number {
  153. if (this.lunarYearDaysMap.has(year)) {
  154. return this.lunarYearDaysMap.get(year)!
  155. }
  156. let num = 0;
  157. this.lunarMonthDays(year).forEach(item => {
  158. num += item;
  159. });
  160. this.lunarYearDaysMap.set(year, num)
  161. return num;
  162. }
  163. /**
  164. * 传入阳历年月日获得详细的公历、农历object信息 <=>JSON
  165. * @param y solar year
  166. * @param m solar month
  167. * @param d solar day
  168. * @return JSON object
  169. * @eg:console.log(calendar.solar2lunar(1987,11,01));
  170. */
  171. solar2lunar(y : number, m : number, d : number) : LunarInfoType { // 参数区间1900.1.31~2100.12.31
  172. let moonDay = this.solar_date(y, m, d);
  173. let lYear = moonDay.lunarY
  174. let lMonth = moonDay.lunarM
  175. let lDay = moonDay.lunarD
  176. let isLeap = moonDay.isLeap
  177. // 计算农历日期
  178. const IMonthCn = this.toChinaMonth(lMonth, isLeap)
  179. let IDayCn = lDay == 1 ? IMonthCn : this.toChinaDay(lDay)
  180. // 是否今天
  181. let isTodayObj = new Date()
  182. let isToday = false
  183. if (isTodayObj.getFullYear() == y && isTodayObj.getMonth() + 1 == m && isTodayObj.getDate() == d) {
  184. isToday = true
  185. }
  186. let info : LunarInfoType = {
  187. 'lYear': lYear,
  188. 'lMonth': lMonth,
  189. 'lDay': lDay,
  190. 'IMonthCn': IMonthCn,
  191. 'IDayCn': IDayCn,
  192. 'cYear': y,
  193. 'cMonth': m,
  194. 'cDay': d,
  195. 'isToday': isToday,
  196. 'isLeap': isLeap,
  197. }
  198. return info
  199. }
  200. solar_date(y : number, m : number, d : number) : InfoType { // 参数区间1900.1.31~2100.12.31
  201. let date = new Date(y, m - 1, d);
  202. // 参照日期 1901-02-19 正月初一
  203. let offset = (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(1901, 1, 19)) / 86400000;
  204. let temp = 0
  205. let i : number;
  206. for (i = 1901; i < 2101 && offset > 0; i++) {
  207. temp = this.lunarYearDays(i);
  208. offset -= temp;
  209. }
  210. if (offset < 0) {
  211. offset += temp;
  212. i--;
  213. }
  214. // 农历年、月、日
  215. let isLeap : boolean = false
  216. let j : number = 0;
  217. let monthDays = this.lunarMonthDays(i);
  218. let leapM = this.leapMonth(i);
  219. if (offset > 0) {
  220. for (j = 0; j < monthDays.length && offset > 0; j++) {
  221. temp = monthDays[j];
  222. offset -= temp;
  223. }
  224. if (offset == 0) {
  225. j++;
  226. }
  227. if (offset < 0) {
  228. offset += temp;
  229. }
  230. } else {
  231. // 补偿公历1901年2月的农历信息
  232. if (offset == -23) {
  233. let info : InfoType = {
  234. lunarY: i,
  235. lunarM: 12,
  236. lunarD: 8,
  237. isLeap: false
  238. }
  239. info = info
  240. }
  241. }
  242. // 矫正闰年月
  243. if (leapM > 0) {
  244. if (j == leapM + 1) {
  245. isLeap = true
  246. }
  247. if (j >= leapM + 1) {
  248. j--
  249. }
  250. }
  251. const info : InfoType = {
  252. lunarY: i,
  253. lunarM: j,
  254. lunarD: ++offset,
  255. isLeap: isLeap
  256. }
  257. return info
  258. }
  259. }