UNPKG

@iro/calendar

Version:

lunar is a calendar library for Solar and Chinese Lunar.

290 lines 12.8 kB
import { LunarMonth } from './LunarMonth'; import { Lunar } from './Lunar'; import { ShouXingUtil } from './ShouXingUtil'; import { Solar } from './Solar'; import { Dictionary } from './Dictionary'; import { LunarUtil } from './LunarUtil'; import { NineStar } from './NineStar'; var LunarYear = (function () { function LunarYear(lunarYear) { LunarYear._initLeap(); this._year = lunarYear; this._months = []; this._jieQiJulianDays = []; var offset = lunarYear - 4; var yearGanIndex = offset % 10; var yearZhiIndex = offset % 12; if (yearGanIndex < 0) { yearGanIndex += 10; } if (yearZhiIndex < 0) { yearZhiIndex += 12; } this._ganIndex = yearGanIndex; this._zhiIndex = yearZhiIndex; this.compute(); } LunarYear.fromYear = function (lunarYear) { var key = '_' + lunarYear; var obj = LunarYear._CACHE.get(key); if (!obj) { obj = new LunarYear(lunarYear); LunarYear._CACHE.set(key, obj); } return obj; }; LunarYear.prototype.compute = function () { var jq = []; var hs = []; var dayCounts = []; var i, j; var currentYear = this._year; var year = currentYear - 2000; for (i = 0, j = Lunar.JIE_QI_IN_USE.length; i < j; i++) { var t = 36525 * ShouXingUtil.saLonT((year + (17 + i) * 15.0 / 360) * ShouXingUtil.PI_2); t += ShouXingUtil.ONE_THIRD - ShouXingUtil.dtT(t); this._jieQiJulianDays.push(t + Solar.J2000); if (i > 0 && i < 26) { jq[i - 1] = Math.round(t); } } var w = ShouXingUtil.calcShuo(jq[0]); if (w > jq[0]) { w -= 29.5306; } for (i = 0; i < 16; i++) { hs.push(ShouXingUtil.calcShuo(w + 29.5306 * i)); } for (i = 0; i < 15; i++) { dayCounts.push(Math.floor(hs[i + 1] - hs[i])); } var currentYearLeap = LunarYear._LEAP.get('_' + currentYear); if (!currentYearLeap) { currentYearLeap = -1; if (hs[13] <= jq[24]) { i = 1; while (hs[i + 1] > jq[2 * i] && i < 13) { i++; } currentYearLeap = i; } } var prevYear = currentYear - 1; var prevYearLeap = LunarYear._LEAP.get('_' + prevYear); prevYearLeap = prevYearLeap ? prevYearLeap - 12 : -1; var y = this._year - 1; var m = 11; for (i = 0, j = dayCounts.length; i < j; i++) { var cm = m; var isNextLeap = false; if (y == currentYear && i == currentYearLeap) { cm = -cm; } else if (y == prevYear && i == prevYearLeap) { cm = -cm; } if (y == currentYear && i + 1 == currentYearLeap) { isNextLeap = true; } else if (y == prevYear && i + 1 == prevYearLeap) { isNextLeap = true; } this._months.push(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000)); if (!isNextLeap) { m++; } if (m == 13) { m = 1; y++; } } }; LunarYear.prototype.getYear = function () { return this._year; }; LunarYear.prototype.getGanIndex = function () { return this._ganIndex; }; LunarYear.prototype.getZhiIndex = function () { return this._zhiIndex; }; LunarYear.prototype.getGan = function () { return LunarUtil.GAN[this._ganIndex + 1]; }; LunarYear.prototype.getZhi = function () { return LunarUtil.ZHI[this._zhiIndex + 1]; }; LunarYear.prototype.getGanZhi = function () { return this.getGan() + this.getZhi(); }; LunarYear.prototype.getJieQiJulianDays = function () { return this._jieQiJulianDays; }; LunarYear.prototype.getMonths = function () { return this._months; }; LunarYear.prototype.getMonth = function (lunarMonth) { for (var i = 0, j = this._months.length; i < j; i++) { var m = this._months[i]; if (m.getYear() == this._year && m.getMonth() == lunarMonth) { return m; } } return null; }; LunarYear.prototype.getLeapMonth = function () { for (var i = 0, j = this._months.length; i < j; i++) { var m = this._months[i]; if (m.getYear() == this._year && m.isLeap()) { return Math.abs(m.getMonth()); } } return 0; }; LunarYear.prototype.toString = function () { return "".concat(this.getYear()); }; LunarYear.prototype.toFullString = function () { return "".concat(this.getYear(), "\u5E74"); }; LunarYear.prototype._getZaoByGan = function (index, name) { var month = this.getMonth(1); if (null == month) { return ''; } var offset = index - Solar.fromJulianDay(month.getFirstJulianDay()).getLunar().getDayGanIndex(); if (offset < 0) { offset += 10; } return name.replace('几', LunarUtil.NUMBER[offset + 1]); }; LunarYear.prototype._getZaoByZhi = function (index, name) { var month = this.getMonth(1); if (null == month) { return ''; } var offset = index - Solar.fromJulianDay(month.getFirstJulianDay()).getLunar().getDayZhiIndex(); if (offset < 0) { offset += 12; } return name.replace('几', LunarUtil.NUMBER[offset + 1]); }; LunarYear.prototype.getTouLiang = function () { return this._getZaoByZhi(0, '几鼠偷粮'); }; LunarYear.prototype.getCaoZi = function () { return this._getZaoByZhi(0, '草子几分'); }; LunarYear.prototype.getGengTian = function () { return this._getZaoByZhi(1, '几牛耕田'); }; LunarYear.prototype.getHuaShou = function () { return this._getZaoByZhi(3, '花收几分'); }; LunarYear.prototype.getZhiShui = function () { return this._getZaoByZhi(4, '几龙治水'); }; LunarYear.prototype.getTuoGu = function () { return this._getZaoByZhi(6, '几马驮谷'); }; LunarYear.prototype.getQiangMi = function () { return this._getZaoByZhi(9, '几鸡抢米'); }; LunarYear.prototype.getKanCan = function () { return this._getZaoByZhi(9, '几姑看蚕'); }; LunarYear.prototype.getGongZhu = function () { return this._getZaoByZhi(11, '几屠共猪'); }; LunarYear.prototype.getJiaTian = function () { return this._getZaoByGan(0, '甲田几分'); }; LunarYear.prototype.getFenBing = function () { return this._getZaoByGan(2, '几人分饼'); }; LunarYear.prototype.getDeJin = function () { return this._getZaoByGan(7, '几日得金'); }; LunarYear.prototype.getRenBing = function () { return this._getZaoByGan(2, this._getZaoByZhi(2, '几人几丙')); }; LunarYear.prototype.getRenChu = function () { return this._getZaoByGan(3, this._getZaoByZhi(2, '几人几锄')); }; LunarYear.prototype.getYuan = function () { return LunarYear.YUAN[Math.floor((this._year + 2696) / 60) % 3] + '元'; }; LunarYear.prototype.getYun = function () { return LunarYear.YUN[Math.floor((this._year + 2696) / 20) % 9] + '运'; }; LunarYear.prototype.getNineStar = function () { var index = LunarUtil.getJiaZiIndex(this.getGanZhi()) + 1; var yuan = (Math.floor(this._year + 2696) / 60) % 3; var offset = (62 + yuan * 3 - index) % 9; if (0 == offset) { offset = 9; } return NineStar.fromIndex(offset - 1); }; LunarYear.prototype.getPositionXi = function () { return LunarUtil.POSITION_XI[this._ganIndex + 1]; }; LunarYear.prototype.getPositionXiDesc = function () { return LunarUtil.POSITION_DESC.get(this.getPositionXi()); }; LunarYear.prototype.getPositionYangGui = function () { return LunarUtil.POSITION_YANG_GUI[this._ganIndex + 1]; }; LunarYear.prototype.getPositionYangGuiDesc = function () { return LunarUtil.POSITION_DESC.get(this.getPositionYangGui()); }; LunarYear.prototype.getPositionYinGui = function () { return LunarUtil.POSITION_YIN_GUI[this._ganIndex + 1]; }; LunarYear.prototype.getPositionYinGuiDesc = function () { return LunarUtil.POSITION_DESC.get(this.getPositionYinGui()); }; LunarYear.prototype.getPositionFu = function (sect) { if (sect === void 0) { sect = 2; } return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[this._ganIndex + 1]; }; LunarYear.prototype.getPositionFuDesc = function (sect) { if (sect === void 0) { sect = 2; } return LunarUtil.POSITION_DESC.get(this.getPositionFu(sect)); }; LunarYear.prototype.getPositionCai = function () { return LunarUtil.POSITION_CAI[this._ganIndex + 1]; }; LunarYear.prototype.getPositionCaiDesc = function () { return LunarUtil.POSITION_DESC.get(this.getPositionCai()); }; LunarYear.prototype.getPositionTaiSui = function () { return LunarUtil.POSITION_TAI_SUI_YEAR[this._zhiIndex]; }; LunarYear.prototype.getPositionTaiSuiDesc = function () { return LunarUtil.POSITION_DESC.get(this.getPositionTaiSui()); }; LunarYear.prototype.next = function (n) { return LunarYear.fromYear(this._year + n); }; LunarYear.YUAN = ['下', '上', '中']; LunarYear.YUN = ['七', '八', '九', '一', '二', '三', '四', '五', '六']; LunarYear._LEAP_11 = [75, 94, 170, 238, 265, 322, 389, 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]; LunarYear._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]; LunarYear._LEAP = new Dictionary(); LunarYear._CACHE = new Dictionary(); LunarYear._initLeap = function () { if (LunarYear._LEAP.isEmpty()) { var i = void 0, j = void 0; for (i = 0, j = LunarYear._LEAP_11.length; i < j; i++) { LunarYear._LEAP.set('_' + LunarYear._LEAP_11[i], 13); } for (i = 0, j = LunarYear._LEAP_12.length; i < j; i++) { LunarYear._LEAP.set('_' + LunarYear._LEAP_12[i], 14); } } }; return LunarYear; }()); export { LunarYear }; //# sourceMappingURL=LunarYear.js.map