vietnamese-lunar 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/lib/cjs/auspiciousHours.d.ts +7 -0
- package/lib/cjs/auspiciousHours.js +57 -0
- package/lib/cjs/compat.d.ts +8 -0
- package/lib/cjs/compat.js +24 -0
- package/lib/{constants.d.ts → cjs/constants.d.ts} +1 -2
- package/lib/{constants.js → cjs/constants.js} +4 -6
- package/lib/cjs/dayStar.d.ts +14 -0
- package/lib/cjs/dayStar.js +64 -0
- package/lib/{heavenly_earthly.d.ts → cjs/heavenly_earthly.d.ts} +3 -1
- package/lib/cjs/heavenly_earthly.js +57 -0
- package/lib/cjs/holidays.d.ts +10 -0
- package/lib/{holidays.js → cjs/holidays.js} +67 -5
- package/lib/cjs/index.d.ts +9 -0
- package/lib/cjs/index.js +33 -0
- package/lib/cjs/input.d.ts +5 -0
- package/lib/cjs/input.js +39 -0
- package/lib/cjs/lunar.d.ts +9 -0
- package/lib/cjs/lunar.js +115 -0
- package/lib/cjs/lunarConverter.d.ts +7 -0
- package/lib/cjs/lunarConverter.js +102 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/solarTerms.d.ts +5 -0
- package/lib/cjs/solarTerms.js +82 -0
- package/lib/cjs/type.d.ts +135 -0
- package/lib/{utils.d.ts → cjs/utils.d.ts} +6 -0
- package/lib/{utils.js → cjs/utils.js} +18 -2
- package/lib/esm/auspiciousHours.d.ts +7 -0
- package/lib/esm/auspiciousHours.js +50 -0
- package/lib/esm/compat.d.ts +8 -0
- package/lib/esm/compat.js +19 -0
- package/lib/esm/constants.d.ts +14 -0
- package/lib/esm/constants.js +117 -0
- package/lib/esm/dayStar.d.ts +14 -0
- package/lib/esm/dayStar.js +58 -0
- package/lib/esm/heavenly_earthly.d.ts +8 -0
- package/lib/esm/heavenly_earthly.js +48 -0
- package/lib/esm/holidays.d.ts +10 -0
- package/lib/esm/holidays.js +120 -0
- package/lib/esm/index.d.ts +9 -0
- package/lib/esm/index.js +8 -0
- package/lib/esm/input.d.ts +5 -0
- package/lib/esm/input.js +35 -0
- package/lib/esm/lunar.d.ts +9 -0
- package/lib/esm/lunar.js +107 -0
- package/lib/esm/lunarConverter.d.ts +7 -0
- package/lib/esm/lunarConverter.js +95 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/solarTerms.d.ts +5 -0
- package/lib/esm/solarTerms.js +77 -0
- package/lib/esm/type.d.ts +135 -0
- package/lib/esm/type.js +1 -0
- package/lib/esm/utils.d.ts +10 -0
- package/lib/esm/utils.js +51 -0
- package/package.json +22 -8
- package/readme.md +114 -11
- package/lib/auspiciousHours.d.ts +0 -2
- package/lib/auspiciousHours.js +0 -39
- package/lib/dayStar.d.ts +0 -2
- package/lib/dayStar.js +0 -24
- package/lib/heavenly_earthly.js +0 -41
- package/lib/holidays.d.ts +0 -2
- package/lib/index.d.ts +0 -7
- package/lib/index.js +0 -22
- package/lib/lunar.d.ts +0 -3
- package/lib/lunar.js +0 -111
- package/lib/lunarConverter.d.ts +0 -3
- package/lib/lunarConverter.js +0 -67
- package/lib/type.d.ts +0 -45
- /package/lib/{type.js → cjs/type.js} +0 -0
package/lib/cjs/lunar.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDayMaster = exports.getSolarDate = exports.getLunarMonthDays = exports.getLeapMonth = exports.getLunarDate = void 0;
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
const constants_js_1 = require("./constants.js");
|
|
6
|
+
const MIN_YEAR = 1800;
|
|
7
|
+
const MAX_YEAR = 2199;
|
|
8
|
+
const ZERO_DATE = (julianDay) => ({ date: 0, month: 0, year: 0, isLeap: false, julianDay, monthDays: 0 });
|
|
9
|
+
const decodeLunarYear = (year, lunarInfo) => {
|
|
10
|
+
const monthDayLengths = [29, 30];
|
|
11
|
+
const monthDays = new Array(12);
|
|
12
|
+
const offsetOfTet = lunarInfo >> 17;
|
|
13
|
+
const leapMonth = lunarInfo & 0xf;
|
|
14
|
+
const leapMonthLength = monthDayLengths[lunarInfo >> 16 & 0x1];
|
|
15
|
+
let currentJulianDay = (0, utils_js_1.getJulianDayNumber)(1, 1, year) + offsetOfTet;
|
|
16
|
+
let bits = lunarInfo >> 4;
|
|
17
|
+
for (let monthIndex = 0; monthIndex < 12; monthIndex++) {
|
|
18
|
+
monthDays[12 - monthIndex - 1] = monthDayLengths[bits & 0x1];
|
|
19
|
+
bits >>= 1;
|
|
20
|
+
}
|
|
21
|
+
const listYears = [];
|
|
22
|
+
const push = (month, isLeap, length) => {
|
|
23
|
+
listYears.push({ date: 1, month, year, isLeap, julianDay: currentJulianDay, monthDays: length });
|
|
24
|
+
currentJulianDay += length;
|
|
25
|
+
};
|
|
26
|
+
for (let month = 1; month <= 12; month++) {
|
|
27
|
+
push(month, false, monthDays[month - 1]);
|
|
28
|
+
if (month === leapMonth) {
|
|
29
|
+
push(month, true, leapMonthLength);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return listYears;
|
|
33
|
+
};
|
|
34
|
+
// Dữ liệu giải mã của từng năm âm, dùng chung giữa các lần gọi (tối đa 400 năm).
|
|
35
|
+
const yearCache = new Map();
|
|
36
|
+
const getYearInfo = (year) => {
|
|
37
|
+
if (year < MIN_YEAR || year > MAX_YEAR) {
|
|
38
|
+
throw new Error("Year out of supported range (1800–2199)");
|
|
39
|
+
}
|
|
40
|
+
let data = yearCache.get(year);
|
|
41
|
+
if (!data) {
|
|
42
|
+
data = decodeLunarYear(year, constants_js_1.LIST_YEARS[year - MIN_YEAR]);
|
|
43
|
+
yearCache.set(year, data);
|
|
44
|
+
}
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
const findLunarDate = (julianDay, lunarYear) => {
|
|
48
|
+
if (julianDay > constants_js_1.LAST_DAY ||
|
|
49
|
+
julianDay < constants_js_1.FIRST_DAY ||
|
|
50
|
+
lunarYear[0].julianDay > julianDay) {
|
|
51
|
+
return ZERO_DATE(julianDay);
|
|
52
|
+
}
|
|
53
|
+
let index = lunarYear.length - 1;
|
|
54
|
+
while (julianDay < lunarYear[index].julianDay) {
|
|
55
|
+
index--;
|
|
56
|
+
}
|
|
57
|
+
const offset = julianDay - lunarYear[index].julianDay;
|
|
58
|
+
return {
|
|
59
|
+
date: lunarYear[index].date + offset,
|
|
60
|
+
month: lunarYear[index].month,
|
|
61
|
+
year: lunarYear[index].year,
|
|
62
|
+
isLeap: lunarYear[index].isLeap,
|
|
63
|
+
julianDay,
|
|
64
|
+
monthDays: lunarYear[index].monthDays
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
const getLunarDate = (day, month, year) => {
|
|
68
|
+
if (year < MIN_YEAR || year > MAX_YEAR) {
|
|
69
|
+
return ZERO_DATE(0);
|
|
70
|
+
}
|
|
71
|
+
let lunarYearData = getYearInfo(year);
|
|
72
|
+
const julianDay = (0, utils_js_1.getJulianDayNumber)(day, month, year);
|
|
73
|
+
if (julianDay < lunarYearData[0].julianDay && year - 1 >= MIN_YEAR) {
|
|
74
|
+
lunarYearData = getYearInfo(year - 1);
|
|
75
|
+
}
|
|
76
|
+
return findLunarDate(julianDay, lunarYearData);
|
|
77
|
+
};
|
|
78
|
+
exports.getLunarDate = getLunarDate;
|
|
79
|
+
const isLunarYear = (year) => Number.isInteger(year) && year >= MIN_YEAR && year <= MAX_YEAR;
|
|
80
|
+
const findMonth = (lunarMonth, lunarYear, isLeap) => {
|
|
81
|
+
if (!isLunarYear(lunarYear))
|
|
82
|
+
return undefined;
|
|
83
|
+
return getYearInfo(lunarYear).find(m => m.month === lunarMonth && m.isLeap === isLeap);
|
|
84
|
+
};
|
|
85
|
+
/** Tháng nhuận của năm âm (1–12), hoặc 0 nếu năm không nhuận / ngoài phạm vi 1800–2199. */
|
|
86
|
+
const getLeapMonth = (lunarYear) => {
|
|
87
|
+
if (!isLunarYear(lunarYear))
|
|
88
|
+
return 0;
|
|
89
|
+
const leap = getYearInfo(lunarYear).find(m => m.isLeap);
|
|
90
|
+
return leap ? leap.month : 0;
|
|
91
|
+
};
|
|
92
|
+
exports.getLeapMonth = getLeapMonth;
|
|
93
|
+
/** Số ngày của tháng âm (29 hoặc 30); `null` nếu tháng/năm không tồn tại (kể cả tháng nhuận không có trong năm đó). */
|
|
94
|
+
const getLunarMonthDays = (lunarMonth, lunarYear, isLeap = false) => {
|
|
95
|
+
const found = findMonth(lunarMonth, lunarYear, isLeap);
|
|
96
|
+
return found ? found.monthDays : null;
|
|
97
|
+
};
|
|
98
|
+
exports.getLunarMonthDays = getLunarMonthDays;
|
|
99
|
+
/** Âm → dương. Trả `null` (không throw) nếu ngày âm không tồn tại hoặc kết quả nằm ngoài dữ liệu (25/1/1800 – 31/12/2199). */
|
|
100
|
+
const getSolarDate = (lunarDay, lunarMonth, lunarYear, isLeap = false) => {
|
|
101
|
+
if (!Number.isInteger(lunarDay))
|
|
102
|
+
return null;
|
|
103
|
+
const found = findMonth(lunarMonth, lunarYear, isLeap === true);
|
|
104
|
+
if (!found || lunarDay < 1 || lunarDay > found.monthDays)
|
|
105
|
+
return null;
|
|
106
|
+
const jd = found.julianDay + lunarDay - 1;
|
|
107
|
+
if (jd < constants_js_1.FIRST_DAY || jd > constants_js_1.LAST_DAY)
|
|
108
|
+
return null;
|
|
109
|
+
return { ...(0, utils_js_1.jdToSolarDate)(jd), jd };
|
|
110
|
+
};
|
|
111
|
+
exports.getSolarDate = getSolarDate;
|
|
112
|
+
const getDayMaster = (date) => {
|
|
113
|
+
return constants_js_1.HEAVENLY_EARTHLY_ELEMENT_MAP[date] || 'Không rõ';
|
|
114
|
+
};
|
|
115
|
+
exports.getDayMaster = getDayMaster;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DateInput, DayInfo, DayInfoOptions, HourInfo, LunarDayInfo } from "./type.js";
|
|
2
|
+
export declare const dayInfo: (date: DateInput, options?: DayInfoOptions) => DayInfo | string;
|
|
3
|
+
export declare const lunarDay: (date: DateInput) => LunarDayInfo | string;
|
|
4
|
+
/** Thông tin một giờ (0..23) của ngày; `null` nếu ngày hoặc giờ không hợp lệ. */
|
|
5
|
+
export declare const getHourInfo: (date: DateInput, hour: number, options?: DayInfoOptions) => HourInfo | null;
|
|
6
|
+
/** `dayInfo` cho mọi ngày của tháng dương (28–31 phần tử); `null` nếu tháng/năm không hợp lệ hoặc ngoài 1800–2199. */
|
|
7
|
+
export declare const monthInfo: (year: number, month: number, options?: DayInfoOptions) => DayInfo[] | null;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.monthInfo = exports.getHourInfo = exports.lunarDay = exports.dayInfo = void 0;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const holidays_js_1 = require("./holidays.js");
|
|
6
|
+
const heavenly_earthly_js_1 = require("./heavenly_earthly.js");
|
|
7
|
+
const lunar_js_1 = require("./lunar.js");
|
|
8
|
+
const auspiciousHours_js_1 = require("./auspiciousHours.js");
|
|
9
|
+
const dayStar_js_1 = require("./dayStar.js");
|
|
10
|
+
const compat_js_1 = require("./compat.js");
|
|
11
|
+
const solarTerms_js_1 = require("./solarTerms.js");
|
|
12
|
+
const utils_js_1 = require("./utils.js");
|
|
13
|
+
const input_js_1 = require("./input.js");
|
|
14
|
+
const INVALID = 'Invalid date input.';
|
|
15
|
+
const getDayName = (lunarDate) => {
|
|
16
|
+
if (lunarDate.date === 0) {
|
|
17
|
+
return { date: '', month: '', year: '' };
|
|
18
|
+
}
|
|
19
|
+
return (0, heavenly_earthly_js_1.getHeavenlyEarthly)(lunarDate);
|
|
20
|
+
};
|
|
21
|
+
const ELEMENTS = ['Kim', 'Mộc', 'Thủy', 'Hỏa', 'Thổ'];
|
|
22
|
+
// Nạp âm có dạng "<tên> <hành>", ví dụ "Thành Đầu Thổ".
|
|
23
|
+
const getElement = (napAm) => {
|
|
24
|
+
const last = napAm.split(' ').pop();
|
|
25
|
+
return ELEMENTS.indexOf(last) !== -1 ? last : null;
|
|
26
|
+
};
|
|
27
|
+
const buildDayInfo = (day, month, year, options) => {
|
|
28
|
+
const system = (options === null || options === void 0 ? void 0 : options.starSystem) === 'ngoc-hap' ? 'ngoc-hap' : 'hiep-ky';
|
|
29
|
+
const lunar = (0, lunar_js_1.getLunarDate)(day, month, year);
|
|
30
|
+
const lunarText = getDayName(lunar);
|
|
31
|
+
const jd = lunar.julianDay;
|
|
32
|
+
const next = jd + 1;
|
|
33
|
+
const nextSolar = (0, lunar_js_1.getLunarDate)(...(nextSolarParts(day, month, year)));
|
|
34
|
+
const canChi = (0, heavenly_earthly_js_1.getCanChi)(lunar);
|
|
35
|
+
const napAm = (0, lunar_js_1.getDayMaster)(lunarText.date);
|
|
36
|
+
return {
|
|
37
|
+
day: constants_js_1.DAY_OF_WEEK[(jd + 1) % 7],
|
|
38
|
+
solarDate: `${day}/${month}/${year}`,
|
|
39
|
+
lunarDate: { date: lunar.date, month: lunar.month, isLeap: lunar.isLeap, monthDays: lunar.monthDays },
|
|
40
|
+
lunarDateText: lunarText,
|
|
41
|
+
firstHour: (0, heavenly_earthly_js_1.getFirstHeavenlyHour)(jd) + ' ' + constants_js_1.EARTHLY_BRANCHES[0],
|
|
42
|
+
solarTerms: constants_js_1.SOLAR_TERMS[(0, utils_js_1.getSolarTermIndex)(jd)],
|
|
43
|
+
auspiciousHours: (0, auspiciousHours_js_1.getAuspiciousHours)(jd),
|
|
44
|
+
inAuspiciousHours: (0, auspiciousHours_js_1.getInauspiciousHours)(jd),
|
|
45
|
+
dayMaster: napAm,
|
|
46
|
+
napAm,
|
|
47
|
+
element: getElement(napAm),
|
|
48
|
+
dayStar: (0, dayStar_js_1.getDayStarInfo)(lunar, system),
|
|
49
|
+
holidays: (0, holidays_js_1.getVietnameseHolidays)(day, month, year, lunar, nextSolar),
|
|
50
|
+
hours: (0, auspiciousHours_js_1.getHours)(jd, system),
|
|
51
|
+
solarTerm: (0, solarTerms_js_1.getSolarTermInfo)(jd),
|
|
52
|
+
canChi,
|
|
53
|
+
clash: (0, compat_js_1.getClash)(canChi.day.can, canChi.day.chi),
|
|
54
|
+
compatible: (0, compat_js_1.getCompatible)(canChi.day.chi),
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const nextSolarParts = (day, month, year) => {
|
|
58
|
+
const d = new Date(Date.UTC(year, month - 1, day + 1));
|
|
59
|
+
return [d.getUTCDate(), d.getUTCMonth() + 1, d.getUTCFullYear()];
|
|
60
|
+
};
|
|
61
|
+
const dayInfo = (date, options) => {
|
|
62
|
+
const parsed = (0, input_js_1.parseDateInput)(date);
|
|
63
|
+
if (!parsed)
|
|
64
|
+
return INVALID;
|
|
65
|
+
return buildDayInfo(parsed.day, parsed.month, parsed.year, options);
|
|
66
|
+
};
|
|
67
|
+
exports.dayInfo = dayInfo;
|
|
68
|
+
const lunarDay = (date) => {
|
|
69
|
+
const parsed = (0, input_js_1.parseDateInput)(date);
|
|
70
|
+
if (!parsed)
|
|
71
|
+
return INVALID;
|
|
72
|
+
const lunar = (0, lunar_js_1.getLunarDate)(parsed.day, parsed.month, parsed.year);
|
|
73
|
+
return {
|
|
74
|
+
date: lunar.date,
|
|
75
|
+
date_text: getDayName(lunar),
|
|
76
|
+
month: lunar.month,
|
|
77
|
+
isLeap: lunar.isLeap,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
exports.lunarDay = lunarDay;
|
|
81
|
+
/** Thông tin một giờ (0..23) của ngày; `null` nếu ngày hoặc giờ không hợp lệ. */
|
|
82
|
+
const getHourInfo = (date, hour, options) => {
|
|
83
|
+
const parsed = (0, input_js_1.parseDateInput)(date);
|
|
84
|
+
if (!parsed || !Number.isInteger(hour) || hour < 0 || hour > 23)
|
|
85
|
+
return null;
|
|
86
|
+
const system = (options === null || options === void 0 ? void 0 : options.starSystem) === 'ngoc-hap' ? 'ngoc-hap' : 'hiep-ky';
|
|
87
|
+
const jd = (0, utils_js_1.getJulianDayNumber)(parsed.day, parsed.month, parsed.year);
|
|
88
|
+
return (0, auspiciousHours_js_1.getHours)(jd, system)[(0, auspiciousHours_js_1.hourToBranchIndex)(hour)];
|
|
89
|
+
};
|
|
90
|
+
exports.getHourInfo = getHourInfo;
|
|
91
|
+
/** `dayInfo` cho mọi ngày của tháng dương (28–31 phần tử); `null` nếu tháng/năm không hợp lệ hoặc ngoài 1800–2199. */
|
|
92
|
+
const monthInfo = (year, month, options) => {
|
|
93
|
+
if (!Number.isInteger(month) || month < 1 || month > 12 || !(0, input_js_1.parseDateInput)({ day: 1, month, year }))
|
|
94
|
+
return null;
|
|
95
|
+
const days = new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
96
|
+
const result = [];
|
|
97
|
+
for (let day = 1; day <= days; day++) {
|
|
98
|
+
result.push(buildDayInfo(day, month, year, options));
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
102
|
+
exports.monthInfo = monthInfo;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SolarTermInfo, SolarTermItem } from "./type.js";
|
|
2
|
+
/** Tiết khí hiện tại và kế tiếp của ngày có JDN `julianDay` (múi giờ UTC+7). */
|
|
3
|
+
export declare const getSolarTermInfo: (julianDay: number) => SolarTermInfo;
|
|
4
|
+
/** 24 tiết khí của năm dương, sắp theo thời gian (Tiểu Hàn → Đông Chí). Sai số cỡ vài chục phút do công thức vị trí Mặt Trời rút gọn. */
|
|
5
|
+
export declare const getSolarTerms: (year: number) => SolarTermItem[];
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSolarTerms = exports.getSolarTermInfo = void 0;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const TWO_PI = Math.PI * 2;
|
|
7
|
+
const UNIX_EPOCH_JD = 2440587.5;
|
|
8
|
+
const MS_PER_DAY = 86400000;
|
|
9
|
+
const TZ_VN = 7; // UTC+7
|
|
10
|
+
const TROPICAL_YEAR = 365.2422;
|
|
11
|
+
const TERM_LONGITUDE_STEP = 15; // mỗi tiết khí cách nhau 15° kinh độ Mặt Trời
|
|
12
|
+
const jdToDate = (jd) => new Date(Math.round((jd - UNIX_EPOCH_JD) * MS_PER_DAY / 60000) * 60000);
|
|
13
|
+
// Độ lệch kinh độ Mặt Trời so với mục tiêu, chuẩn hóa về (-π, π]; tăng dần và đổi dấu từ âm sang dương tại thời điểm tiết khí.
|
|
14
|
+
const longitudeOffset = (jdUt, targetRad) => {
|
|
15
|
+
let diff = (0, utils_js_1.calculateSunLongitude)(jdUt) - targetRad;
|
|
16
|
+
diff -= TWO_PI * Math.round(diff / TWO_PI);
|
|
17
|
+
return diff;
|
|
18
|
+
};
|
|
19
|
+
// Thời điểm (JD, UT) Mặt Trời đạt kinh độ của tiết khí `index`, trong cửa sổ [lo, hi] với f(lo) < 0 ≤ f(hi).
|
|
20
|
+
const bisectTerm = (index, lo, hi) => {
|
|
21
|
+
const target = (index * TERM_LONGITUDE_STEP) * Math.PI / 180;
|
|
22
|
+
for (let i = 0; i < 40; i++) {
|
|
23
|
+
const mid = (lo + hi) / 2;
|
|
24
|
+
if (longitudeOffset(mid, target) < 0)
|
|
25
|
+
lo = mid;
|
|
26
|
+
else
|
|
27
|
+
hi = mid;
|
|
28
|
+
}
|
|
29
|
+
return (lo + hi) / 2;
|
|
30
|
+
};
|
|
31
|
+
// JD (UT) của thời điểm kết thúc ngày địa phương (UTC+7) có JDN `julianDay`; cùng mốc với getSolarTermIndex.
|
|
32
|
+
const endOfLocalDay = (julianDay) => julianDay + 0.5 - TZ_VN / 24;
|
|
33
|
+
// JDN của ngày địa phương (UTC+7) chứa thời điểm `jdUt`.
|
|
34
|
+
const localDayOf = (jdUt) => Math.floor(jdUt + 0.5 + TZ_VN / 24);
|
|
35
|
+
const dayCache = new Map();
|
|
36
|
+
const getTermRecord = (julianDay) => {
|
|
37
|
+
let rec = dayCache.get(julianDay);
|
|
38
|
+
if (!rec) {
|
|
39
|
+
const end = endOfLocalDay(julianDay);
|
|
40
|
+
const currentIndex = (0, utils_js_1.getSolarTermIndex)(julianDay);
|
|
41
|
+
const nextIndex = (currentIndex + 1) % 24;
|
|
42
|
+
rec = {
|
|
43
|
+
currentIndex,
|
|
44
|
+
currentJd: bisectTerm(currentIndex, end - 17, end),
|
|
45
|
+
nextIndex,
|
|
46
|
+
nextJd: bisectTerm(nextIndex, end, end + 17),
|
|
47
|
+
};
|
|
48
|
+
dayCache.set(julianDay, rec);
|
|
49
|
+
}
|
|
50
|
+
return rec;
|
|
51
|
+
};
|
|
52
|
+
const item = (index, jd) => ({ name: constants_js_1.SOLAR_TERMS[index], index, start: jdToDate(jd) });
|
|
53
|
+
/** Tiết khí hiện tại và kế tiếp của ngày có JDN `julianDay` (múi giờ UTC+7). */
|
|
54
|
+
const getSolarTermInfo = (julianDay) => {
|
|
55
|
+
const rec = getTermRecord(julianDay);
|
|
56
|
+
return {
|
|
57
|
+
current: item(rec.currentIndex, rec.currentJd),
|
|
58
|
+
next: { ...item(rec.nextIndex, rec.nextJd), daysUntil: localDayOf(rec.nextJd) - julianDay },
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
exports.getSolarTermInfo = getSolarTermInfo;
|
|
62
|
+
const yearCache = new Map();
|
|
63
|
+
/** 24 tiết khí của năm dương, sắp theo thời gian (Tiểu Hàn → Đông Chí). Sai số cỡ vài chục phút do công thức vị trí Mặt Trời rút gọn. */
|
|
64
|
+
const getSolarTerms = (year) => {
|
|
65
|
+
if (!Number.isInteger(year) || year < 1800 || year > 2199)
|
|
66
|
+
return [];
|
|
67
|
+
let cached = yearCache.get(year);
|
|
68
|
+
if (!cached) {
|
|
69
|
+
const xuanPhan = (0, utils_js_1.getJulianDayNumber)(20, 3, year);
|
|
70
|
+
cached = [];
|
|
71
|
+
for (let index = 0; index < 24; index++) {
|
|
72
|
+
let approx = xuanPhan + index * TROPICAL_YEAR / 24;
|
|
73
|
+
if (index >= 19)
|
|
74
|
+
approx -= TROPICAL_YEAR; // Tiểu Hàn, Đại Hàn, Lập Xuân, Vũ Thủy, Kinh Trập rơi vào đầu năm
|
|
75
|
+
cached.push(item(index, bisectTerm(index, approx - 10, approx + 10)));
|
|
76
|
+
}
|
|
77
|
+
cached.sort((a, b) => a.start.getTime() - b.start.getTime());
|
|
78
|
+
yearCache.set(year, cached);
|
|
79
|
+
}
|
|
80
|
+
return cached.map(t => ({ ...t, start: new Date(t.start.getTime()) }));
|
|
81
|
+
};
|
|
82
|
+
exports.getSolarTerms = getSolarTerms;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export type LunarDate = {
|
|
2
|
+
date: number;
|
|
3
|
+
month: number;
|
|
4
|
+
year: number;
|
|
5
|
+
isLeap: boolean;
|
|
6
|
+
julianDay: number;
|
|
7
|
+
/** Số ngày của tháng âm: 29 (thiếu) hoặc 30 (đủ). `0` chỉ khi `date === 0` (ngoài phạm vi dữ liệu). */
|
|
8
|
+
monthDays: 29 | 30 | 0;
|
|
9
|
+
};
|
|
10
|
+
export type dateFormat = {
|
|
11
|
+
date: string | number;
|
|
12
|
+
month: string | number;
|
|
13
|
+
year: string | number;
|
|
14
|
+
};
|
|
15
|
+
/** Hệ thống phân loại Hoàng Đạo / Hắc Đạo. */
|
|
16
|
+
export type StarSystem = 'hiep-ky' | 'ngoc-hap';
|
|
17
|
+
export type StarCategory = 'hoang-dao' | 'hac-dao' | null;
|
|
18
|
+
export type DayStarInfo = {
|
|
19
|
+
star: string;
|
|
20
|
+
/** Giữ cho tương thích 2.x. `false` KHÔNG đồng nghĩa hắc đạo — xem `category`. */
|
|
21
|
+
isAuspicious: boolean;
|
|
22
|
+
/** 1..12, thứ tự cố định bắt đầu từ Thanh Long. 0 nếu không xác định. */
|
|
23
|
+
index: number;
|
|
24
|
+
category: StarCategory;
|
|
25
|
+
system: StarSystem;
|
|
26
|
+
aliases: string[];
|
|
27
|
+
};
|
|
28
|
+
export type HolidayType = 'public' | 'traditional' | 'commemorative';
|
|
29
|
+
export type Holiday = {
|
|
30
|
+
name: string;
|
|
31
|
+
calendar: 'solar' | 'lunar';
|
|
32
|
+
isPublicHoliday: boolean;
|
|
33
|
+
type: HolidayType;
|
|
34
|
+
isDayOff: boolean;
|
|
35
|
+
};
|
|
36
|
+
export type SolarDateObject = {
|
|
37
|
+
day: number;
|
|
38
|
+
month: number;
|
|
39
|
+
year: number;
|
|
40
|
+
};
|
|
41
|
+
/** Ngày dương: chuỗi 'd/m/yyyy', đối tượng `Date` (giờ địa phương) hoặc `{day, month, year}`. */
|
|
42
|
+
export type DateInput = string | Date | SolarDateObject;
|
|
43
|
+
export type SolarDateResult = {
|
|
44
|
+
day: number;
|
|
45
|
+
month: number;
|
|
46
|
+
year: number;
|
|
47
|
+
jd: number;
|
|
48
|
+
};
|
|
49
|
+
export type HolidayEvent = {
|
|
50
|
+
name: string;
|
|
51
|
+
date: SolarDateObject;
|
|
52
|
+
calendar: 'solar' | 'lunar';
|
|
53
|
+
lunarDate?: {
|
|
54
|
+
day: number;
|
|
55
|
+
month: number;
|
|
56
|
+
};
|
|
57
|
+
type: HolidayType;
|
|
58
|
+
isDayOff: boolean;
|
|
59
|
+
isPublicHoliday: boolean;
|
|
60
|
+
};
|
|
61
|
+
export type HourInfo = {
|
|
62
|
+
chi: string;
|
|
63
|
+
canChi: string;
|
|
64
|
+
/** Giờ bắt đầu (23 với giờ Tý). */
|
|
65
|
+
start: number;
|
|
66
|
+
end: number;
|
|
67
|
+
/** Thần trực giờ (12 thần). */
|
|
68
|
+
star: string;
|
|
69
|
+
category: StarCategory;
|
|
70
|
+
};
|
|
71
|
+
export type SolarTermItem = {
|
|
72
|
+
name: string;
|
|
73
|
+
index: number;
|
|
74
|
+
start: Date;
|
|
75
|
+
};
|
|
76
|
+
export type SolarTermInfo = {
|
|
77
|
+
current: SolarTermItem;
|
|
78
|
+
next: SolarTermItem & {
|
|
79
|
+
daysUntil: number;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
export type CanChiPart = {
|
|
83
|
+
can: number;
|
|
84
|
+
chi: number;
|
|
85
|
+
text: string;
|
|
86
|
+
};
|
|
87
|
+
export type CanChiInfo = {
|
|
88
|
+
day: CanChiPart;
|
|
89
|
+
month: CanChiPart;
|
|
90
|
+
year: CanChiPart;
|
|
91
|
+
hour: CanChiPart;
|
|
92
|
+
};
|
|
93
|
+
export type Element = 'Kim' | 'Mộc' | 'Thủy' | 'Hỏa' | 'Thổ';
|
|
94
|
+
export type DayInfoOptions = {
|
|
95
|
+
starSystem?: StarSystem;
|
|
96
|
+
};
|
|
97
|
+
export type DayInfo = {
|
|
98
|
+
day: string;
|
|
99
|
+
solarDate: string;
|
|
100
|
+
lunarDate: {
|
|
101
|
+
date: number;
|
|
102
|
+
month: number;
|
|
103
|
+
isLeap: boolean;
|
|
104
|
+
monthDays: 29 | 30 | 0;
|
|
105
|
+
};
|
|
106
|
+
lunarDateText: dateFormat;
|
|
107
|
+
firstHour: string;
|
|
108
|
+
solarTerms: string;
|
|
109
|
+
auspiciousHours: string;
|
|
110
|
+
inAuspiciousHours: string;
|
|
111
|
+
/** @deprecated Đây là Nạp âm, không phải Nhật chủ. Dùng `napAm`. */
|
|
112
|
+
dayMaster: string;
|
|
113
|
+
napAm: string;
|
|
114
|
+
/** Hành của Nạp âm; `null` nếu không xác định. */
|
|
115
|
+
element: Element | null;
|
|
116
|
+
dayStar: DayStarInfo;
|
|
117
|
+
holidays: Holiday[];
|
|
118
|
+
hours: HourInfo[];
|
|
119
|
+
solarTerm: SolarTermInfo;
|
|
120
|
+
canChi: CanChiInfo;
|
|
121
|
+
clash: {
|
|
122
|
+
canChi: string[];
|
|
123
|
+
chi: string[];
|
|
124
|
+
};
|
|
125
|
+
compatible: {
|
|
126
|
+
tamHop: string[];
|
|
127
|
+
lucHop: string;
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
export type LunarDayInfo = {
|
|
131
|
+
date: number;
|
|
132
|
+
date_text: dateFormat;
|
|
133
|
+
month: number;
|
|
134
|
+
isLeap: boolean;
|
|
135
|
+
};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export declare const INT: (number: number) => number;
|
|
2
2
|
export declare const getJulianDayNumber: (day: number, month: number, year: number) => number;
|
|
3
|
+
export declare const calculateSunLongitude: (julianDay: number) => number;
|
|
3
4
|
export declare const calculateSunLongitudeInTimeZone: (julianDay: number, timeZoneOffset: number) => number;
|
|
4
5
|
export declare const getSolarTermIndex: (julianDay: number) => number;
|
|
6
|
+
export declare const jdToSolarDate: (jd: number) => {
|
|
7
|
+
day: number;
|
|
8
|
+
month: number;
|
|
9
|
+
year: number;
|
|
10
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSolarTermIndex = exports.calculateSunLongitudeInTimeZone = exports.getJulianDayNumber = exports.INT = void 0;
|
|
3
|
+
exports.jdToSolarDate = exports.getSolarTermIndex = exports.calculateSunLongitudeInTimeZone = exports.calculateSunLongitude = exports.getJulianDayNumber = exports.INT = void 0;
|
|
4
4
|
const INT = (number) => {
|
|
5
5
|
return Math.floor(number);
|
|
6
6
|
};
|
|
@@ -31,9 +31,10 @@ const calculateSunLongitude = (julianDay) => {
|
|
|
31
31
|
trueLongitude -= Math.PI * 2 * (0, exports.INT)(trueLongitude / (Math.PI * 2));
|
|
32
32
|
return trueLongitude;
|
|
33
33
|
};
|
|
34
|
+
exports.calculateSunLongitude = calculateSunLongitude;
|
|
34
35
|
const calculateSunLongitudeInTimeZone = (julianDay, timeZoneOffset) => {
|
|
35
36
|
const adjustedDay = julianDay - 0.5 - timeZoneOffset / 24.0;
|
|
36
|
-
const sunLongitude = calculateSunLongitude(adjustedDay);
|
|
37
|
+
const sunLongitude = (0, exports.calculateSunLongitude)(adjustedDay);
|
|
37
38
|
return (0, exports.INT)(sunLongitude / Math.PI * 12);
|
|
38
39
|
};
|
|
39
40
|
exports.calculateSunLongitudeInTimeZone = calculateSunLongitudeInTimeZone;
|
|
@@ -42,3 +43,18 @@ const getSolarTermIndex = (julianDay) => {
|
|
|
42
43
|
return (0, exports.calculateSunLongitudeInTimeZone)(julianDay + 1, 7);
|
|
43
44
|
};
|
|
44
45
|
exports.getSolarTermIndex = getSolarTermIndex;
|
|
46
|
+
// Ngược lại của getJulianDayNumber: JDN -> ngày dương (lịch Gregory).
|
|
47
|
+
const jdToSolarDate = (jd) => {
|
|
48
|
+
const a = jd + 32044;
|
|
49
|
+
const b = (0, exports.INT)((4 * a + 3) / 146097);
|
|
50
|
+
const c = a - (0, exports.INT)(146097 * b / 4);
|
|
51
|
+
const d = (0, exports.INT)((4 * c + 3) / 1461);
|
|
52
|
+
const e = c - (0, exports.INT)(1461 * d / 4);
|
|
53
|
+
const m = (0, exports.INT)((5 * e + 2) / 153);
|
|
54
|
+
return {
|
|
55
|
+
day: e - (0, exports.INT)((153 * m + 2) / 5) + 1,
|
|
56
|
+
month: m + 3 - 12 * (0, exports.INT)(m / 10),
|
|
57
|
+
year: 100 * b + d - 4800 + (0, exports.INT)(m / 10),
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
exports.jdToSolarDate = jdToSolarDate;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HourInfo, StarSystem } from "./type.js";
|
|
2
|
+
export declare const getAuspiciousHours: (julianDay: number) => string;
|
|
3
|
+
export declare const getInauspiciousHours: (julianDay: number) => string;
|
|
4
|
+
/** 12 giờ của ngày (Tý → Hợi) kèm thần trực giờ và phân loại theo `system`. */
|
|
5
|
+
export declare const getHours: (julianDay: number, system?: StarSystem) => HourInfo[];
|
|
6
|
+
/** Giờ dương lịch 0..23 → chỉ số giờ Chi (23 và 0 đều là giờ Tý). */
|
|
7
|
+
export declare const hourToBranchIndex: (hour: number) => number;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AUSPICIOUS_HOURS, EARTHLY_BRANCHES, HEAVENLY_STEMS, } from "./constants.js";
|
|
2
|
+
import { getDayBranchIndex } from "./heavenly_earthly.js";
|
|
3
|
+
import { getHourStarPosition, getStarCategory, STAR_DEFINITIONS } from "./dayStar.js";
|
|
4
|
+
const getHourStemIndex = (julianDay, hour) => {
|
|
5
|
+
const firstHourStemIndex = (julianDay - 1) * 2 % 10;
|
|
6
|
+
return (firstHourStemIndex + hour) % 10;
|
|
7
|
+
};
|
|
8
|
+
const hourText = (julianDay, hour) => {
|
|
9
|
+
const start = (hour * 2 + 23) % 24;
|
|
10
|
+
const end = (hour * 2 + 1) % 24;
|
|
11
|
+
return `${HEAVENLY_STEMS[getHourStemIndex(julianDay, hour)]} ${EARTHLY_BRANCHES[hour]} (${start}-${end})`;
|
|
12
|
+
};
|
|
13
|
+
export const getAuspiciousHours = (julianDay) => {
|
|
14
|
+
const pattern = AUSPICIOUS_HOURS[getDayBranchIndex(julianDay) % 6];
|
|
15
|
+
const list = [];
|
|
16
|
+
for (let hour = 0; hour < 12; hour++) {
|
|
17
|
+
if (pattern.charAt(hour) === '1')
|
|
18
|
+
list.push(hourText(julianDay, hour));
|
|
19
|
+
}
|
|
20
|
+
return list.join(', ');
|
|
21
|
+
};
|
|
22
|
+
export const getInauspiciousHours = (julianDay) => {
|
|
23
|
+
const pattern = AUSPICIOUS_HOURS[getDayBranchIndex(julianDay) % 6];
|
|
24
|
+
if (!pattern || pattern.length < 12)
|
|
25
|
+
return '';
|
|
26
|
+
const list = [];
|
|
27
|
+
for (let hour = 0; hour < 12; hour++) {
|
|
28
|
+
if (pattern.charAt(hour) !== '1')
|
|
29
|
+
list.push(hourText(julianDay, hour));
|
|
30
|
+
}
|
|
31
|
+
return list.join(', ');
|
|
32
|
+
};
|
|
33
|
+
/** 12 giờ của ngày (Tý → Hợi) kèm thần trực giờ và phân loại theo `system`. */
|
|
34
|
+
export const getHours = (julianDay, system = 'hiep-ky') => {
|
|
35
|
+
const hours = [];
|
|
36
|
+
for (let hour = 0; hour < 12; hour++) {
|
|
37
|
+
const position = getHourStarPosition(julianDay, hour);
|
|
38
|
+
hours.push({
|
|
39
|
+
chi: EARTHLY_BRANCHES[hour],
|
|
40
|
+
canChi: `${HEAVENLY_STEMS[getHourStemIndex(julianDay, hour)]} ${EARTHLY_BRANCHES[hour]}`,
|
|
41
|
+
start: (hour * 2 + 23) % 24,
|
|
42
|
+
end: (hour * 2 + 1) % 24,
|
|
43
|
+
star: STAR_DEFINITIONS[position].name,
|
|
44
|
+
category: getStarCategory(position, system),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return hours;
|
|
48
|
+
};
|
|
49
|
+
/** Giờ dương lịch 0..23 → chỉ số giờ Chi (23 và 0 đều là giờ Tý). */
|
|
50
|
+
export const hourToBranchIndex = (hour) => Math.floor(((hour + 1) % 24) / 2);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EARTHLY_BRANCHES, HEAVENLY_STEMS } from "./constants.js";
|
|
2
|
+
// Tam hợp: Thân-Tý-Thìn, Hợi-Mão-Mùi, Dần-Ngọ-Tuất, Tỵ-Dậu-Sửu (Chi cách nhau 4).
|
|
3
|
+
// Lục hợp: Tý-Sửu, Dần-Hợi, Mão-Tuất, Thìn-Dậu, Tỵ-Thân, Ngọ-Mùi (tổng chỉ số ≡ 1 mod 12).
|
|
4
|
+
// Lục xung: Chi đối nhau (cách 6).
|
|
5
|
+
// Thiên khắc địa xung: Chi đối nhau + Can tương xung (Giáp-Canh, Ất-Tân, Bính-Nhâm, Đinh-Quý).
|
|
6
|
+
// Mậu và Kỷ không có Can xung, nên với ngày Mậu/Kỷ `canChi` rỗng (vẫn có `chi`).
|
|
7
|
+
const getClashCan = (can) => (can <= 3 ? can + 6 : can >= 6 ? can - 6 : -1);
|
|
8
|
+
export const getClash = (dayCan, dayChi) => {
|
|
9
|
+
const oppositeChi = (dayChi + 6) % 12;
|
|
10
|
+
const clashCan = getClashCan(dayCan);
|
|
11
|
+
return {
|
|
12
|
+
canChi: clashCan === -1 ? [] : [`${HEAVENLY_STEMS[clashCan]} ${EARTHLY_BRANCHES[oppositeChi]}`],
|
|
13
|
+
chi: [EARTHLY_BRANCHES[oppositeChi]],
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export const getCompatible = (dayChi) => ({
|
|
17
|
+
tamHop: [EARTHLY_BRANCHES[(dayChi + 4) % 12], EARTHLY_BRANCHES[(dayChi + 8) % 12]],
|
|
18
|
+
lucHop: EARTHLY_BRANCHES[(13 - dayChi) % 12],
|
|
19
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare const LIST_YEARS: number[];
|
|
2
|
+
declare const HEAVENLY_STEMS: string[];
|
|
3
|
+
declare const EARTHLY_BRANCHES: string[];
|
|
4
|
+
declare const AUSPICIOUS_HOURS: string[];
|
|
5
|
+
declare const SOLAR_TERMS: string[];
|
|
6
|
+
declare const HEAVENLY_EARTHLY_ELEMENT_MAP: Record<string, string>;
|
|
7
|
+
declare const DAY_STARS: string[];
|
|
8
|
+
declare const AUSPICIOUS_DAY_STARS: string[];
|
|
9
|
+
declare const LEAP_TEXT = " (nhu\u1EADn)";
|
|
10
|
+
declare const DAY_OF_WEEK: string[];
|
|
11
|
+
declare const FIRST_DAY: number;
|
|
12
|
+
declare const LAST_DAY: number;
|
|
13
|
+
declare const PI: number;
|
|
14
|
+
export { LIST_YEARS, FIRST_DAY, LAST_DAY, PI, HEAVENLY_STEMS, EARTHLY_BRANCHES, AUSPICIOUS_HOURS, SOLAR_TERMS, DAY_OF_WEEK, LEAP_TEXT, HEAVENLY_EARTHLY_ELEMENT_MAP, DAY_STARS, AUSPICIOUS_DAY_STARS };
|