forked from yuangu/sxtwl_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday.cpp
More file actions
320 lines (283 loc) · 5.67 KB
/
day.cpp
File metadata and controls
320 lines (283 loc) · 5.67 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "day.h"
#include "eph.h"
namespace sxtwl
{
GZ getShiGz(uint8_t dayTg, uint8_t hour, bool isZaoWanZiShi = true);
};
void Day::checkSSQ()
{
if (!SSQPtr.ZQ.size() || this->d0 < SSQPtr.ZQ[0] || this->d0 >= SSQPtr.ZQ[24])
{
SSQPtr.calcY(this->d0);
}
}
/**
* 确定已经计算过阴历信息
*/
void Day::checkLunarData()
{
// 已经计算过了
if (this->Ldn != 0)
{
return;
}
this->checkSSQ();
int mk = int2((this->d0 - SSQPtr.HS[0]) / 30);
if (mk < 13 && SSQPtr.HS[mk + 1] <= this->d0)
{
mk++; //农历所在月的序数
}
//if (this.d0 == SSQPtr.HS[mk]) { //月的信息
this->Lmc = SSQPtr.ym[mk]; //月名称
this->Ldn = SSQPtr.dx[mk]; //月大小
this->Lleap = (SSQPtr.leap != 0 && SSQPtr.leap == mk); //闰状况
//}
// 阴历所处的日
this->Ldi = this->d0 - SSQPtr.HS[mk];
}
void Day::checkSolarData()
{
if (this->m != 0)
{
return;
}
Time t = JD::JD2DD(this->d0 + J2000);
this->y = t.Y;
this->d = t.D;
this->m = t.M;
}
/**
* 计算节气数据
*/
void Day::checkJQData()
{
if (this->qk != -2)
{
return;
}
this->qk = -1;
this->getJieQiJD();
//this->checkSSQ();
//int qk = int2((this->d0 - SSQPtr.ZQ[0] - 7) / 15.2184);
//////节气的取值范围是0-23
//if (qk < 23 && this->d0 >= SSQPtr.ZQ[qk + 1])
//{
// qk++;
//}
//this->qk = -1;
//if (this->d0 == SSQPtr.ZQ[qk])
//{
// this->qk = qk;
//}
}
Day *Day::after(int day)
{
return new Day(this->d0 + day);
}
Day *Day::before(int day)
{
return new Day(this->d0 - day);
}
/**
* 获取阴历日期
*/
int Day::getLunarDay()
{
this->checkLunarData();
return this->Ldi + 1;
}
/**
* 获取阴历月
*/
uint8_t Day::getLunarMonth()
{
this->checkLunarData();
static const int yueIndex[12] = { 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
return yueIndex[this->Lmc];
}
int Day::getLunarYear(bool chineseNewYearBoundary)
{
// 以立春为界
if (chineseNewYearBoundary == false)
{
if (this->Lyear == 0)
{
this->checkSSQ();
long double D = SSQPtr.ZQ[3] + (this->d0 < SSQPtr.ZQ[3] ? -365 : 0) + 365.25 * 16 - 35; //以立春为界定纪年
this->Lyear = int2(D / 365.2422 + 0.5);
}
return this->Lyear + 1984;
}
// 以春节为界
if (this->Lyear0 == 0)
{
this->checkSSQ();
int D = SSQPtr.HS[2]; //一般第3个月为春节
for (int j = 0; j < 14; j++)
{ //找春节
if (SSQPtr.ym[j] != 2 || SSQPtr.leap == j && j)
continue;
D = SSQPtr.HS[j];
if (this->d0 < D)
{
D -= 365;
break;
} //无需再找下一个正月
}
D = D + 5810; //计算该年春节与1984年平均春节(立春附近)相差天数估计
this->Lyear0 = int2(D / 365.2422 + 0.5);
}
return this->Lyear0 + 1984;
}
GZ Day::getYearGZ(bool chineseNewYearBoundary)
{
//以春节为界
if (chineseNewYearBoundary)
{
if (this->Lyear3 == NULL)
{
int year = this->getLunarYear(chineseNewYearBoundary) - 1984;
int D = year + 12000;
this->Lyear3 = new GZ(D % 10, D % 12);
}
return *(this->Lyear3);
}
// 以立春为界
if (this->Lyear2 == NULL)
{
int year = this->getLunarYear(false) - 1984;
int D = year + 12000;
this->Lyear2 = new GZ(D % 10, D % 12);
}
return *(this->Lyear2);
}
GZ Day::getMonthGZ()
{
if (this->Lmonth2 == NULL)
{
this->checkSSQ();
int mk = int2((this->d0 - SSQPtr.ZQ[0]) / 30.43685);
//相对大雪的月数计算,mk的取值范围0-12
if (mk < 12 && this->d0 >= SSQPtr.ZQ[2 * mk + 1])
mk++;
//相对于1998年12月7(大雪)的月数,900000为正数基数
int D = mk + int2((SSQPtr.ZQ[12] + 390) / 365.2422) * 12 + 900000;
this->Lmonth2 = new GZ(D % 10, D % 12);
}
return *(this->Lmonth2);
}
GZ Day::getDayGZ()
{
if (this->Lday2 == NULL)
{
int D = this->d0 - 6 + 9000000;
this->Lday2 = new GZ(D % 10, D % 12);
}
return *(this->Lday2);
}
GZ Day::getHourGZ(uint8_t hour, bool isZaoWanZiShi)
{
GZ dayGZ = this->getDayGZ();
return sxtwl::getShiGz(dayGZ.tg, hour, isZaoWanZiShi);
}
bool Day::isLunarLeap()
{
this->checkLunarData();
return this->Lleap;
}
int Day::getSolarYear()
{
this->checkSolarData();
return this->y;
}
uint8_t Day::getSolarMonth()
{
this->checkSolarData();
return this->m;
}
int Day::getSolarDay()
{
this->checkSolarData();
return this->d;
}
uint8_t Day::getWeek()
{
if (this->week == 0xFF)
{
this->week = (this->d0 + J2000 + 1 + 7000000) % 7;
}
return this->week;
}
// 处于该月的第几周
uint8_t Day::getWeekIndex()
{
int i = (this->getSolarDay() - 1) % 7;
int w0 = 0;
if (this->getWeek() >= i)
{
w0 = this->getWeek() - i;
}
else
{
w0 = this->getWeek() + 7 - i;
}
return int2((w0 + this->getSolarDay() - 1) / 7) + 1;
}
//是否有节气
bool Day::hasJieQi()
{
this->checkJQData();
return this->qk != -1;
}
// 获取节气
uint8_t Day::getJieQi()
{
this->checkJQData();
return this->qk;
}
double Day::getJieQiJD()
{
if (this->jqjd != 0)
{
return this->jqjd + J2000;
}
long double d, xn, jd2 = this->d0 + dt_T(this->d0) - 8 / 24;
long double w = XL::S_aLon(jd2 / 36525, 3);
w = int2((w - 0.13) / pi2 * 24) * pi2 / 24;
int D = 0;
do
{
d = qi_accurate(w);
D = int2(d + 0.5);
// 计算出的节令值
xn = int2(w / pi2 * 24 + 24000006.01) % 24;
w += pi2 / 24;
if (D > this->d0)
break;
if (D < this->d0)
continue;
if (D == this->d0)
{
this->jqjd = d;
this->qk = xn;
break;
}
} while (D + 12 < this->d0);
return this->jqjd + J2000;
}
// 获取星座
uint8_t Day::getConstellation()
{
if (this->XiZ == 0xFF)
{
this->checkSSQ();
int mk = int2((this->d0 - SSQPtr.ZQ[0] - 15) / 30.43685);
//星座所在月的序数,(如果j=13,ob.d0不会超过第14号中气)
if (mk < 11 && this->d0 >= SSQPtr.ZQ[2 * mk + 2])
{
mk++;
}
this->XiZ = (mk + 12) % 12;
}
return this->XiZ;
}