forked from 6tail/lunar-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunar.js
8494 lines (8448 loc) · 425 KB
/
lunar.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;(function(root,factory){
if (typeof define==='function'&&define.amd){
define(factory);
}else if(typeof module!='undefined'&&module.exports){
module.exports = factory();
}else{
var o = factory();
for(var i in o){
root[i] = o[i];
}
}
})(this,function(){
var Solar = (function(){
var _fromDate = function(date){
return _fromYmdHms(date.getFullYear(),date.getMonth()+1,date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds());
};
var _fromJulianDay = function(julianDay){
var d = Math.floor(julianDay + 0.5);
var f = julianDay + 0.5 - d;
var c;
if (d >= 2299161) {
c = Math.floor((d - 1867216.25) / 36524.25);
d += 1 + c - Math.floor(c / 4);
}
d += 1524;
var year = Math.floor((d - 122.1) / 365.25);
d -= Math.floor(365.25 * year);
var month = Math.floor(d / 30.601);
d -= Math.floor(30.601 * month);
var day = d;
if (month > 13) {
month -= 13;
year -= 4715;
} else {
month -= 1;
year -= 4716;
}
f *= 24;
var hour = Math.floor(f);
f -= hour;
f *= 60;
var minute = Math.floor(f);
f -= minute;
f *= 60;
var second = Math.round(f);
if(second>59){
second-=60;
minute++;
}
if(minute>59){
minute-=60;
hour++;
}
if(hour>23){
hour-=24;
day+=1;
}
return _fromYmdHms(year,month,day,hour,minute,second);
};
var _fromYmdHms = function(y,m,d,hour,minute,second){
var oy = y;
var om = m;
var od = d;
var oh = hour;
var oi = minute;
var os = second;
y *= 1;
if(isNaN(y)){
throw new Error('wrong solar year '+oy);
}
m *= 1;
if(isNaN(m)){
throw new Error('wrong solar month '+om);
}
d *= 1;
if(isNaN(d)){
throw new Error('wrong solar day '+od);
}
hour *= 1;
if(isNaN(hour)){
throw new Error('wrong hour '+oh);
}
minute *= 1;
if(isNaN(minute)){
throw new Error('wrong minute '+oi);
}
second *= 1;
if(isNaN(second)){
throw new Error('wrong second '+os);
}
if(1582===y && 10===m){
if(d>4&&d<15){
throw new Error('wrong solar year '+y+' month '+m+' day '+d);
}
}
if(m<1||m>12){
throw new Error('wrong month ' + m);
}
if(d<1||d>31){
throw new Error('wrong day ' + d);
}
if(hour<0||hour>23){
throw new Error('wrong hour '+hour);
}
if(minute<0||minute>59){
throw new Error('wrong minute '+minute);
}
if(second<0||second>59){
throw new Error('wrong second '+second);
}
return {
_p:{
year:y,
month:m,
day:d,
hour:hour,
minute:minute,
second:second
},
subtract:function(solar){
return SolarUtil.getDaysBetween(solar.getYear(), solar.getMonth(), solar.getDay(), this._p.year, this._p.month, this._p.day);
},
subtractMinute:function(solar){
var days = this.subtract(solar);
var cm = this._p.hour * 60 + this._p.minute;
var sm = solar.getHour() * 60 + solar.getMinute();
var m = cm - sm;
if (m < 0) {
m += 1440;
days--;
}
m += days * 1440;
return m;
},
isAfter: function(solar) {
if (this._p.year > solar.getYear()) {
return true;
}
if (this._p.year < solar.getYear()) {
return false;
}
if (this._p.month > solar.getMonth()) {
return true;
}
if (this._p.month < solar.getMonth()) {
return false;
}
if (this._p.day > solar.getDay()) {
return true;
}
if (this._p.day < solar.getDay()) {
return false;
}
if (this._p.hour > solar.getHour()) {
return true;
}
if (this._p.hour < solar.getHour()) {
return false;
}
if (this._p.minute > solar.getMinute()) {
return true;
}
if (this._p.minute < solar.getMinute()) {
return false;
}
return this._p.second > solar.getSecond();
},
isBefore: function(solar) {
if (this._p.year > solar.getYear()) {
return false;
}
if (this._p.year < solar.getYear()) {
return true;
}
if (this._p.month > solar.getMonth()) {
return false;
}
if (this._p.month < solar.getMonth()) {
return true;
}
if (this._p.day > solar.getDay()) {
return false;
}
if (this._p.day < solar.getDay()) {
return true;
}
if (this._p.hour > solar.getHour()) {
return false;
}
if (this._p.hour < solar.getHour()) {
return true;
}
if (this._p.minute > solar.getMinute()) {
return false;
}
if (this._p.minute < solar.getMinute()) {
return true;
}
return this._p.second < solar.getSecond();
},
getYear:function(){
return this._p.year;
},
getMonth:function(){
return this._p.month;
},
getDay:function(){
return this._p.day;
},
getHour:function(){
return this._p.hour;
},
getMinute:function(){
return this._p.minute;
},
getSecond:function(){
return this._p.second;
},
getWeek:function(){
return (Math.floor(this.getJulianDay() + 0.5) + 7000001) % 7;
},
getWeekInChinese:function(){
return SolarUtil.WEEK[this.getWeek()];
},
/**
* 获取当天的阳历周
* @param start 星期几作为一周的开始,1234560分别代表星期一至星期天
*/
getSolarWeek:function(start){
return SolarWeek.fromYmd(this._p.year, this._p.month, this._p.day, start);
},
isLeapYear:function(){
return SolarUtil.isLeapYear(this._p.year);
},
getFestivals:function(){
var l = [];
var f = SolarUtil.FESTIVAL[this._p.month+'-'+this._p.day];
if(f){
l.push(f);
}
var weeks = Math.ceil(this._p.day/7);
var week = this.getWeek();
f = SolarUtil.WEEK_FESTIVAL[this._p.month+'-'+weeks+'-'+week];
if(f){
l.push(f);
}
if (this._p.day + 7 > SolarUtil.getDaysOfMonth(this._p.year, this._p.month)) {
f = SolarUtil.WEEK_FESTIVAL[this._p.month + '-0-' + week];
if (f) {
l.push(f);
}
}
return l;
},
getOtherFestivals:function(){
var l=[];
var fs=SolarUtil.OTHER_FESTIVAL[this._p.month+'-'+this._p.day];
if(fs){
l=l.concat(fs);
}
return l;
},
getXingzuo:function(){
return this.getXingZuo();
},
getXingZuo:function(){
var index = 11;
var y = this._p.month*100+this._p.day;
if (y >= 321 && y <= 419) {
index = 0;
} else if (y >= 420 && y <= 520) {
index = 1;
} else if (y >= 521 && y <= 621) {
index = 2;
} else if (y >= 622 && y <= 722) {
index = 3;
} else if (y >= 723 && y <= 822) {
index = 4;
} else if (y >= 823 && y <= 922) {
index = 5;
} else if (y >= 923 && y <= 1023) {
index = 6;
} else if (y >= 1024 && y <= 1122) {
index = 7;
} else if (y >= 1123 && y <= 1221) {
index = 8;
} else if (y >= 1222 || y <= 119) {
index = 9;
} else if (y <= 218) {
index = 10;
}
return SolarUtil.XINGZUO[index];
},
toYmd:function(){
var m = this._p.month;
var d = this._p.day;
var y = this._p.year + '';
while (y.length < 4) {
y = '0' + y;
}
return [y,(m<10?'0':'')+m,(d<10?'0':'')+d].join('-');
},
toYmdHms:function(){
return this.toYmd()+' '+[(this._p.hour<10?'0':'')+this._p.hour,(this._p.minute<10?'0':'')+this._p.minute,(this._p.second<10?'0':'')+this._p.second].join(':');
},
toString:function(){
return this.toYmd();
},
toFullString:function(){
var s = this.toYmdHms();
if(this.isLeapYear()){
s += ' 闰年';
}
s += ' 星期'+this.getWeekInChinese();
var festivals = this.getFestivals();
for(var i=0,j=festivals.length;i<j;i++){
s += ' ('+festivals[i]+')';
}
s += ' '+this.getXingZuo()+'座';
return s;
},
nextYear:function(years){
var oy = years;
years *= 1;
if (isNaN(years)) {
throw new Error('wrong years ' + oy);
}
var y = this._p.year + years;
var m = this._p.month;
var d = this._p.day;
if (1582 === y && 10 === m) {
if (d > 4 && d < 15) {
d += 10;
}
} else if (2 === m) {
if (d > 28) {
if (!SolarUtil.isLeapYear(y)) {
d = 28;
}
}
}
return _fromYmdHms(y, m, d, this._p.hour, this._p.minute, this._p.second);
},
nextMonth:function(months){
var om = months;
months *= 1;
if (isNaN(months)) {
throw new Error('wrong months ' + om);
}
var month = SolarMonth.fromYm(this._p.year, this._p.month).next(months);
var y = month.getYear();
var m = month.getMonth();
var d = this._p.day;
if (1582 === y && 10 === m) {
if (d > 4 && d < 15) {
d += 10;
}
} else {
var maxDay = SolarUtil.getDaysOfMonth(y, m);
if (d > maxDay) {
d = maxDay;
}
}
return _fromYmdHms(y, m, d, this._p.hour, this._p.minute, this._p.second);
},
nextDay:function(days){
var od = days;
days *= 1;
if (isNaN(days)) {
throw new Error('wrong days ' + od);
}
var y = this._p.year;
var m = this._p.month;
var d = this._p.day;
if (1582 === y && 10 === m) {
if (d > 4) {
d -= 10
}
}
if (days > 0) {
d += days;
var daysInMonth = SolarUtil.getDaysOfMonth(y, m);
while (d > daysInMonth) {
d -= daysInMonth;
m++;
if (m > 12) {
m = 1;
y++;
}
daysInMonth = SolarUtil.getDaysOfMonth(y, m);
}
} else if (days < 0) {
while (d + days <= 0) {
m--;
if (m < 1) {
m = 12;
y--;
}
d += SolarUtil.getDaysOfMonth(y, m);
}
d += days;
}
if (1582 === y && 10 === m) {
if (d > 4) {
d += 10;
}
}
return _fromYmdHms(y, m, d, this._p.hour, this._p.minute, this._p.second);
},
nextWorkday:function(days){
var od = days;
days *= 1;
if (isNaN(days)) {
throw new Error('wrong days ' + od);
}
var solar = _fromYmdHms(this._p.year, this._p.month, this._p.day, this._p.hour, this._p.minute, this._p.second);
if (days !== 0) {
var rest = Math.abs(days);
var add = days < 1 ? -1 : 1;
while (rest > 0) {
solar = solar.next(add);
var work = true;
var holiday = HolidayUtil.getHoliday(solar.getYear(), solar.getMonth(), solar.getDay());
if (!holiday) {
var week = solar.getWeek();
if (0 === week || 6 === week) {
work = false;
}
} else {
work = holiday.isWork();
}
if (work) {
rest -= 1;
}
}
}
return solar;
},
next:function(days, onlyWorkday){
if (onlyWorkday) {
return this.nextWorkday(days);
}
return this.nextDay(days);
},
nextHour:function(hours){
var oh = hours;
hours *= 1;
if (isNaN(hours)) {
throw new Error('wrong hours ' + oh);
}
var h = this._p.hour + hours;
var n = h < 0 ? -1 : 1;
var hour = Math.abs(h);
var days = Math.floor(hour / 24) * n;
hour = (hour % 24) * n;
if (hour < 0) {
hour += 24;
days--;
}
var solar = this.next(days);
return _fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), hour, solar.getMinute(), solar.getSecond());
},
getLunar:function(){
return Lunar.fromSolar(this);
},
getJulianDay:function(){
var y = this._p.year;
var m = this._p.month;
var d = this._p.day + ((this._p.second / 60 + this._p.minute) / 60 + this._p.hour) / 24;
var n = 0;
var g = false;
if (y * 372 + m * 31 + Math.floor(d) >= 588829) {
g = true;
}
if (m <= 2) {
m += 12;
y--;
}
if (g) {
n = Math.floor(y / 100);
n = 2 - n + Math.floor(n / 4);
}
return Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + d + n - 1524.5;
},
getSalaryRate:function(){
// 元旦节
if (this._p.month === 1 && this._p.day === 1) {
return 3;
}
// 劳动节
if (this._p.month === 5 && this._p.day === 1) {
return 3;
}
// 国庆
if (this._p.month === 10 && this._p.day >= 1 && this._p.day <= 3) {
return 3;
}
var lunar = this.getLunar();
// 春节
if (lunar.getMonth() === 1 && lunar.getDay() >= 1 && lunar.getDay() <= 3) {
return 3;
}
// 端午
if (lunar.getMonth() === 5 && lunar.getDay() === 5) {
return 3;
}
// 中秋
if (lunar.getMonth() === 8 && lunar.getDay() === 15) {
return 3;
}
// 清明
if ('清明' === lunar.getJieQi()) {
return 3;
}
var holiday = HolidayUtil.getHoliday(this._p.year, this._p.month, this._p.day);
if (holiday) {
// 法定假日非上班
if (!holiday.isWork()) {
return 2;
}
} else {
// 周末
var week = this.getWeek();
if (week === 6 || week === 0) {
return 2;
}
}
// 工作日
return 1;
}
};
};
var _fromBaZi=function(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,baseYear){
sect *= 1;
if(isNaN(sect)){
sect = 2;
}
if (1 !== sect) {
sect = 2;
}
baseYear *= 1;
if(isNaN(baseYear)){
baseYear = 1900;
}
var l = [];
// 月地支距寅月的偏移值
var m = LunarUtil.index(monthGanZhi.substring(1), LunarUtil.ZHI, -1) - 2;
if (m < 0) {
m += 12;
}
// 月天干要一致
if (((LunarUtil.index(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 !== LunarUtil.index(monthGanZhi.substring(0,1), LunarUtil.GAN, -1)) {
return l;
}
// 1年的立春是辛酉,序号57
var y = LunarUtil.getJiaZiIndex(yearGanZhi) - 57;
if (y < 0) {
y += 60;
}
y++;
// 节令偏移值
m *= 2;
// 时辰地支转时刻,子时按零点算
var h = LunarUtil.index(timeGanZhi.substring(1), LunarUtil.ZHI, -1) * 2;
var hours = [h];
if (0 === h && 2 === sect) {
hours = [0, 23];
}
var startYear = baseYear - 1;
// 结束年
var endYear = new Date().getFullYear();
while (y <= endYear) {
if (y >= startYear) {
// 立春为寅月的开始
var jieQiLunar = Lunar.fromYmd(y, 1, 1);
var jieQiList = jieQiLunar.getJieQiList();
var jieQiTable = jieQiLunar.getJieQiTable();
// 节令推移,年干支和月干支就都匹配上了
var solarTime = jieQiTable[jieQiList[4 + m]];
if (solarTime.getYear() >= baseYear) {
// 日干支和节令干支的偏移值
var d = LunarUtil.getJiaZiIndex(dayGanZhi) - LunarUtil.getJiaZiIndex(solarTime.getLunar().getDayInGanZhiExact2());
if (d < 0) {
d += 60;
}
if (d > 0) {
// 从节令推移天数
solarTime = solarTime.next(d);
}
for (var i = 0, j = hours.length; i < j; i++) {
var hour = hours[i];
var mi = 0;
var s = 0;
if (d === 0 && hour === solarTime.getHour()) {
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
mi = solarTime.getMinute();
s = solarTime.getSecond();
}
// 验证一下
var solar = Solar.fromYmdHms(solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(), hour, mi, s);
var lunar = solar.getLunar();
var dgz = (2 === sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact() === yearGanZhi && lunar.getMonthInGanZhiExact() === monthGanZhi && dgz === dayGanZhi && lunar.getTimeInGanZhi() === timeGanZhi) {
l.push(solar);
}
}
}
}
y += 60;
}
return l;
};
return {
J2000:2451545,
fromYmd:function(y,m,d){return _fromYmdHms(y,m,d,0,0,0);},
fromYmdHms:function(y,m,d,hour,minute,second){return _fromYmdHms(y,m,d,hour,minute,second);},
fromDate:function(date){return _fromDate(date);},
fromJulianDay:function(julianDay){return _fromJulianDay(julianDay);},
fromBaZi:function(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,baseYear){return _fromBaZi(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,baseYear);}
};
})();
var Lunar = (function(){
var _computeJieQi = function(o,ly) {
o['jieQiList'] = [];
o['jieQi'] = {};
var julianDays = ly.getJieQiJulianDays();
for (var i = 0, j = LunarUtil.JIE_QI_IN_USE.length; i < j; i++) {
var key = LunarUtil.JIE_QI_IN_USE[i];
o['jieQiList'].push(key);
o['jieQi'][key] = Solar.fromJulianDay(julianDays[i]);
}
};
var _computeYear = function(o,solar,year){
//以正月初一开始
var offset = year - 4;
var yearGanIndex = offset % 10;
var yearZhiIndex = offset % 12;
if (yearGanIndex < 0) {
yearGanIndex += 10;
}
if (yearZhiIndex < 0) {
yearZhiIndex += 12;
}
//以立春作为新一年的开始的干支纪年
var g = yearGanIndex;
var z = yearZhiIndex;
//精确的干支纪年,以立春交接时刻为准
var gExact = yearGanIndex;
var zExact = yearZhiIndex;
var solarYear = solar.getYear();
var solarYmd = solar.toYmd();
var solarYmdHms = solar.toYmdHms();
//获取立春的阳历时刻
var liChun = o['jieQi'][I18n.getMessage('jq.liChun')];
if (liChun.getYear() !== solarYear) {
liChun = o['jieQi']['LI_CHUN'];
}
var liChunYmd = liChun.toYmd();
var liChunYmdHms = liChun.toYmdHms();
//阳历和阴历年份相同代表正月初一及以后
if(year===solarYear){
//立春日期判断
if(solarYmd<liChunYmd) {
g--;
z--;
}
//立春交接时刻判断
if(solarYmdHms<liChunYmdHms) {
gExact--;
zExact--;
}
}else if (year < solarYear){
if(solarYmd>=liChunYmd) {
g++;
z++;
}
if(solarYmdHms>=liChunYmdHms) {
gExact++;
zExact++;
}
}
o['yearGanIndex'] = yearGanIndex;
o['yearZhiIndex'] = yearZhiIndex;
o['yearGanIndexByLiChun'] = (g<0?g+10:g)%10;
o['yearZhiIndexByLiChun'] = (z<0?z+12:z)%12;
o['yearGanIndexExact'] = (gExact<0?gExact+10:gExact)%10;
o['yearZhiIndexExact'] = (zExact<0?zExact+12:zExact)%12;
};
var _computeMonth = function(o,solar){
var start = null;
var i;
var end;
var size = LunarUtil.JIE_QI_IN_USE.length;
//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
var index = -3;
for(i=0;i<size;i+=2){
end = o.jieQi[LunarUtil.JIE_QI_IN_USE[i]];
var ymd = solar.toYmd();
var symd = null==start?ymd:start.toYmd();
if(ymd>=symd&&ymd<end.toYmd()){
break;
}
start = end;
index++;
}
var offset = (((o.yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
o['monthGanIndex'] = ((index<0?index+10:index) + offset) % 10;
o['monthZhiIndex'] = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
start = null;
index = -3;
for(i=0;i<size;i+=2){
end = o.jieQi[LunarUtil.JIE_QI_IN_USE[i]];
var time = solar.toYmdHms();
var stime = null==start?time:start.toYmdHms();
if(time>=stime&&time<end.toYmdHms()){
break;
}
start = end;
index++;
}
offset = (((o.yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
o['monthGanIndexExact'] = ((index<0?index+10:index) + offset) % 10;
o['monthZhiIndexExact'] = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
};
var _computeDay = function(o,solar,hour,minute){
var noon = Solar.fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), 12, 0, 0);
var offset = Math.floor(noon.getJulianDay()) - 11;
var dayGanIndex = offset % 10;
var dayZhiIndex = offset % 12;
o['dayGanIndex'] = dayGanIndex;
o['dayZhiIndex'] = dayZhiIndex;
var dayGanExact = dayGanIndex;
var dayZhiExact = dayZhiIndex;
o['dayGanIndexExact2'] = dayGanExact;
o['dayZhiIndexExact2'] = dayZhiExact;
var hm = (hour<10?'0':'')+hour+':'+(minute<10?'0':'')+minute;
if(hm>='23:00'&&hm<='23:59'){
dayGanExact++;
if(dayGanExact>=10){
dayGanExact -= 10;
}
dayZhiExact++;
if(dayZhiExact>=12){
dayZhiExact -= 12;
}
}
o['dayGanIndexExact'] = dayGanExact;
o['dayZhiIndexExact'] = dayZhiExact;
};
var _computeTime = function(o,hour,minute){
var timeZhiIndex = LunarUtil.getTimeZhiIndex((hour<10?'0':'')+hour+':'+(minute<10?'0':'')+minute);
o['timeZhiIndex'] = timeZhiIndex;
o['timeGanIndex'] = (o['dayGanIndexExact']%5*2+timeZhiIndex)%10;
};
var _computeWeek = function(o,solar){
o['weekIndex'] = solar.getWeek();
};
var _compute = function(year,hour,minute,second,solar,ly){
var o = {};
_computeJieQi(o, ly);
_computeYear(o, solar, year);
_computeMonth(o, solar);
_computeDay(o, solar, hour, minute);
_computeTime(o, hour, minute);
_computeWeek(o, solar);
return o;
};
var _fromSolar = function(solar){
var lunarYear = 0;
var lunarMonth = 0;
var lunarDay = 0;
var ly = LunarYear.fromYear(solar.getYear());
var lms = ly.getMonths();
for (var i = 0, j = lms.length; i < j; i++) {
var m = lms[i];
var days = solar.subtract(Solar.fromJulianDay(m.getFirstJulianDay()));
if (days < m.getDayCount()) {
lunarYear = m.getYear();
lunarMonth = m.getMonth();
lunarDay = days + 1;
break;
}
}
return _new(lunarYear, lunarMonth, lunarDay, solar.getHour(), solar.getMinute(), solar.getSecond(), solar, ly);
};
var _fromDate = function(date){
return _fromSolar(Solar.fromDate(date));
};
var _fromYmdHms = function(lunarYear,lunarMonth,lunarDay,hour,minute,second) {
var oy = lunarYear;
var om = lunarMonth;
var od = lunarDay;
var oh = hour;
var oi = minute;
var os = second;
lunarYear *= 1;
if(isNaN(lunarYear)){
throw new Error('wrong lunar year '+oy);
}
lunarMonth *= 1;
if(isNaN(lunarMonth)){
throw new Error('wrong lunar month '+om);
}
lunarDay *= 1;
if(isNaN(lunarDay)){
throw new Error('wrong lunar day '+od);
}
hour *= 1;
if(isNaN(hour)){
throw new Error('wrong hour '+oh);
}
minute *= 1;
if(isNaN(minute)){
throw new Error('wrong minute '+oi);
}
second *= 1;
if(isNaN(second)){
throw new Error('wrong second '+os);
}
if(hour<0||hour>23){
throw new Error('wrong hour '+hour);
}
if(minute<0||minute>59){
throw new Error('wrong minute '+minute);
}
if(second<0||second>59){
throw new Error('wrong second '+second);
}
var y = LunarYear.fromYear(lunarYear);
var m = y.getMonth(lunarMonth);
if (null == m) {
throw new Error('wrong lunar year '+lunarYear+' month '+lunarMonth);
}
if (lunarDay < 1) {
throw new Error('lunar day must bigger than 0');
}
var days = m.getDayCount();
if (lunarDay > days) {
throw new Error('only '+days+' days in lunar year '+lunarYear+' month '+lunarMonth);
}
var noon = Solar.fromJulianDay(m.getFirstJulianDay() + lunarDay - 1);
var solar = Solar.fromYmdHms(noon.getYear(), noon.getMonth(), noon.getDay(), hour, minute, second);
if (noon.getYear() !== lunarYear) {
y = LunarYear.fromYear(noon.getYear());
}
return _new(lunarYear, lunarMonth, lunarDay, hour, minute, second, solar, y);
};
var _new = function(year,month,day,hour,minute,second,solar,ly){
var gz = _compute(year,hour,minute,second,solar,ly);
return {
_p:{
lang: I18n.getLanguage(),
year:year,
month:month,
day:day,
hour:hour,
minute:minute,
second:second,
timeGanIndex:gz.timeGanIndex,
timeZhiIndex:gz.timeZhiIndex,
dayGanIndex:gz.dayGanIndex,
dayZhiIndex:gz.dayZhiIndex,
dayGanIndexExact:gz.dayGanIndexExact,
dayZhiIndexExact:gz.dayZhiIndexExact,
dayGanIndexExact2:gz.dayGanIndexExact2,
dayZhiIndexExact2:gz.dayZhiIndexExact2,
monthGanIndex:gz.monthGanIndex,
monthZhiIndex:gz.monthZhiIndex,
monthGanIndexExact:gz.monthGanIndexExact,
monthZhiIndexExact:gz.monthZhiIndexExact,
yearGanIndex:gz.yearGanIndex,
yearZhiIndex:gz.yearZhiIndex,
yearGanIndexByLiChun:gz.yearGanIndexByLiChun,
yearZhiIndexByLiChun:gz.yearZhiIndexByLiChun,
yearGanIndexExact:gz.yearGanIndexExact,
yearZhiIndexExact:gz.yearZhiIndexExact,
weekIndex:gz.weekIndex,
jieQi:gz.jieQi,
jieQiList:gz.jieQiList,
solar:solar,
eightChar:null
},
getYear:function(){return this._p.year;},
getMonth:function(){return this._p.month;},
getDay:function(){return this._p.day;},
getHour:function(){return this._p.hour;},
getMinute:function(){return this._p.minute;},
getSecond:function(){return this._p.second;},
getTimeGanIndex:function(){return this._p.timeGanIndex;},
getTimeZhiIndex:function(){return this._p.timeZhiIndex;},
getDayGanIndex:function(){return this._p.dayGanIndex;},
getDayGanIndexExact:function(){return this._p.dayGanIndexExact;},
getDayGanIndexExact2:function(){return this._p.dayGanIndexExact2;},
getDayZhiIndex:function(){return this._p.dayZhiIndex;},
getDayZhiIndexExact:function(){return this._p.dayZhiIndexExact;},
getDayZhiIndexExact2:function(){return this._p.dayZhiIndexExact2;},
getMonthGanIndex:function(){return this._p.monthGanIndex;},
getMonthGanIndexExact:function(){return this._p.monthGanIndexExact;},
getMonthZhiIndex:function(){return this._p.monthZhiIndex;},
getMonthZhiIndexExact:function(){return this._p.monthZhiIndexExact;},
getYearGanIndex:function(){return this._p.yearGanIndex;},
getYearGanIndexByLiChun:function(){return this._p.yearGanIndexByLiChun;},
getYearGanIndexExact:function(){return this._p.yearGanIndexExact;},
getYearZhiIndex:function(){return this._p.yearZhiIndex;},
getYearZhiIndexByLiChun:function(){return this._p.yearZhiIndexByLiChun;},
getYearZhiIndexExact:function(){return this._p.yearZhiIndexExact;},
getGan:function(){return this.getYearGan();},
getZhi:function(){return this.getYearZhi();},
getYearGan:function(){return LunarUtil.GAN[this._p.yearGanIndex+1];},
getYearGanByLiChun:function(){return LunarUtil.GAN[this._p.yearGanIndexByLiChun+1];},
getYearGanExact:function(){return LunarUtil.GAN[this._p.yearGanIndexExact+1];},
getYearZhi:function(){return LunarUtil.ZHI[this._p.yearZhiIndex+1];},
getYearZhiByLiChun:function(){return LunarUtil.ZHI[this._p.yearZhiIndexByLiChun+1];},
getYearZhiExact:function(){return LunarUtil.ZHI[this._p.yearZhiIndexExact+1];},
getYearInGanZhi:function(){return this.getYearGan()+this.getYearZhi();},
getYearInGanZhiByLiChun:function(){return this.getYearGanByLiChun()+this.getYearZhiByLiChun();},
getYearInGanZhiExact:function(){return this.getYearGanExact()+this.getYearZhiExact();},
getMonthGan:function(){return LunarUtil.GAN[this._p.monthGanIndex+1];},
getMonthGanExact:function(){return LunarUtil.GAN[this._p.monthGanIndexExact+1];},
getMonthZhi:function(){return LunarUtil.ZHI[this._p.monthZhiIndex+1];},
getMonthZhiExact:function(){return LunarUtil.ZHI[this._p.monthZhiIndexExact+1];},
getMonthInGanZhi:function(){return this.getMonthGan()+this.getMonthZhi();},
getMonthInGanZhiExact:function(){return this.getMonthGanExact()+this.getMonthZhiExact();},
getDayGan:function(){return LunarUtil.GAN[this._p.dayGanIndex+1];},
getDayGanExact:function(){return LunarUtil.GAN[this._p.dayGanIndexExact+1];},
getDayGanExact2:function(){return LunarUtil.GAN[this._p.dayGanIndexExact2+1];},
getDayZhi:function(){return LunarUtil.ZHI[this._p.dayZhiIndex+1];},
getDayZhiExact:function(){return LunarUtil.ZHI[this._p.dayZhiIndexExact+1];},
getDayZhiExact2:function(){return LunarUtil.ZHI[this._p.dayZhiIndexExact2+1];},
getDayInGanZhi:function(){return this.getDayGan()+this.getDayZhi();},
getDayInGanZhiExact:function(){return this.getDayGanExact()+this.getDayZhiExact();},
getDayInGanZhiExact2:function(){return this.getDayGanExact2()+this.getDayZhiExact2();},
getTimeGan:function(){return LunarUtil.GAN[this._p.timeGanIndex+1];},
getTimeZhi:function(){return LunarUtil.ZHI[this._p.timeZhiIndex+1];},
getTimeInGanZhi:function(){return this.getTimeGan()+this.getTimeZhi();},
getShengxiao:function(){return this.getYearShengXiao();},
getYearShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.yearZhiIndex+1];},
getYearShengXiaoByLiChun:function(){return LunarUtil.SHENGXIAO[this._p.yearZhiIndexByLiChun+1];},
getYearShengXiaoExact:function(){return LunarUtil.SHENGXIAO[this._p.yearZhiIndexExact+1];},
getMonthShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.monthZhiIndex+1];},
getMonthShengXiaoExact:function(){return LunarUtil.SHENGXIAO[this._p.monthZhiIndexExact+1];},
getDayShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.dayZhiIndex+1];},
getTimeShengXiao:function(){return LunarUtil.SHENGXIAO[this._p.timeZhiIndex+1];},
getYearInChinese:function(){
var y = this._p.year+'';
var s = '';
var zero = '0'.charCodeAt(0);
for(var i=0,j=y.length;i<j;i++){
s+=LunarUtil.NUMBER[y.charCodeAt(i)-zero];
}
return s;
},
getMonthInChinese:function(){
var month = this._p.month;
return (month<0?'闰':'')+LunarUtil.MONTH[Math.abs(month)];
},
getDayInChinese:function(){
return LunarUtil.DAY[this._p.day];
},
getPengZuGan:function(){
return LunarUtil.PENGZU_GAN[this._p.dayGanIndex+1];
},
getPengZuZhi:function(){
return LunarUtil.PENGZU_ZHI[this._p.dayZhiIndex+1];
},
getPositionXi:function(){
return this.getDayPositionXi();
},
getPositionXiDesc:function(){
return this.getDayPositionXiDesc();
},
getPositionYangGui:function(){
return this.getDayPositionYangGui();
},
getPositionYangGuiDesc:function(){
return this.getDayPositionYangGuiDesc();
},
getPositionYinGui:function(){
return this.getDayPositionYinGui();
},
getPositionYinGuiDesc:function(){
return this.getDayPositionYinGuiDesc();
},
getPositionFu:function(){