@ysgroup/core 0.1.32 → 0.1.33
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/package.json +1 -1
- package/src/calendar.ts +99 -0
- package/src/index.ts +9 -0
- package/src/lunar.ts +420 -0
package/package.json
CHANGED
package/src/calendar.ts
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* 交易控制一律在服务端判断。
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import { ysLunar, type YsLunarDate } from "./lunar";
|
|
12
|
+
|
|
11
13
|
/** workday 工作日、weekend 周末、holiday 放假、adjusted 调休上班。 */
|
|
12
14
|
export type YsDayKind = "workday" | "weekend" | "holiday" | "adjusted";
|
|
13
15
|
|
|
@@ -302,3 +304,100 @@ export function ysMonthSummary(
|
|
|
302
304
|
adjusted: count((item) => item.kind === "adjusted"),
|
|
303
305
|
};
|
|
304
306
|
}
|
|
307
|
+
|
|
308
|
+
/** 某天在本月里的位置:第几个、共几个。 */
|
|
309
|
+
export interface YsDayOrdinal {
|
|
310
|
+
index: number;
|
|
311
|
+
total: number;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** 选中日详情需要的汇总:本月第几个工作日、交易日,交易日标记,所在假期,农历。 */
|
|
315
|
+
export interface YsDayDetail {
|
|
316
|
+
date: string;
|
|
317
|
+
info?: YsCalendarDay;
|
|
318
|
+
/** 本月第几个工作日(含法定补班);不是工作日或月份信息不全时没有。 */
|
|
319
|
+
workday?: YsDayOrdinal;
|
|
320
|
+
/** 本月第几个交易日;不是交易日或月份信息不全时没有。 */
|
|
321
|
+
trading?: YsDayOrdinal;
|
|
322
|
+
/** 交易日标记(节前最后、节后首日、月初、月末)。 */
|
|
323
|
+
tags: YsTradingTag[];
|
|
324
|
+
/** 所在的连续休息(含法定假日):如国庆假期第 3 天、共 8 天;两头都在数据范围里才给。 */
|
|
325
|
+
holiday?: YsDayOrdinal & { name: string };
|
|
326
|
+
/** 农历;超出 1901—2100 年为 null。 */
|
|
327
|
+
lunar: YsLunarDate | null;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 汇总某一天的详情(只用服务端下发的 days 推,不自己按星期猜)。
|
|
332
|
+
*
|
|
333
|
+
* ```ts
|
|
334
|
+
* ysDayDetail(days, "2026-09-30") // { workday: { index: 21, total: 21 }, trading: { index: 21, total: 21 }, tags: ["pre-holiday", "month-last"], lunar: { … } }
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
export function ysDayDetail(
|
|
338
|
+
days: readonly YsCalendarDay[],
|
|
339
|
+
date: string,
|
|
340
|
+
): YsDayDetail {
|
|
341
|
+
const byDate = new Map(days.map((item) => [item.date, item]));
|
|
342
|
+
const info = byDate.get(date);
|
|
343
|
+
const detail: YsDayDetail = {
|
|
344
|
+
date,
|
|
345
|
+
info,
|
|
346
|
+
tags: [],
|
|
347
|
+
lunar: ysIsDate(date) ? ysLunar(date) : null,
|
|
348
|
+
};
|
|
349
|
+
if (!info || info.covered === false) return detail;
|
|
350
|
+
const month = ysMonthOf(date);
|
|
351
|
+
if (monthComplete(byDate, month)) {
|
|
352
|
+
const list = days
|
|
353
|
+
.filter((item) => item.date.startsWith(`${month}-`))
|
|
354
|
+
.sort((a, b) => a.date.localeCompare(b.date));
|
|
355
|
+
const ordinal = (test: (item: YsCalendarDay) => boolean) => {
|
|
356
|
+
const matched = list.filter(test);
|
|
357
|
+
const index = matched.findIndex((item) => item.date === date);
|
|
358
|
+
return index >= 0
|
|
359
|
+
? { index: index + 1, total: matched.length }
|
|
360
|
+
: undefined;
|
|
361
|
+
};
|
|
362
|
+
detail.workday = ordinal((item) => Boolean(item.workday));
|
|
363
|
+
detail.trading = ordinal((item) => Boolean(item.trading));
|
|
364
|
+
}
|
|
365
|
+
detail.tags = ysTradingMarks(days).get(date)?.tags ?? [];
|
|
366
|
+
if (!info.workday) {
|
|
367
|
+
// 往两边找到工作日为止;中间有法定假日才算假期,两头都要在数据范围里
|
|
368
|
+
const rest = (day: string) => {
|
|
369
|
+
const item = byDate.get(day);
|
|
370
|
+
return item && item.covered !== false ? !item.workday : undefined;
|
|
371
|
+
};
|
|
372
|
+
let start = date;
|
|
373
|
+
let end = date;
|
|
374
|
+
let known = true;
|
|
375
|
+
for (;;) {
|
|
376
|
+
const state = rest(ysAddDays(start, -1));
|
|
377
|
+
if (state === undefined) known = false;
|
|
378
|
+
if (!state) break;
|
|
379
|
+
start = ysAddDays(start, -1);
|
|
380
|
+
}
|
|
381
|
+
for (;;) {
|
|
382
|
+
const state = rest(ysAddDays(end, 1));
|
|
383
|
+
if (state === undefined) known = false;
|
|
384
|
+
if (!state) break;
|
|
385
|
+
end = ysAddDays(end, 1);
|
|
386
|
+
}
|
|
387
|
+
let name = "";
|
|
388
|
+
for (let day = start; day <= end; day = ysAddDays(day, 1)) {
|
|
389
|
+
const item = byDate.get(day);
|
|
390
|
+
if (item?.kind === "holiday" && item.name) {
|
|
391
|
+
name = item.name;
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (known && name)
|
|
396
|
+
detail.holiday = {
|
|
397
|
+
name,
|
|
398
|
+
index: ysDaysBetween(start, date) + 1,
|
|
399
|
+
total: ysDaysBetween(start, end) + 1,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
return detail;
|
|
403
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -135,12 +135,21 @@ export {
|
|
|
135
135
|
ysShiftMonth,
|
|
136
136
|
ysToday,
|
|
137
137
|
ysTradingMarks,
|
|
138
|
+
ysDayDetail,
|
|
138
139
|
ysWeekday,
|
|
139
140
|
ysWeekdayLabel,
|
|
140
141
|
type YsCalendarDay,
|
|
142
|
+
type YsDayDetail,
|
|
143
|
+
type YsDayOrdinal,
|
|
141
144
|
type YsMonthSummary,
|
|
142
145
|
type YsTradingMark,
|
|
143
146
|
type YsTradingTag,
|
|
144
147
|
type YsDayKind,
|
|
145
148
|
type YsMonthCell,
|
|
146
149
|
} from "./calendar";
|
|
150
|
+
export {
|
|
151
|
+
ysLunar,
|
|
152
|
+
YS_LUNAR_MIN_YEAR,
|
|
153
|
+
YS_LUNAR_MAX_YEAR,
|
|
154
|
+
type YsLunarDate,
|
|
155
|
+
} from "./lunar";
|
package/src/lunar.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 农历(中国传统历法)与二十四节气:按天文算法现算,不依赖第三方库和浏览器的 Intl 农历。
|
|
3
|
+
*
|
|
4
|
+
* - 朔日(每月初一)用 Meeus《天文算法》第 49 章的月相公式,节气用第 25 章的太阳视黄经,换成北京时间(东八区)
|
|
5
|
+
* 取日期;冬至所在的月是十一月,两个冬至之间有 13 个朔望月的年份,冬至后第一个不含中气的月是闰月。
|
|
6
|
+
* - 支持 1901—2100 年。节气、朔日恰好落在午夜前后几分钟时,个别日期可能与官方历书差一天(这一范围内已知没有)。
|
|
7
|
+
* - 浏览器自带的 Intl 农历在 2027 年春节等午夜附近的朔日上会错一天,所以不用它。
|
|
8
|
+
* - 只用于显示(日期详情、节日),不用于任何交易、考勤判断。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const RAD = Math.PI / 180;
|
|
12
|
+
const DAY_MS = 86_400_000;
|
|
13
|
+
|
|
14
|
+
export interface YsLunarDate {
|
|
15
|
+
/** 农历年(以正月初一换年),如 2026。 */
|
|
16
|
+
year: number;
|
|
17
|
+
/** 农历月 1—12。 */
|
|
18
|
+
month: number;
|
|
19
|
+
/** 农历日 1—30。 */
|
|
20
|
+
day: number;
|
|
21
|
+
/** 是否闰月。 */
|
|
22
|
+
leap: boolean;
|
|
23
|
+
/** 干支年,如「丙午」。 */
|
|
24
|
+
yearName: string;
|
|
25
|
+
/** 生肖,如「马」。 */
|
|
26
|
+
zodiac: string;
|
|
27
|
+
/** 月名,如「八月」「闰六月」「腊月」。 */
|
|
28
|
+
monthName: string;
|
|
29
|
+
/** 日名,如「初一」「二十」「廿九」。 */
|
|
30
|
+
dayName: string;
|
|
31
|
+
/** 这个月有几天(29 或 30)。 */
|
|
32
|
+
monthDays: number;
|
|
33
|
+
/** 传统节日,如「中秋节」「除夕」;没有为空。 */
|
|
34
|
+
festival: string;
|
|
35
|
+
/** 当天交节的节气,如「秋分」;没有为空。 */
|
|
36
|
+
term: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const STEMS = "甲乙丙丁戊己庚辛壬癸";
|
|
40
|
+
const BRANCHES = "子丑寅卯辰巳午未申酉戌亥";
|
|
41
|
+
const ZODIACS = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
|
|
42
|
+
const MONTH_NAMES = [
|
|
43
|
+
"正",
|
|
44
|
+
"二",
|
|
45
|
+
"三",
|
|
46
|
+
"四",
|
|
47
|
+
"五",
|
|
48
|
+
"六",
|
|
49
|
+
"七",
|
|
50
|
+
"八",
|
|
51
|
+
"九",
|
|
52
|
+
"十",
|
|
53
|
+
"冬",
|
|
54
|
+
"腊",
|
|
55
|
+
];
|
|
56
|
+
const DIGITS = "一二三四五六七八九十";
|
|
57
|
+
/** 二十四节气,从春分(太阳视黄经 0°)起每 15° 一个。 */
|
|
58
|
+
const TERMS = [
|
|
59
|
+
"春分",
|
|
60
|
+
"清明",
|
|
61
|
+
"谷雨",
|
|
62
|
+
"立夏",
|
|
63
|
+
"小满",
|
|
64
|
+
"芒种",
|
|
65
|
+
"夏至",
|
|
66
|
+
"小暑",
|
|
67
|
+
"大暑",
|
|
68
|
+
"立秋",
|
|
69
|
+
"处暑",
|
|
70
|
+
"白露",
|
|
71
|
+
"秋分",
|
|
72
|
+
"寒露",
|
|
73
|
+
"霜降",
|
|
74
|
+
"立冬",
|
|
75
|
+
"小雪",
|
|
76
|
+
"大雪",
|
|
77
|
+
"冬至",
|
|
78
|
+
"小寒",
|
|
79
|
+
"大寒",
|
|
80
|
+
"立春",
|
|
81
|
+
"雨水",
|
|
82
|
+
"惊蛰",
|
|
83
|
+
];
|
|
84
|
+
const FESTIVALS: Record<string, string> = {
|
|
85
|
+
"1-1": "春节",
|
|
86
|
+
"1-15": "元宵节",
|
|
87
|
+
"2-2": "龙抬头",
|
|
88
|
+
"5-5": "端午节",
|
|
89
|
+
"7-7": "七夕节",
|
|
90
|
+
"7-15": "中元节",
|
|
91
|
+
"8-15": "中秋节",
|
|
92
|
+
"9-9": "重阳节",
|
|
93
|
+
"12-8": "腊八节",
|
|
94
|
+
"12-23": "北方小年",
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const YS_LUNAR_MIN_YEAR = 1901;
|
|
98
|
+
export const YS_LUNAR_MAX_YEAR = 2100;
|
|
99
|
+
|
|
100
|
+
// ---------- 儒略日与日期 ----------
|
|
101
|
+
/** 公历日期的儒略日数(当天正午)。 */
|
|
102
|
+
function jdnOf(date: string): number {
|
|
103
|
+
return Date.parse(`${date}T12:00:00Z`) / DAY_MS + 2440587.5;
|
|
104
|
+
}
|
|
105
|
+
function dateOfJdn(jdn: number): string {
|
|
106
|
+
return new Date((jdn - 2440587.5) * DAY_MS).toISOString().slice(0, 10);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** ΔT = TT − UT(秒),Espenak & Meeus 多项式。 */
|
|
110
|
+
function deltaT(year: number): number {
|
|
111
|
+
if (year < 1920) {
|
|
112
|
+
const t = year - 1900;
|
|
113
|
+
return (
|
|
114
|
+
-2.79 +
|
|
115
|
+
1.494119 * t -
|
|
116
|
+
0.0598939 * t ** 2 +
|
|
117
|
+
0.0061966 * t ** 3 -
|
|
118
|
+
0.000197 * t ** 4
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (year < 1941) {
|
|
122
|
+
const t = year - 1920;
|
|
123
|
+
return 21.2 + 0.84493 * t - 0.0761 * t ** 2 + 0.0020936 * t ** 3;
|
|
124
|
+
}
|
|
125
|
+
if (year < 1961) {
|
|
126
|
+
const t = year - 1950;
|
|
127
|
+
return 29.07 + 0.407 * t - t ** 2 / 233 + t ** 3 / 2547;
|
|
128
|
+
}
|
|
129
|
+
if (year < 1986) {
|
|
130
|
+
const t = year - 1975;
|
|
131
|
+
return 45.45 + 1.067 * t - t ** 2 / 260 - t ** 3 / 718;
|
|
132
|
+
}
|
|
133
|
+
if (year < 2005) {
|
|
134
|
+
const t = year - 2000;
|
|
135
|
+
return (
|
|
136
|
+
63.86 +
|
|
137
|
+
0.3345 * t -
|
|
138
|
+
0.060374 * t ** 2 +
|
|
139
|
+
0.0017275 * t ** 3 +
|
|
140
|
+
0.000651814 * t ** 4 +
|
|
141
|
+
0.00002373599 * t ** 5
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (year < 2050) {
|
|
145
|
+
const t = year - 2000;
|
|
146
|
+
return 62.92 + 0.32217 * t + 0.005589 * t ** 2;
|
|
147
|
+
}
|
|
148
|
+
return -20 + 32 * ((year - 1820) / 100) ** 2 - 0.5628 * (2150 - year);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 力学时儒略日 → 北京时间所在日期的儒略日数。 */
|
|
152
|
+
function beijingJdn(jde: number): number {
|
|
153
|
+
const year = 2000 + (jde - 2451545) / 365.25;
|
|
154
|
+
const ut = jde - deltaT(year) / 86400;
|
|
155
|
+
return Math.floor(ut + 8 / 24 + 0.5);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ---------- 朔日(Meeus 第 49 章)----------
|
|
159
|
+
function newMoonJde(k: number): number {
|
|
160
|
+
const t = k / 1236.85;
|
|
161
|
+
let jde =
|
|
162
|
+
2451550.09766 +
|
|
163
|
+
29.530588861 * k +
|
|
164
|
+
0.00015437 * t ** 2 -
|
|
165
|
+
0.00000015 * t ** 3 +
|
|
166
|
+
0.00000000073 * t ** 4;
|
|
167
|
+
const e = 1 - 0.002516 * t - 0.0000074 * t ** 2;
|
|
168
|
+
const m =
|
|
169
|
+
(2.5534 + 29.1053567 * k - 0.0000014 * t ** 2 - 0.00000011 * t ** 3) * RAD;
|
|
170
|
+
const mp =
|
|
171
|
+
(201.5643 +
|
|
172
|
+
385.81693528 * k +
|
|
173
|
+
0.0107582 * t ** 2 +
|
|
174
|
+
0.00001238 * t ** 3 -
|
|
175
|
+
0.000000058 * t ** 4) *
|
|
176
|
+
RAD;
|
|
177
|
+
const f =
|
|
178
|
+
(160.7108 +
|
|
179
|
+
390.67050284 * k -
|
|
180
|
+
0.0016118 * t ** 2 -
|
|
181
|
+
0.00000227 * t ** 3 +
|
|
182
|
+
0.000000011 * t ** 4) *
|
|
183
|
+
RAD;
|
|
184
|
+
const om =
|
|
185
|
+
(124.7746 - 1.56375588 * k + 0.0020672 * t ** 2 + 0.00000215 * t ** 3) *
|
|
186
|
+
RAD;
|
|
187
|
+
const s = Math.sin;
|
|
188
|
+
jde +=
|
|
189
|
+
-0.4072 * s(mp) +
|
|
190
|
+
0.17241 * e * s(m) +
|
|
191
|
+
0.01608 * s(2 * mp) +
|
|
192
|
+
0.01039 * s(2 * f) +
|
|
193
|
+
0.00739 * e * s(mp - m) -
|
|
194
|
+
0.00514 * e * s(mp + m) +
|
|
195
|
+
0.00208 * e * e * s(2 * m) -
|
|
196
|
+
0.00111 * s(mp - 2 * f) -
|
|
197
|
+
0.00057 * s(mp + 2 * f) +
|
|
198
|
+
0.00056 * e * s(2 * mp + m) -
|
|
199
|
+
0.00042 * s(3 * mp) +
|
|
200
|
+
0.00042 * e * s(m + 2 * f) +
|
|
201
|
+
0.00038 * e * s(m - 2 * f) -
|
|
202
|
+
0.00024 * e * s(2 * mp - m) -
|
|
203
|
+
0.00017 * s(om) -
|
|
204
|
+
0.00007 * s(mp + 2 * m) +
|
|
205
|
+
0.00004 * s(2 * mp - 2 * f) +
|
|
206
|
+
0.00004 * s(3 * m) +
|
|
207
|
+
0.00003 * s(mp + m - 2 * f) +
|
|
208
|
+
0.00003 * s(2 * mp + 2 * f) -
|
|
209
|
+
0.00003 * s(mp + m + 2 * f) +
|
|
210
|
+
0.00003 * s(mp - m + 2 * f) -
|
|
211
|
+
0.00002 * s(mp - m - 2 * f) -
|
|
212
|
+
0.00002 * s(3 * mp + m) +
|
|
213
|
+
0.00002 * s(4 * mp);
|
|
214
|
+
const extra: [number, number, number][] = [
|
|
215
|
+
[0.000325, 299.77, 0.107408],
|
|
216
|
+
[0.000165, 251.88, 0.016321],
|
|
217
|
+
[0.000164, 251.83, 26.651886],
|
|
218
|
+
[0.000126, 349.42, 36.412478],
|
|
219
|
+
[0.00011, 84.66, 18.206239],
|
|
220
|
+
[0.000062, 141.74, 53.303771],
|
|
221
|
+
[0.00006, 207.14, 2.453732],
|
|
222
|
+
[0.000056, 154.84, 7.30686],
|
|
223
|
+
[0.000047, 34.52, 27.261239],
|
|
224
|
+
[0.000042, 207.19, 0.121824],
|
|
225
|
+
[0.00004, 291.34, 1.844379],
|
|
226
|
+
[0.000037, 161.72, 24.198154],
|
|
227
|
+
[0.000035, 239.56, 25.513099],
|
|
228
|
+
[0.000023, 331.55, 3.592518],
|
|
229
|
+
];
|
|
230
|
+
for (const [index, [amp, base, rate]] of extra.entries()) {
|
|
231
|
+
const angle = base + rate * k - (index === 0 ? 0.009173 * t ** 2 : 0);
|
|
232
|
+
jde += amp * s(angle * RAD);
|
|
233
|
+
}
|
|
234
|
+
return jde;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 某一天(儒略日数)当天或之前最近的朔日,返回北京日期的儒略日数。 */
|
|
238
|
+
function newMoonOnOrBefore(jdn: number): number {
|
|
239
|
+
let k = Math.floor((jdn - 2451550.1) / 29.530588861) + 1;
|
|
240
|
+
for (;;) {
|
|
241
|
+
const day = beijingJdn(newMoonJde(k));
|
|
242
|
+
if (day <= jdn) return day;
|
|
243
|
+
k -= 1;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function newMoonAfter(jdn: number): number {
|
|
247
|
+
let k = Math.floor((jdn - 2451550.1) / 29.530588861) - 1;
|
|
248
|
+
for (;;) {
|
|
249
|
+
const day = beijingJdn(newMoonJde(k));
|
|
250
|
+
if (day > jdn) return day;
|
|
251
|
+
k += 1;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---------- 节气(太阳视黄经,Meeus 第 25 章)----------
|
|
256
|
+
function sunLongitude(jde: number): number {
|
|
257
|
+
const t = (jde - 2451545) / 36525;
|
|
258
|
+
const l0 = 280.46646 + 36000.76983 * t + 0.0003032 * t ** 2;
|
|
259
|
+
const m = (357.52911 + 35999.05029 * t - 0.0001537 * t ** 2) * RAD;
|
|
260
|
+
const c =
|
|
261
|
+
(1.914602 - 0.004817 * t - 0.000014 * t ** 2) * Math.sin(m) +
|
|
262
|
+
(0.019993 - 0.000101 * t) * Math.sin(2 * m) +
|
|
263
|
+
0.000289 * Math.sin(3 * m);
|
|
264
|
+
const om = (125.04 - 1934.136 * t) * RAD;
|
|
265
|
+
const value = l0 + c - 0.00569 - 0.00478 * Math.sin(om);
|
|
266
|
+
return ((value % 360) + 360) % 360;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** 公历 year 年里太阳视黄经到 angle 度的时刻(力学时儒略日);285° 以上在 1—3 月。 */
|
|
270
|
+
function termJde(year: number, angle: number): number {
|
|
271
|
+
// 春分约在 3 月 20 日,每度约 1.0146 天
|
|
272
|
+
const offset = angle >= 285 ? angle - 360 : angle;
|
|
273
|
+
let jde = jdnOf(`${year}-03-20`) + (offset * 365.2422) / 360;
|
|
274
|
+
for (let index = 0; index < 8; index += 1) {
|
|
275
|
+
let diff = angle - sunLongitude(jde);
|
|
276
|
+
diff = ((diff + 540) % 360) - 180;
|
|
277
|
+
jde += (diff * 365.2422) / 360;
|
|
278
|
+
if (Math.abs(diff) < 0.000001) break;
|
|
279
|
+
}
|
|
280
|
+
return jde;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** 某年各节气所在的北京日期(儒略日数 → 节气序号)。 */
|
|
284
|
+
const termCache = new Map<number, Map<number, number>>();
|
|
285
|
+
function termsOfYear(year: number): Map<number, number> {
|
|
286
|
+
let map = termCache.get(year);
|
|
287
|
+
if (!map) {
|
|
288
|
+
map = new Map();
|
|
289
|
+
for (let index = 0; index < 24; index += 1) {
|
|
290
|
+
// 春分起算的序号;小寒、大寒、立春、雨水、惊蛰在当年春分之前
|
|
291
|
+
const angle = index * 15;
|
|
292
|
+
const y = index >= 19 ? year + 1 : year;
|
|
293
|
+
map.set(beijingJdn(termJde(y, angle)), index);
|
|
294
|
+
}
|
|
295
|
+
termCache.set(year, map);
|
|
296
|
+
}
|
|
297
|
+
return map;
|
|
298
|
+
}
|
|
299
|
+
function termOn(jdn: number, year: number): number | undefined {
|
|
300
|
+
return termsOfYear(year).get(jdn) ?? termsOfYear(year - 1).get(jdn);
|
|
301
|
+
}
|
|
302
|
+
/** 冬至(270°)所在的北京日期。 */
|
|
303
|
+
function winterSolstice(year: number): number {
|
|
304
|
+
return beijingJdn(termJde(year, 270));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ---------- 农历月表 ----------
|
|
308
|
+
interface LunarMonth {
|
|
309
|
+
start: number;
|
|
310
|
+
month: number;
|
|
311
|
+
leap: boolean;
|
|
312
|
+
days: number;
|
|
313
|
+
year: number;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** 从 year 年冬至所在月(十一月)到下一年冬至所在月之前的各月。 */
|
|
317
|
+
const cycleCache = new Map<number, LunarMonth[]>();
|
|
318
|
+
function lunarCycle(year: number): LunarMonth[] {
|
|
319
|
+
const cached = cycleCache.get(year);
|
|
320
|
+
if (cached) return cached;
|
|
321
|
+
const first = newMoonOnOrBefore(winterSolstice(year));
|
|
322
|
+
const last = newMoonOnOrBefore(winterSolstice(year + 1));
|
|
323
|
+
const starts = [first];
|
|
324
|
+
while (starts[starts.length - 1] < last)
|
|
325
|
+
starts.push(newMoonAfter(starts[starts.length - 1]));
|
|
326
|
+
// starts 最后一个就是下一个十一月
|
|
327
|
+
const count = starts.length - 1;
|
|
328
|
+
const hasMajorTerm = (from: number, to: number) => {
|
|
329
|
+
for (let day = from; day < to; day += 1) {
|
|
330
|
+
const term = termOn(day, year + 1) ?? termOn(day, year + 2);
|
|
331
|
+
// 中气:从春分起偶数序号(春分、谷雨、小满……冬至、大寒、雨水)
|
|
332
|
+
if (term !== undefined && term % 2 === 0) return true;
|
|
333
|
+
}
|
|
334
|
+
return false;
|
|
335
|
+
};
|
|
336
|
+
let leapIndex = -1;
|
|
337
|
+
if (count === 13) {
|
|
338
|
+
for (let index = 1; index < count; index += 1) {
|
|
339
|
+
if (!hasMajorTerm(starts[index], starts[index + 1])) {
|
|
340
|
+
leapIndex = index;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const months: LunarMonth[] = [];
|
|
346
|
+
let number = 11;
|
|
347
|
+
for (let index = 0; index < count; index += 1) {
|
|
348
|
+
const leap = index === leapIndex;
|
|
349
|
+
if (index > 0 && !leap) number = (number % 12) + 1;
|
|
350
|
+
months.push({
|
|
351
|
+
start: starts[index],
|
|
352
|
+
month: number,
|
|
353
|
+
leap,
|
|
354
|
+
days: starts[index + 1] - starts[index],
|
|
355
|
+
year: 0,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
// 正月起换年:正月之前的十一月、十二月属于上一年
|
|
359
|
+
const firstMonth = months.findIndex((item) => item.month === 1 && !item.leap);
|
|
360
|
+
const newYear = Number(dateOfJdn(months[firstMonth].start).slice(0, 4));
|
|
361
|
+
for (const [index, item] of months.entries())
|
|
362
|
+
item.year = index < firstMonth ? newYear - 1 : newYear;
|
|
363
|
+
cycleCache.set(year, months);
|
|
364
|
+
return months;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function dayName(day: number): string {
|
|
368
|
+
if (day === 10) return "初十";
|
|
369
|
+
if (day === 20) return "二十";
|
|
370
|
+
if (day === 30) return "三十";
|
|
371
|
+
const tens = ["初", "十", "廿"][Math.floor(day / 10)];
|
|
372
|
+
return `${tens}${DIGITS[(day % 10) - 1]}`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* 公历日期(YYYY-MM-DD)对应的农历;超出 1901—2100 年或日期不合法返回 null。
|
|
377
|
+
*
|
|
378
|
+
* ```ts
|
|
379
|
+
* ysLunar("2026-09-25") // { monthName: "八月", dayName: "十五", festival: "中秋节", yearName: "丙午", zodiac: "马", ... }
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
export function ysLunar(date: string): YsLunarDate | null {
|
|
383
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) return null;
|
|
384
|
+
const jdn = jdnOf(date);
|
|
385
|
+
if (!Number.isFinite(jdn) || dateOfJdn(jdn) !== date) return null;
|
|
386
|
+
const year = Number(date.slice(0, 4));
|
|
387
|
+
if (year < YS_LUNAR_MIN_YEAR || year > YS_LUNAR_MAX_YEAR) return null;
|
|
388
|
+
// 当年冬至之后的日子属于当年这一轮,否则属于上一年那一轮
|
|
389
|
+
const cycleYear =
|
|
390
|
+
jdn >= newMoonOnOrBefore(winterSolstice(year)) ? year : year - 1;
|
|
391
|
+
const months = lunarCycle(cycleYear);
|
|
392
|
+
let found = months[0];
|
|
393
|
+
for (const item of months) if (item.start <= jdn) found = item;
|
|
394
|
+
const day = jdn - found.start + 1;
|
|
395
|
+
const offset = (((found.year - 4) % 60) + 60) % 60;
|
|
396
|
+
// 除夕:正月初一的前一天(腊月二十九或三十)
|
|
397
|
+
const next =
|
|
398
|
+
months[months.indexOf(found) + 1] ?? lunarCycle(cycleYear + 1)[0];
|
|
399
|
+
const isEve =
|
|
400
|
+
!found.leap && found.month === 12 && next.month === 1 && day === found.days;
|
|
401
|
+
const festival = isEve
|
|
402
|
+
? "除夕"
|
|
403
|
+
: found.leap
|
|
404
|
+
? ""
|
|
405
|
+
: (FESTIVALS[`${found.month}-${day}`] ?? "");
|
|
406
|
+
const term = termOn(jdn, year);
|
|
407
|
+
return {
|
|
408
|
+
year: found.year,
|
|
409
|
+
month: found.month,
|
|
410
|
+
day,
|
|
411
|
+
leap: found.leap,
|
|
412
|
+
yearName: `${STEMS[offset % 10]}${BRANCHES[offset % 12]}`,
|
|
413
|
+
zodiac: ZODIACS[offset % 12],
|
|
414
|
+
monthName: `${found.leap ? "闰" : ""}${MONTH_NAMES[found.month - 1]}月`,
|
|
415
|
+
dayName: dayName(day),
|
|
416
|
+
monthDays: found.days,
|
|
417
|
+
festival,
|
|
418
|
+
term: term === undefined ? "" : TERMS[term],
|
|
419
|
+
};
|
|
420
|
+
}
|