/
index.js
175 lines (150 loc) 路 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
export function lunar(date = new Date()) {
const aharakoune = y => (y * 292207 + 373) % 800;
const harakoune = (y) => Math.floor((y * 292207 + 373) / 800) + 1;
const avomane = (y) => (11 * harakoune(y) + 650) % 692;
const regularLeap = y => (800 - aharakoune(y)) <= 207
function bodethey(y) {
const ha = harakoune(y);
return (ha + Math.floor((11 * ha + 650) / 692)) % 30;
}
function lunarDiffDays(end) {
let count = 0;
let x = 1970 - 638 + 1;
let y = end.getFullYear() - 638;
if (x > y) {
let _x = x;
x = y;
y = _x;
}
while (x < y) count += daysInYear(x++);
return count;
}
function daysInYear(year) {
if (jaisLeap(year)) return 384;
if (greatLeap(year)) return 355;
return 354;
}
function jaisLeap(y) {
const b0 = bodethey(y);
const b1 = bodethey(y + 1);
return b0 > 24 || b0 < 6 || (b0 === 24 && b1 === 6) || (b0 === 25 && b1 === 5)
}
function langSak(y) {
const i = sakDay(y);
return { month: 3 + (i >= 6 && i <= 29) | 0, day: i };
}
function sakDay(y) {
const bo = bodethey(y)
const bl0 = jaisLeap(y - 1);
if (!bl0 || (bl0 && !isProtetinLeap(y - 1))) {
if (bo < 6) return bo + 1;
return bo;
}
return bo + 1;
}
function greatLeap(y) {
let value = isProtetinLeap(y);
if (jaisLeap(y) && value) value = false;
return value;
}
function isProtetinLeap(y) {
const avomane0 = avomane(y);
const avomane1 = avomane(y + 1);
const normal = regularLeap(y);
let value = normal && avomane0 < 127;
if (!normal) {
if (avomane0 === 137 && avomane1 === 0) value = false;
else if (avomane0 < 138) value = true;
}
if (!value) value = isProtetinLeap(y - 1) && jaisLeap(y - 1);
return value
}
function diffDays(end) {
return Math.abs(
Math.round((end - 286596e5) / (1000 * 60 * 60 * 24))
);
}
function monthsOfYear(year) {
const ath = jaisLeap(year);
const great = greatLeap(year);
const items = [];
for (let i = 0; i < 12 + (ath | 0); i++) {
let j = i;
if (ath && j >= 8) j--;
items.push(29 + ((j % 2 != 0) | 0) + ((j == 6 && great) | 0));
}
return items;
}
if (typeof date === 'string') {
date = new Date(date);
}
date = new Date(date - 7 * 60 * 60 * 1000);
const CE = date.getFullYear();
let y = CE - 638;
let day = Math.abs(diffDays(date) - lunarDiffDays(date)) + 1;
const BE = CE + 543 + (day > 162 | 0);
const len = daysInYear(y)
if (day > len) {
day -= len
y++;
}
let m = 0;
const lengthOfYear = monthsOfYear(y);
for (const month of lengthOfYear) {
if (day <= month) break;
day -= month;
m++;
}
const sak = langSak(y - 1);
const JE = y - 1 - ((sak.month > m || (sak.month === m && sak.day > date.getDate())) | 0)
const yearMonths = [
"M默K脭S臄R",
"B艓SS",
"M脡AKH",
"PH脗LK努N",
"CH脡TR",
"V默SAKH",
"CH脡STH",
"ASATH",
"B脗TH脗MSATH",
"T努T臄Y脡ASATH",
"SRAP脭N脗",
"PH脭TR脭B脗T",
"脗SS艓CH",
"K脗TD臄K"
].filter(
(_, i) => {
if (lengthOfYear.length == 12) return i != 8 && i != 9
else return i != 7;
}
)
const ZODIAC_YEARS = [
"JUTE",
"CHLOV",
"KARL",
"THOS",
"RORNG",
"MASAGN",
"MOMEE",
"MOMAY",
"VOKE",
"ROKA",
"JOR",
"KAOR",
];
return {
day,
period: [(day - 1) % 15 + 1, day > 15 ? 'R' : 'K'],
sequence: (((JE + 1) % 10) + 9) % 10,
zodiac: ZODIAC_YEARS[(((JE + 1) % 12) + 10) % 12],
years: {
JE,
CE,
BE,
},
length: len,
monthLength: lengthOfYear[m],
month: { name: yearMonths[m], index: m },
months: yearMonths,
}
}