@ysgroup/core 0.1.32 → 0.1.34
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 +169 -1
- package/src/index.ts +14 -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
|
|
|
@@ -20,6 +22,72 @@ export interface YsCalendarDay {
|
|
|
20
22
|
workday?: boolean;
|
|
21
23
|
trading?: boolean;
|
|
22
24
|
covered?: boolean;
|
|
25
|
+
/** 交易所额外休市的原因(国务院安排是工作日、交易所另行休市,如 2024-02-09 除夕);不是时为空。 */
|
|
26
|
+
market_closed?: string;
|
|
27
|
+
/** 当晚有没有期货夜盘:只对交易日给出;下一交易日所在年份还没有安排时为 null(未知)。 */
|
|
28
|
+
night_session?: boolean | null;
|
|
29
|
+
/** 当晚夜盘归属的交易日(有夜盘时才有)。 */
|
|
30
|
+
night_trading_day?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 某一年日历数据的来源:auth Auth 下发、bundled 内置快照、unpublished 国务院还没发布、unknown 没有数据。 */
|
|
34
|
+
export type YsCalendarYearSource =
|
|
35
|
+
| "auth"
|
|
36
|
+
| "bundled"
|
|
37
|
+
| "unpublished"
|
|
38
|
+
| "unknown";
|
|
39
|
+
|
|
40
|
+
/** 一个数据源的核对结果(Auth calendar_years.checks)。 */
|
|
41
|
+
export interface YsCalendarCheck {
|
|
42
|
+
source: string;
|
|
43
|
+
label?: string;
|
|
44
|
+
kind?: string;
|
|
45
|
+
status:
|
|
46
|
+
| "agree"
|
|
47
|
+
| "differ"
|
|
48
|
+
| "unavailable"
|
|
49
|
+
| "no_data"
|
|
50
|
+
| "unpublished"
|
|
51
|
+
| "skipped";
|
|
52
|
+
status_label?: string;
|
|
53
|
+
covered_from?: string;
|
|
54
|
+
covered_to?: string;
|
|
55
|
+
differences?: string[];
|
|
56
|
+
message?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 一年的来源与核对状态(服务端 CalendarService.year_states() 的一行)。 */
|
|
60
|
+
export interface YsCalendarYearInfo {
|
|
61
|
+
year: number;
|
|
62
|
+
status: YsCalendarYearSource;
|
|
63
|
+
status_label?: string;
|
|
64
|
+
covered?: boolean;
|
|
65
|
+
/** 已与交易所数据核对且一致。 */
|
|
66
|
+
verified?: boolean;
|
|
67
|
+
checks?: YsCalendarCheck[];
|
|
68
|
+
checked_at?: number | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const YS_CALENDAR_SOURCE_LABELS: Record<YsCalendarYearSource, string> = {
|
|
72
|
+
auth: "Auth 下发",
|
|
73
|
+
bundled: "内置数据",
|
|
74
|
+
unpublished: "国务院还没发布",
|
|
75
|
+
unknown: "还没有数据",
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** 「Auth 下发 · 已与交易所核对」「内置数据 · 已核验」「Auth 下发 · 按国务院安排推算,未经交易所核对」。 */
|
|
79
|
+
export function ysCalendarYearText(
|
|
80
|
+
info: YsCalendarYearInfo | undefined,
|
|
81
|
+
): string {
|
|
82
|
+
if (!info) return "";
|
|
83
|
+
const base =
|
|
84
|
+
info.status_label || YS_CALENDAR_SOURCE_LABELS[info.status] || "";
|
|
85
|
+
if (info.status === "unpublished" || info.status === "unknown") return base;
|
|
86
|
+
const differ = info.checks?.some((item) => item.status === "differ");
|
|
87
|
+
if (differ) return `${base} · 数据源有差异`;
|
|
88
|
+
return info.verified
|
|
89
|
+
? `${base} · 已与交易所核对`
|
|
90
|
+
: `${base} · 按国务院安排推算,未经交易所核对`;
|
|
23
91
|
}
|
|
24
92
|
|
|
25
93
|
export interface YsMonthCell {
|
|
@@ -247,7 +315,7 @@ export function ysTradingMarks(
|
|
|
247
315
|
break;
|
|
248
316
|
}
|
|
249
317
|
if (isTrading(next)) break;
|
|
250
|
-
if (next.kind === "holiday") holiday = true;
|
|
318
|
+
if (next.kind === "holiday" || next.market_closed) holiday = true;
|
|
251
319
|
cursor = ysAddDays(cursor, step);
|
|
252
320
|
}
|
|
253
321
|
if (known && holiday) mark.tags.push(tag);
|
|
@@ -280,6 +348,8 @@ export interface YsMonthSummary {
|
|
|
280
348
|
closed: number;
|
|
281
349
|
holidays: number;
|
|
282
350
|
adjusted: number;
|
|
351
|
+
/** 交易所额外休市天数(国务院安排是工作日)。 */
|
|
352
|
+
marketClosed: number;
|
|
283
353
|
}
|
|
284
354
|
|
|
285
355
|
/** 一个月的工作日、交易日、休市、放假、调休上班天数。 */
|
|
@@ -300,5 +370,103 @@ export function ysMonthSummary(
|
|
|
300
370
|
closed: count((item) => !item.trading),
|
|
301
371
|
holidays: count((item) => item.kind === "holiday"),
|
|
302
372
|
adjusted: count((item) => item.kind === "adjusted"),
|
|
373
|
+
marketClosed: count((item) => Boolean(item.market_closed)),
|
|
303
374
|
};
|
|
304
375
|
}
|
|
376
|
+
|
|
377
|
+
/** 某天在本月里的位置:第几个、共几个。 */
|
|
378
|
+
export interface YsDayOrdinal {
|
|
379
|
+
index: number;
|
|
380
|
+
total: number;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** 选中日详情需要的汇总:本月第几个工作日、交易日,交易日标记,所在假期,农历。 */
|
|
384
|
+
export interface YsDayDetail {
|
|
385
|
+
date: string;
|
|
386
|
+
info?: YsCalendarDay;
|
|
387
|
+
/** 本月第几个工作日(含法定补班);不是工作日或月份信息不全时没有。 */
|
|
388
|
+
workday?: YsDayOrdinal;
|
|
389
|
+
/** 本月第几个交易日;不是交易日或月份信息不全时没有。 */
|
|
390
|
+
trading?: YsDayOrdinal;
|
|
391
|
+
/** 交易日标记(节前最后、节后首日、月初、月末)。 */
|
|
392
|
+
tags: YsTradingTag[];
|
|
393
|
+
/** 所在的连续休息(含法定假日):如国庆假期第 3 天、共 8 天;两头都在数据范围里才给。 */
|
|
394
|
+
holiday?: YsDayOrdinal & { name: string };
|
|
395
|
+
/** 农历;超出 1901—2100 年为 null。 */
|
|
396
|
+
lunar: YsLunarDate | null;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* 汇总某一天的详情(只用服务端下发的 days 推,不自己按星期猜)。
|
|
401
|
+
*
|
|
402
|
+
* ```ts
|
|
403
|
+
* ysDayDetail(days, "2026-09-30") // { workday: { index: 21, total: 21 }, trading: { index: 21, total: 21 }, tags: ["pre-holiday", "month-last"], lunar: { … } }
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
export function ysDayDetail(
|
|
407
|
+
days: readonly YsCalendarDay[],
|
|
408
|
+
date: string,
|
|
409
|
+
): YsDayDetail {
|
|
410
|
+
const byDate = new Map(days.map((item) => [item.date, item]));
|
|
411
|
+
const info = byDate.get(date);
|
|
412
|
+
const detail: YsDayDetail = {
|
|
413
|
+
date,
|
|
414
|
+
info,
|
|
415
|
+
tags: [],
|
|
416
|
+
lunar: ysIsDate(date) ? ysLunar(date) : null,
|
|
417
|
+
};
|
|
418
|
+
if (!info || info.covered === false) return detail;
|
|
419
|
+
const month = ysMonthOf(date);
|
|
420
|
+
if (monthComplete(byDate, month)) {
|
|
421
|
+
const list = days
|
|
422
|
+
.filter((item) => item.date.startsWith(`${month}-`))
|
|
423
|
+
.sort((a, b) => a.date.localeCompare(b.date));
|
|
424
|
+
const ordinal = (test: (item: YsCalendarDay) => boolean) => {
|
|
425
|
+
const matched = list.filter(test);
|
|
426
|
+
const index = matched.findIndex((item) => item.date === date);
|
|
427
|
+
return index >= 0
|
|
428
|
+
? { index: index + 1, total: matched.length }
|
|
429
|
+
: undefined;
|
|
430
|
+
};
|
|
431
|
+
detail.workday = ordinal((item) => Boolean(item.workday));
|
|
432
|
+
detail.trading = ordinal((item) => Boolean(item.trading));
|
|
433
|
+
}
|
|
434
|
+
detail.tags = ysTradingMarks(days).get(date)?.tags ?? [];
|
|
435
|
+
if (!info.workday) {
|
|
436
|
+
// 往两边找到工作日为止;中间有法定假日才算假期,两头都要在数据范围里
|
|
437
|
+
const rest = (day: string) => {
|
|
438
|
+
const item = byDate.get(day);
|
|
439
|
+
return item && item.covered !== false ? !item.workday : undefined;
|
|
440
|
+
};
|
|
441
|
+
let start = date;
|
|
442
|
+
let end = date;
|
|
443
|
+
let known = true;
|
|
444
|
+
for (;;) {
|
|
445
|
+
const state = rest(ysAddDays(start, -1));
|
|
446
|
+
if (state === undefined) known = false;
|
|
447
|
+
if (!state) break;
|
|
448
|
+
start = ysAddDays(start, -1);
|
|
449
|
+
}
|
|
450
|
+
for (;;) {
|
|
451
|
+
const state = rest(ysAddDays(end, 1));
|
|
452
|
+
if (state === undefined) known = false;
|
|
453
|
+
if (!state) break;
|
|
454
|
+
end = ysAddDays(end, 1);
|
|
455
|
+
}
|
|
456
|
+
let name = "";
|
|
457
|
+
for (let day = start; day <= end; day = ysAddDays(day, 1)) {
|
|
458
|
+
const item = byDate.get(day);
|
|
459
|
+
if (item?.kind === "holiday" && item.name) {
|
|
460
|
+
name = item.name;
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
if (known && name)
|
|
465
|
+
detail.holiday = {
|
|
466
|
+
name,
|
|
467
|
+
index: ysDaysBetween(start, date) + 1,
|
|
468
|
+
total: ysDaysBetween(start, end) + 1,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
return detail;
|
|
472
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -119,10 +119,12 @@ export {
|
|
|
119
119
|
type YsStatusOption,
|
|
120
120
|
} from "./status";
|
|
121
121
|
export {
|
|
122
|
+
YS_CALENDAR_SOURCE_LABELS,
|
|
122
123
|
YS_DAY_KIND_LABELS,
|
|
123
124
|
YS_TRADING_TAG_LABELS,
|
|
124
125
|
YS_WEEKDAY_LABELS,
|
|
125
126
|
ysAddDays,
|
|
127
|
+
ysCalendarYearText,
|
|
126
128
|
ysDateTitle,
|
|
127
129
|
ysDaysBetween,
|
|
128
130
|
ysDaysInMonth,
|
|
@@ -135,12 +137,24 @@ export {
|
|
|
135
137
|
ysShiftMonth,
|
|
136
138
|
ysToday,
|
|
137
139
|
ysTradingMarks,
|
|
140
|
+
ysDayDetail,
|
|
138
141
|
ysWeekday,
|
|
139
142
|
ysWeekdayLabel,
|
|
143
|
+
type YsCalendarCheck,
|
|
140
144
|
type YsCalendarDay,
|
|
145
|
+
type YsCalendarYearInfo,
|
|
146
|
+
type YsCalendarYearSource,
|
|
147
|
+
type YsDayDetail,
|
|
148
|
+
type YsDayOrdinal,
|
|
141
149
|
type YsMonthSummary,
|
|
142
150
|
type YsTradingMark,
|
|
143
151
|
type YsTradingTag,
|
|
144
152
|
type YsDayKind,
|
|
145
153
|
type YsMonthCell,
|
|
146
154
|
} from "./calendar";
|
|
155
|
+
export {
|
|
156
|
+
ysLunar,
|
|
157
|
+
YS_LUNAR_MIN_YEAR,
|
|
158
|
+
YS_LUNAR_MAX_YEAR,
|
|
159
|
+
type YsLunarDate,
|
|
160
|
+
} 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
|
+
}
|