forked from ykdsea/SimpleLunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchinesedate.h
591 lines (520 loc) · 25.7 KB
/
chinesedate.h
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* ChineseDate.h
* @Author Tu Yongce <yongce (at) 126 (dot) com>
* @Created 2008-12-13
* @Modified 2008-12-13
* @Version 0.1
*/
//#include "ChineseCalendarDB.h"
#include <iostream.h>
#if 0
#ifndef CHINESE_DATE_H_INCLUDED
#define CHINESE_DATE_H_INCLUDED
#ifdef _MSC_VER
#pragma once
#endif
#include <cassert>
#include "ChineseCalendarDB.h"
class SolarDate; // 前置声明
/*
* 农历日期类
* @note: 只能表示农历1901年1月1日到农历2050年12月30日期间的日期
*/
class ChineseDate
{
public:
int m_year; // 1901~2050
int m_month; // 1~13
int m_day; // 1~[29|30]
bool m_bLeap;
public:
ChineseDate();
ChineseDate(int year, int month, int day,bool bleap=false);
//!! 使用编译器生成的拷贝构造函数、拷贝赋值操作符和析构函数
//int GetYear() const;
//int GetMonth() const;
//int GetDay() const;
// 判断是否为有效日期
bool IsValidDate() const;
// 判断是否在指定日期之前
bool IsPrior(const ChineseDate &ref) const;
// 判断是否为同一日期
friend bool operator== (const ChineseDate &lhs, const ChineseDate &rhs);
friend bool operator!= (const ChineseDate &lhs, const ChineseDate &rhs);
// 计算当前日期是该年的第几天
int YearDay() const;
// 从该年的某天计算日期(如果无对应日期,则操作失败并返回false;否则返回true)
bool FromYearDay(int days);
// 计算和指定日期相差的天数
// 如果this->IsPrior(ref)为真,则返回负数;否则返回正数
int Diff(const ChineseDate &ref) const;
// 向前或者向后调整指定天数(有效日期范围:农历1901年1月1日到农历2050年12月30日)
// 如果days大于0,则向前调整(未来);否则向后调整(过去)
// 如果调整成功,则返回true;否则返回false(此时*this在调用前后不会发生改变)
bool AdjustDays(int days);
// 转换为阳历
SolarDate ToSolarDate() const;
private:
// 计算*this和ref之间相差的天数,要求!IsPrior(ref)为真
int DoDiff(const ChineseDate &ref) const;
// 向前(未来)调整指定天数(注意:不能调整到农历2050年12月30日之后)
// 如果调整成功,则返回true;否则返回false(此时*this在调用前后不会发生改变)
bool DoAdjustForward(int days);
// 向后(过去)调整指定天数(注意:不能调整到农历1901年1月1日之前)
// 如果调整成功,则返回true;否则返回false(此时*this在调用前后不会发生改变)
bool DoAdjustBackward(int days);
};
inline ChineseDate::ChineseDate(): m_year(1901), m_month(1), m_day(1)
{
m_bLeap = false;
}
inline ChineseDate::ChineseDate(int year, int month, int day,bool bleap): m_year(year), m_month(month), m_day(day)
{
//assert(IsValidDate()); // 当ChineseDate对象内部数据变化时确保仍是正确的日期
m_bLeap = bleap;
}
#if 0
inline int ChineseDate::GetYear() const
{
return m_year;
}
inline int ChineseDate::GetMonth() const
{
return m_month;
}
inline int ChineseDate::GetDay() const
{
return m_day;
}
#endif
// 判断是否在指定日期之前
inline bool ChineseDate::IsPrior(const ChineseDate &ref) const
{
return m_year < ref.m_year ||
m_year == ref.m_year && (m_month < ref.m_month || m_month == ref.m_month && m_day < ref.m_day);
}
// 判断是否为同一日期
inline bool operator== (const ChineseDate &lhs, const ChineseDate &rhs)
{
return lhs.m_year == rhs.m_year && lhs.m_month == rhs.m_month && lhs.m_day == rhs.m_day;
}
inline bool operator!= (const ChineseDate &lhs, const ChineseDate &rhs)
{
return !(lhs == rhs);
}
// 计算和指定日期相差的天数
// 如果this->IsPrior(ref)为真,则返回负数;否则返回正数
inline int ChineseDate::Diff(const ChineseDate &ref) const
{
return this->IsPrior(ref) ? -ref.DoDiff(*this) : this->DoDiff(ref);
}
// 向前或者向后调整指定天数(注意:不能调整到公元1年1月1日之前)
// 如果days大于0,则向前调整(未来);否则向后调整(过去)
// 如果调整成功,则返回true;否则返回false(此时*this在调用前后不会发生改变)
inline bool ChineseDate::AdjustDays(int days)
{
if (days > 0)
return DoAdjustForward(days);
else if (days < 0)
return DoAdjustBackward(-days);
assert(IsValidDate()); // 当ChineseDate对象内部数据变化时确保仍是正确的日期
return true;
}
#endif //CHINESE_DATE_H_INCLUDED
#endif
static bool CheckLeapYear(int year)
{
return (!(year % 4) && (year % 100)) || !(year % 400);
}
// 平年中每一个月之前所有月的天数
const int YearDays[12] = {
0, 31, 59 , 90, 120, 151, 181, 212, 243, 273, 304, 334
};
static int CalMonthDay(int year, int month)
{
bool leap_flag = CheckLeapYear(year);
int days = YearDays[month - 1] ;
if (month > 2 && CheckLeapYear(year))
++days;
return days;
}
#define CHINESEDATE_MIN (1901)
#define CHINESEDATE_MAX (2050)
#define CHINESEDATE_NUM (150)
unsigned char ChinaYearLeapMonth[CHINESEDATE_NUM] = {
0x00, 0x00, 0x05, 0x00, 0x00, 0x04, 0x00, 0x00, 0x02, 0x00, //1910
0x06, 0x00, 0x00, 0x05, 0x00, 0x00, 0x02, 0x00, 0x07, 0x00, //1920
0x00, 0x05, 0x00, 0x00, 0x04, 0x00, 0x00, 0x02, 0x00, 0x06, //1930
0x00, 0x00, 0x05, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, //1940
0x06, 0x00, 0x00, 0x04, 0x00, 0x00, 0x02, 0x00, 0x07, 0x00, //1950
0x00, 0x05, 0x00, 0x00, 0x03, 0x00, 0x08, 0x00, 0x00, 0x06, //1960
0x00, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, //1970
0x05, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, //1980
0x00, 0x04, 0x00, 0x0A, 0x00, 0x00, 0x06, 0x00, 0x00, 0x05, //1990
0x00, 0x00, 0x03, 0x00, 0x08, 0x00, 0x00, 0x05, 0x00, 0x00, //2000
0x04, 0x00, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x05, 0x00, //2010
0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x06, 0x00, 0x00, 0x04, //2020
0x00, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x05, 0x00, 0x00, //2030
0x03, 0x00, 0x0B, 0x00, 0x00, 0x06, 0x00, 0x00, 0x05, 0x00, //2040
0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x05, 0x00, 0x00, 0x03, //2050
};
unsigned short ChinaYearMonthDay[CHINESEDATE_NUM] = {
0x0752, 0x0EA5, 0x164A, 0x064B, 0x0A9B, 0x1556, 0x056A, 0x0B59, 0x1752, 0x0752, //1910
0x1B25, 0x0B25, 0x0A4B, 0x12AB, 0x0AAD, 0x056A, 0x0B69, 0x0DA9, 0x1D92, 0x0D92, //1920
0x0D25, 0x1A4D, 0x0A56, 0x02B6, 0x15B5, 0x06D4, 0x0EA9, 0x1E92, 0x0E92, 0x0D26, //1930
0x052B, 0x0A57, 0x12B6, 0x0B5A, 0x06D4, 0x0EC9, 0x0749, 0x1693, 0x0A93, 0x052B, //1940
0x0A5B, 0x0AAD, 0x056A, 0x1B55, 0x0BA4, 0x0B49, 0x1A93, 0x0A95, 0x152D, 0x0536, //1950
0x0AAD, 0x15AA, 0x05B2, 0x0DA5, 0x1D4A, 0x0D4A, 0x0A95, 0x0A97, 0x0556, 0x0AB5, //1960
0x0AD5, 0x06D2, 0x0EA5, 0x0EA5, 0x064A, 0x0C97, 0x0A9B, 0x155A, 0x056A, 0x0B69, //1970
0x1752, 0x0B52, 0x0B25, 0x164B, 0x0A4B, 0x14AB, 0x02AD, 0x056D, 0x0B69, 0x0DA9, //1980
0x0D92, 0x1D25, 0x0D25, 0x1A4D, 0x0A56, 0x02B6, 0x05B5, 0x06D5, 0x0EC9, 0x1E92, //1990
0x0E92, 0x0D26, 0x0A56, 0x0A57, 0x14D6, 0x035A, 0x06D5, 0x16C9, 0x0749, 0x0693, //2000
0x152B, 0x052B, 0x0A5B, 0x155A, 0x056A, 0x1B55, 0x0BA4, 0x0B49, 0x1A93, 0x0A95, //2010
0x052D, 0x0AAD, 0x0AAD, 0x15AA, 0x05D2, 0x0DA5, 0x1D4A, 0x0D4A, 0x0C95, 0x152E, //2020
0x0556, 0x0AB5, 0x15B2, 0x06D2, 0x0EA9, 0x0725, 0x064B, 0x0C97, 0x0CAB, 0x055A, //2030
0x0AD6, 0x0B69, 0x1752, 0x0B52, 0x0B25, 0x1A4B, 0x0A4B, 0x04AB, 0x055B, 0x05AD, //2040
0x0B6A, 0x1B52, 0x0D92, 0x1D25, 0x0D25, 0x0A55, 0x14AD, 0x04B6, 0x05B5, 0x0DAA, //2050
};
// 二十四节气数据(注:这里是公历日期)
// 1901~2050期间的二十四节气的公历日期满足如下规律:
// 1月 小寒(5~7) 大寒(19~21)
// 2月 立春(3~5) 雨水(18~20)
// 3月 惊蛰(5~7) 春分(20~22)
// 4月 清明(4~6) 谷雨(19~21)
// 5月 立夏(5~7) 小满(20~22)
// 6月 芒种(5~7) 夏至(20~22)
// 7月 小暑(6~8) 大暑(22~24)
// 8月 立秋(7~9) 处暑(22~24)
// 9月 白露(7~9) 秋分(22~24)
// 10月 寒露(7~9) 霜降(23~24)
// 11月 立冬(7~8) 小雪(21~23)
// 12月 大雪(6~8) 冬至(21~23)
// 可以看出,如果把每月的第二个节气的日期减去15将得到与第一个节气相近的日期,且都在3~9范围内
// 因此,可以使用一个字节来保存每个月两个节气的日期
// 另外,也可以看出,每个节气都在3天的波动范围内,因此还可以近一步把四个节气压缩在一个字节内
// 但那样做使用起来太复杂,因此这里还是把两个节气保存在一个字节中
//
// 数据格式:每个字节的高四位存放每个月的第一个节气的日期,低四位存放该月的第二个
// 节气日期减去15之后的日期
unsigned char ChinaTermSolarDate[CHINESEDATE_NUM][12] = {
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1901
0x66, 0x54, 0x66, 0x66, 0x67, 0x77, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1902
0x66, 0x55, 0x77, 0x66, 0x77, 0x77, 0x89, 0x99, 0x99, 0x99, 0x88, 0x88, //1903
0x76, 0x55, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x88, 0x77, //1904
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1905
0x66, 0x54, 0x66, 0x66, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1906
0x66, 0x55, 0x77, 0x66, 0x77, 0x77, 0x89, 0x99, 0x99, 0x99, 0x88, 0x88, //1907
0x76, 0x55, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1908
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1909
0x66, 0x54, 0x66, 0x66, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1910
0x66, 0x55, 0x77, 0x66, 0x77, 0x77, 0x89, 0x99, 0x99, 0x99, 0x88, 0x88, //1911
0x76, 0x55, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1912
0x65, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1913
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1914
0x66, 0x55, 0x67, 0x66, 0x67, 0x77, 0x89, 0x89, 0x99, 0x99, 0x88, 0x88, //1915
0x66, 0x55, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1916
0x65, 0x44, 0x66, 0x56, 0x66, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x77, //1917
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x87, //1918
0x66, 0x55, 0x67, 0x66, 0x67, 0x77, 0x89, 0x89, 0x99, 0x99, 0x88, 0x88, //1919
0x66, 0x55, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1920
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x77, //1921
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x87, //1922
0x66, 0x54, 0x66, 0x66, 0x67, 0x77, 0x89, 0x89, 0x99, 0x99, 0x88, 0x88, //1923
0x66, 0x55, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1924
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x77, //1925
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1926
0x66, 0x54, 0x66, 0x66, 0x67, 0x77, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1927
0x66, 0x55, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1928
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1929
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1930
0x66, 0x54, 0x66, 0x66, 0x67, 0x77, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1931
0x66, 0x55, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1932
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1933
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1934
0x66, 0x54, 0x66, 0x66, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1935
0x66, 0x55, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1936
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1937
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1938
0x66, 0x54, 0x66, 0x66, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1939
0x66, 0x55, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1940
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1941
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1942
0x66, 0x54, 0x66, 0x66, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1943
0x66, 0x55, 0x66, 0x55, 0x56, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1944
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1945
0x65, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x87, //1946
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1947
0x66, 0x55, 0x56, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //1948
0x55, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x89, 0x88, 0x89, 0x87, 0x77, //1949
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x87, //1950
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x89, 0x89, 0x89, 0x99, 0x88, 0x88, //1951
0x66, 0x55, 0x56, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //1952
0x55, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1953
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x88, 0x89, 0x88, 0x98, 0x88, 0x77, //1954
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1955
0x66, 0x55, 0x55, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //1956
0x55, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1957
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1958
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1959
0x66, 0x54, 0x55, 0x55, 0x56, 0x66, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1960
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1961
0x66, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1962
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1963
0x66, 0x54, 0x55, 0x55, 0x56, 0x66, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1964
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1965
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1966
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1967
0x66, 0x54, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1968
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1969
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1970
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x89, 0x99, 0x88, 0x87, //1971
0x66, 0x54, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1972
0x55, 0x45, 0x66, 0x55, 0x56, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1973
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1974
0x66, 0x44, 0x66, 0x56, 0x67, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x87, //1975
0x66, 0x54, 0x55, 0x45, 0x56, 0x56, 0x78, 0x79, 0x78, 0x88, 0x77, 0x77, //1976
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //1977
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x88, 0x77, //1978
0x66, 0x44, 0x66, 0x56, 0x66, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x87, //1979
0x66, 0x54, 0x55, 0x45, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1980
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x87, 0x77, //1981
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1982
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x88, 0x89, 0x88, 0x99, 0x88, 0x87, //1983
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x77, //1984
0x55, 0x44, 0x56, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //1985
0x55, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1986
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x89, 0x88, 0x99, 0x88, 0x77, //1987
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //1988
0x55, 0x44, 0x55, 0x55, 0x56, 0x66, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1989
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //1990
0x65, 0x44, 0x66, 0x55, 0x76, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1991
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //1992
0x55, 0x43, 0x55, 0x55, 0x56, 0x66, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1993
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1994
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x86, 0x88, 0x99, 0x88, 0x77, //1995
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //1996
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //1997
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //1998
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //1999
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //2000
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2001
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //2002
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //2003
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //2004
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2005
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2006
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x99, 0x88, 0x77, //2007
0x66, 0x44, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x77, 0x88, 0x77, 0x76, //2008
0x55, 0x43, 0x55, 0x45, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2009
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2010
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x88, 0x77, //2011
0x66, 0x44, 0x55, 0x45, 0x55, 0x56, 0x77, 0x78, 0x77, 0x88, 0x77, 0x76, //2012
0x55, 0x43, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x77, //2013
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2014
0x65, 0x44, 0x66, 0x55, 0x66, 0x67, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //2015
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x77, 0x78, 0x77, 0x88, 0x77, 0x76, //2016
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x77, //2017
0x55, 0x44, 0x56, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2018
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //2019
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x76, //2020
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //2021
0x55, 0x44, 0x55, 0x55, 0x56, 0x66, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2022
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x89, 0x87, 0x77, //2023
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x66, //2024
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //2025
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2026
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //2027
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x66, //2028
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //2029
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2030
0x55, 0x44, 0x66, 0x55, 0x66, 0x66, 0x78, 0x88, 0x88, 0x88, 0x77, 0x77, //2031
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x66, //2032
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x76, //2033
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x88, 0x78, 0x88, 0x77, 0x77, //2034
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2035
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x66, //2036
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x76, //2037
0x55, 0x43, 0x55, 0x55, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2038
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2039
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x66, //2040
0x55, 0x33, 0x55, 0x45, 0x55, 0x56, 0x77, 0x78, 0x77, 0x88, 0x77, 0x76, //2041
0x55, 0x43, 0x55, 0x45, 0x56, 0x56, 0x78, 0x78, 0x78, 0x88, 0x77, 0x77, //2042
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2043
0x65, 0x44, 0x55, 0x44, 0x55, 0x56, 0x67, 0x77, 0x77, 0x78, 0x77, 0x66, //2044
0x55, 0x33, 0x55, 0x44, 0x55, 0x56, 0x77, 0x78, 0x77, 0x88, 0x77, 0x76, //2045
0x55, 0x43, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x78, 0x88, 0x77, 0x77, //2046
0x55, 0x44, 0x66, 0x55, 0x56, 0x66, 0x78, 0x78, 0x88, 0x88, 0x77, 0x77, //2047
0x65, 0x44, 0x55, 0x44, 0x55, 0x55, 0x67, 0x77, 0x77, 0x78, 0x76, 0x66, //2048
0x54, 0x33, 0x55, 0x55, 0x55, 0x56, 0x67, 0x77, 0x77, 0x88, 0x77, 0x76, //2049
0x55, 0x33, 0x55, 0x45, 0x56, 0x56, 0x77, 0x78, 0x88, 0x88, 0x77, 0x77, //2050
};
//农历节日日期,高字节是月份,低字节是日期
unsigned short ChineseFestivalDate[] =
{
0x0101,//春节
0x010F,//元宵
0x0309,//清明
0x0505,//端午
0x080F,//中秋
0x0909,//重阳
0x0C1E,//除夕
};
//===========================================
//the day used as the start point to caluate
#define CALUATE_START_YEAR 2009
#define CALUATE_START_MONTH 1
#define CALUATE_START_DAY 26
#define CALUATE_START_CHINESEYEAR 2009
#define CALUATE_START_CHINESEMONTH 1
#define CALUATE_START_CHINESEDAY 1
struct SolarDate
{
int m_iyear;
int m_imonth;
int m_iday;
};
static int CompareSolarDate(SolarDate hdate1,SolarDate hdate2)
{
int diff = hdate1.m_iyear - hdate2.m_iyear;
if(diff == 0)
diff = hdate1.m_imonth - hdate2.m_imonth;
if(diff == 0)
diff = hdate1.m_iday - hdate2.m_iday;
return diff;
}
static int GetSolarLeapYears(int year)
{
if (year == 0)
return 0;
int y = year - 1;
return y / 4 - y / 100 + y / 400;
}
static int CalSolarDaysDiff(SolarDate& hdate)
{
SolarDate href = {CALUATE_START_YEAR,CALUATE_START_MONTH,CALUATE_START_DAY};
int daysdiff = 0;
int yeardiff = hdate.m_iyear - href.m_iyear;
daysdiff = yeardiff * 365;
daysdiff += GetSolarLeapYears(hdate.m_iyear);
daysdiff -= GetSolarLeapYears(href.m_iyear);
daysdiff += CalMonthDay(hdate.m_iyear,hdate.m_imonth);
daysdiff += (hdate.m_iday-1);
daysdiff -= CalMonthDay(href.m_iyear,href.m_imonth);
daysdiff -= (href.m_iday-1);
return daysdiff;
}
static int IsValidSolarDate(SolarDate& hdate)
{
if( (hdate.m_iyear < CHINESEDATE_MIN) || (hdate.m_iyear > CHINESEDATE_MAX) )
return 0;
return 1;
}
struct ChineseDate
{
int m_iyear;
int m_imonth;
int m_iday;
int m_bleap;//is this month the leap month
};
static int GetChineseYearDays(int year)
{
int num = ChinaYearMonthDay[year - CHINESEDATE_MIN];
num = ((num >> 1) & 0x5555) + (num & 0x5555);
num = ((num >> 2) & 0x3333) + (num & 0x3333);
num = ((num >> 4) & 0x0F0F) + (num & 0x0F0F);
num = ((num >> 8) & 0x00FF) + (num & 0x00FF);
int monthnum = 12 + (ChinaYearLeapMonth[year - CHINESEDATE_MIN] ? 1 : 0);
return monthnum * 29 + num;
}
static int GetChineseMonthDays(int year, int month)
{
return (ChinaYearMonthDay[year - CHINESEDATE_MIN] & (1 << (month - 1))) ? 30 : 29;
}
static int GetChineseLeapMonth(int year)
{
return ChinaYearLeapMonth[year-CHINESEDATE_MIN];
}
static int CalChineseDate(ChineseDate& hchdate, int daysoff)
{
ChineseDate href = { CALUATE_START_CHINESEYEAR, CALUATE_START_CHINESEMONTH, CALUATE_START_CHINESEDAY};
int movesymbol = (daysoff < 0) ? -1 : 1;
int yeardays = 0, monthdays, days;
int year = href.m_iyear, month = href.m_imonth;
hchdate.m_bleap = false;
if(movesymbol < 0)
year += movesymbol;
yeardays = GetChineseYearDays(year);
while( (daysoff*movesymbol) > yeardays )
{
daysoff -= yeardays*movesymbol;
year += movesymbol;
yeardays = GetChineseYearDays(year);
}
hchdate.m_iyear = year;
month = (movesymbol < 0) ? (12 + (GetChineseLeapMonth(year) > 0 ? 1 : 0)) : 1;
monthdays = GetChineseMonthDays(year,month);
while( (daysoff*movesymbol) > monthdays)
{
daysoff -= monthdays*movesymbol;
month += movesymbol;
monthdays = GetChineseMonthDays(year,month);
}
hchdate.m_imonth = month;
if(movesymbol > 0)
hchdate.m_iday = 1 + daysoff;
else
hchdate.m_iday = 1 + GetChineseMonthDays(year,month) + daysoff ;
if( (GetChineseLeapMonth(hchdate.m_iyear) > 0) && (hchdate.m_imonth > GetChineseLeapMonth(hchdate.m_iyear)) )
{
hchdate.m_imonth--;
hchdate.m_bleap = true;
}
return 1;
}
static int ConvertToChineseDate(SolarDate& hsodate, ChineseDate& hchdate)
{
int daysdiff = 0;
if(IsValidSolarDate(hsodate) == 0)
return 0;
daysdiff = CalSolarDaysDiff(hsodate);
return CalChineseDate(hchdate,daysdiff);
}
static int GetSolarTerm(SolarDate* psodate, int* startpos)
{
int solartermnum = 2;
*startpos = (psodate->m_imonth-1)*2;
return solartermnum;
}
static int GetSolarTermDate(SolarDate* psodate, int pos)
{
unsigned char SolarDate = 0;
if( (pos < 0) || (pos > 24) )
return 0;
psodate->m_imonth = (pos + 1)/2;
SolarDate = ChinaTermSolarDate[psodate->m_iyear-CHINESEDATE_MIN][psodate->m_imonth-1];
if(pos%2 == 1)
{
psodate->m_iday = SolarDate>>4;
}
else
psodate->m_iday = (SolarDate&0x0F) + 15;
return 1;
}
static int GetChineseFestival(ChineseDate* pchdate, int* startpos)
{
int chinesefestivalnum = 0;
int festivaeltotal = sizeof(ChineseFestivalDate)/sizeof(short);
int monthval = pchdate->m_imonth << 8;
int i = 0;
*startpos = 0;
for(; i < festivaeltotal; i++)
{
if(ChineseFestivalDate[i] & monthval == monthval)
{
if(*startpos == 0)
*startpos = i+1;
chinesefestivalnum++;
}
}
return chinesefestivalnum;
}