-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
564 lines (494 loc) · 14.3 KB
/
calendar.go
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
/**
* Copyright (c) 2011 ~ 2014 Deepin, Inc.
* 2013 ~ 2014 jouyouyun
*
* Author: jouyouyun <jouyouwen717@gmail.com>
* Maintainer: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
**/
package lunarcalendar
import (
"fmt"
"time"
)
func isYearValid(year int32) bool {
if year > MaxYear || year < MinYear {
fmt.Printf("Invalid Year: %d. Year Range(%d - %d)\n",
year, MinYear, MaxYear)
return false
}
return true
}
/**
* 判断农历年闰月数
* @param {Number} year 农历年
* return 闰月数 (月份从1开始)
*/
func getLunarLeapYear(year int32) (int32, bool) {
if !isYearValid(year) {
return -1, false
}
info := lunarInfos[year-MinYear]
//fmt.Println("LeapMonth: ", info.leapMonth)
//fmt.Println("MonthNum: ", info.lunarMonthNum)
//fmt.Println("ZhengMonth: ", info.zhengMonth)
//fmt.Println("ZhengDay: ", info.zhengDay)
return info.leapMonth, true
}
/**
* 获取农历年每月的天数及一年的总天数
*/
func getLunarYearDays(year int32) ([]caDayInfo, int32, bool) {
if !isYearValid(year) {
return nil, -1, false
}
info := lunarInfos[year-MinYear]
leapMonth := info.leapMonth
monthData := fmt.Sprintf("%b", info.lunarMonthNum)
//fmt.Println("Month Bianry before insert: ", monthData)
tmp := ""
l := len(monthData)
//还原数据至16位,少于16位的在前面插入0(二进制存储时前面的0被忽略)
for i := 0; i < 16-l; i++ {
tmp += "0"
}
monthData = tmp + monthData
//fmt.Println("Month Bianry after insert: ", monthData)
monthNum := 0
if leapMonth > 0 {
monthNum = 13
} else {
monthNum = 12
}
yearDays := int32(0)
monthDayInfos := []caDayInfo{}
for i := 0; i < monthNum; i++ {
tmp := caDayInfo{}
if monthData[i] == '0' {
yearDays += 29
tmp.days = 29
} else {
yearDays += 30
tmp.days = 30
}
// 让月份从1开始,不从0开始
//t := i + 1
// 处理闰月
//if i >= leapMonth {
//t -= 1
//}
//tmp.index = t
tmp.index = int32(i)
monthDayInfos = append(monthDayInfos, tmp)
}
return monthDayInfos, yearDays, true
}
/**
* 通过间隔天数查找农历日期
*/
func getLunarDateByBetween(year, between int32) (CaYearInfo, bool) {
month := int32(-1)
day := int32(-1)
monthDayInfos, yearDays, ok := getLunarYearDays(year)
if !ok {
fmt.Println("Get Year Days Failed For Year: ", year)
return CaYearInfo{year, month, day}, false
}
//leapMonth, _ := getLunarLeapYear(year)
end := int32(0)
if between > 0 {
end = between
} else {
end = yearDays + between
}
//fmt.Println("Between: ", end)
tmpDays := int32(0)
for _, info := range monthDayInfos {
tmpDays += info.days
//fmt.Println("\tTmp: ", tmpDays)
if tmpDays > end {
month = info.index
tmpDays = tmpDays - info.days
break
}
}
day = end - tmpDays + 1
return CaYearInfo{year, month, day}, true
}
/**
* 通过公历日期获取农历日期
*/
func getLunarDateBySolar(year, month, day int32) (CaYearInfo, bool) {
if !isYearValid(year) {
return CaYearInfo{-1, -1, -1}, false
}
info := lunarInfos[year-MinYear]
zengMonth := info.zhengMonth
zengDay := info.zhengDay
between, _ := getDaysBetweenSolar(year, zengMonth, zengDay,
year, month, day)
if between == 0 { //正月初一
return CaYearInfo{year, 0, 1}, true
} else if between < 0 {
year -= 1
}
return getLunarDateByBetween(year, int32(between))
}
/**
* 计算两个公历日期之间的天数
*/
func getDaysBetweenSolar(year, month, day, year1, month1, day1 int32) (int64, bool) {
date := time.Date(int(year), time.Month(month), int(day),
0, 0, 0, 0, time.UTC).Unix()
date1 := time.Date(int(year1), time.Month(month1), int(day1),
0, 0, 0, 0, time.UTC).Unix()
return (date1 - date) / 86400, true
}
/**
* 计算农历日期离正月初一有多少天
*/
func getDaysBetweenZheng(year, month, day int32) (int32, bool) {
monthDayInfos, _, ok := getLunarYearDays(year)
if !ok {
fmt.Println("Get Year Days Failed For Year: ", year)
return -1, false
}
days := int32(0)
for _, info := range monthDayInfos {
if info.index < month {
days += info.days
} else {
break
}
}
return days + day - 1, true
}
func formatDayD4(month, day int32) string {
monStr := ""
dayStr := ""
if month < 10 {
monStr = fmt.Sprintf("0%d", month)
} else {
monStr = fmt.Sprintf("%d", month)
}
if day < 10 {
dayStr = fmt.Sprintf("0%d", day)
} else {
dayStr = fmt.Sprintf("%d", day)
}
return fmt.Sprintf("d%s%s", monStr, dayStr)
}
/**
* 某年的第n个节气为几日
* 31556925974.7为地球公转周期,是毫秒
* 1890年的正小寒点:01-05 16:02:31,1890年为基准点
* year 公历年
* n 第几个节气,从0小寒起算
* 由于农历24节气交节时刻采用近似算法,可能存在少量误差(30分钟内)
*/
func getTermDate(year, n int32) (CaYearInfo, bool) {
if !isYearValid(year) {
return CaYearInfo{}, false
}
offset := 31556925974/1000*(int64(year)-1890) + int64(termInfo[n])*60 + time.Date(1890, 1, 5, 16, 2, 31, 0, time.UTC).Unix()
y, m, d := time.Unix(offset, 0).Date()
return CaYearInfo{int32(y), int32(m), int32(d)}, true
}
/**
* 获取公历年一年的二十四节气
* 返回key:日期,value:节气中文名
*/
func getYearTerm(year int32) map[string]string {
res := make(map[string]string)
month := int32(0)
for i := int32(0); i < 24; i++ {
if info, ok := getTermDate(year, i); !ok {
continue
} else {
// 每个月中有两个节气
month = i/2 + 1
res[formatDayD4(month, info.Day)] = lunarData["solarTerm"][i]
}
}
return res
}
/**
* 获取生肖
* 十二生肖,即:鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
* year: 干支所在年(默认以立春前的公历年作为基数)
*/
func getYearZodiac(year int32) (string, bool) {
if !isYearValid(year) {
return "", false
}
// 1890 属虎
num := year - 1890 + 2 + 24 //参考干支纪年的计算,生肖对应地支
//fmt.Println("zodiac num: ", num)
return lunarData["zodiac"][num%12], true
}
/**
* 计算天干地支
* num 60进制中的位置(把60个天干地支,当成一个60进制的数)
*/
func cyclical(num int32) (string, bool) {
return lunarData["heavenlyStems"][num%10] + lunarData["earthlyBranches"][num%12], true
}
/**
* 获取干支纪年
* year 干支所在年
* offset 偏移量,默认为0,便于查询一个年跨两个干支纪年(以立春为分界线)
*/
func getLunarYearName(year, offset int32) (string, bool) {
if !isYearValid(year) {
return "", false
}
offset = offset | 0
return cyclical(year - 1890 + 26 + offset)
}
/**
* 获取干支纪月
* year,month 公历年,干支所在月
* offset 偏移量,默认为0,便于查询一个年跨两个干支纪年(以立春为分界线)
*/
func getLunarMonthName(year, month, offset int32) (string, bool) {
if !isYearValid(year) {
return "", false
}
offset = offset | 0
return cyclical((year-1890)*12 + month + 12 + offset)
}
/**
* 获取干支纪日
* year,month,day 公历年,月,日
*/
func getLunarDayName(year, month, day int32) (string, bool) {
if !isYearValid(year) {
return "", false
}
//当日与1890/1/1 相差天数
//1890/1/1与 1970/1/1 相差29219日, 1890/1/1 日柱为壬午日(60进制18)
date := time.Date(int(year), time.Month(month), int(day),
0, 0, 0, 0, time.UTC).Unix()
dayCyclical := date/86400 + 29219 + 18
return cyclical(int32(dayCyclical))
}
/**
* 获取公历月份的天数
*/
func getSolarMonthDays(year, month int32) (int32, bool) {
if !isYearValid(year) {
return -1, false
}
monthDays := []int32{}
if isLeapYear(year) {
monthDays = []int32{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
} else {
monthDays = []int32{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
}
return monthDays[month-1], true
}
func isLeapYear(year int32) bool {
return (year%4 == 0 && year%100 == 0) || year%400 == 0
}
/**
* 将农历转换为公历
* year,month,day 农历年,月(1-13,有闰月),日
*/
func lunarToSolar(year, month, day int32) (CaYearInfo, bool) {
if !isYearValid(year) {
return CaYearInfo{-1, -1, -1}, false
}
between, _ := getDaysBetweenZheng(year, month, day)
info := lunarInfos[year-MinYear]
zengMonth := info.zhengMonth
zengDay := info.zhengDay
offDate := time.Date(int(year), time.Month(zengMonth), int(zengDay),
0, 0, 0, 0, time.UTC).Unix() + int64(between)*86400
newDate := time.Unix(offDate, 0)
y, m, d := newDate.Date()
return CaYearInfo{int32(y), int32(m), int32(d)}, true
}
/**
* 将公历转换为农历
*/
func solarToLunar(year, month, day int32) (caLunarDayInfo, bool) {
if !isYearValid(year) {
return caLunarDayInfo{}, false
}
cacheObj.setCurrent(year)
// 立春日期
v, ok := cacheObj.getCache("term2")
if !ok {
info, _ := getTermDate(year, 2)
v = cacheObj.setCache("term2", info)
}
term2 := v.(CaYearInfo)
// 二十四节气
v, ok = cacheObj.getCache("termList")
if !ok {
list := getYearTerm(year)
v = cacheObj.setCache("termList", list)
}
termList := v.(map[string]string)
//某月第一个节气开始日期
firstTerm, _ := getTermDate(year, month*2-1)
//干支所在年份
ganZhiYear := int32(0)
if month > 1 || (month == 1 && day >= term2.Day) {
ganZhiYear = year
} else {
ganZhiYear = year - 1
}
//干支所在月份(以节气为界)
ganZhiMonth := int32(0)
if day >= firstTerm.Day {
ganZhiMonth = month
} else {
ganZhiMonth = month - 1
}
lunarDate, _ := getLunarDateBySolar(year, month, day)
//fmt.Printf("SOLAR DATE: %v - %v - %v\n", year, month, day)
//fmt.Println("LUNAR DATE:", lunarDate)
lunarLeapMonth, _ := getLunarLeapYear(lunarDate.Year)
lunarMonthName := ""
if lunarLeapMonth > 0 && lunarLeapMonth == lunarDate.Month {
lunarMonthName = "闰" + lunarData["monthCn"][lunarDate.Month-1] + "月"
} else if lunarLeapMonth > 0 && lunarLeapMonth < lunarDate.Month {
lunarMonthName = lunarData["monthCn"][lunarDate.Month-1] + "月"
} else {
lunarMonthName = lunarData["monthCn"][lunarDate.Month] + "月"
}
//农历节日判断
lunarFtv := ""
lunarMonthInfos, _, _ := getLunarYearDays(lunarDate.Year)
lunarMonthLen := int32(len(lunarMonthInfos))
//除夕
if int32(lunarDate.Month) == (lunarMonthLen-1) && lunarDate.Day == lunarMonthInfos[lunarMonthLen-1].days {
lunarFtv = lunarFestival["d0100"]
} else if lunarLeapMonth > 0 && lunarDate.Month >= lunarLeapMonth {
lunarFtv = lunarFestival[formatDayD4(lunarDate.Month, lunarDate.Day)]
} else {
lunarFtv = lunarFestival[formatDayD4(lunarDate.Month+1, lunarDate.Day)]
}
// 返回结果
resInfo := caLunarDayInfo{}
//fmt.Println("GanZhiYear: ", ganZhiYear)
zodiac, _ := getYearZodiac(ganZhiYear)
resInfo.Zodiac = zodiac
yearName, _ := getLunarYearName(ganZhiYear, 0)
resInfo.GanZhiYear = yearName
monthName, _ := getLunarMonthName(year, ganZhiMonth, 0)
resInfo.GanZhiMonth = monthName
dayName, _ := getLunarDayName(year, month, day)
resInfo.GanZhiDay = dayName
resInfo.Term = termList[formatDayD4(month, day)]
resInfo.LunarMonthName = lunarMonthName
resInfo.LunarDayName = lunarData["dateCn"][lunarDate.Day-1]
resInfo.LunarLeapMonth = lunarLeapMonth
resInfo.SolarFestival = solarFestival[formatDayD4(month, day)]
resInfo.LunarFestival = lunarFtv
resInfo.Worktime = 0
//fmt.Printf("*** Date: %v - %v - %v\n", year, month, day)
if m, ok := worktimeYearMap[fmt.Sprintf("y%d", year)]; ok {
//fmt.Printf("--- get %d worktime\n", year)
if v, ok := m[formatDayD4(month, day)]; ok {
//fmt.Printf("--- get %d - %d worktime\n", month, day)
resInfo.Worktime = v
}
}
return resInfo, true
}
/**
* 获取指定公历月份的农历数据
* year,month 公历年,月
* fill 是否用上下月数据补齐首尾空缺,首例数据从周日开始
*/
func getLunarCalendar(year, month int32, fill bool) (caLunarMonthInfo, bool) {
if !isYearValid(year) {
return caLunarMonthInfo{}, false
}
solarData, _ := getSolarCalendar(year, month, fill)
l := len(solarData.Datas)
datas := []caLunarDayInfo{}
for i := 0; i < l; i++ {
data1 := solarData.Datas[i]
tmp, _ := solarToLunar(data1.Year, data1.Month, data1.Day)
datas = append(datas, tmp)
}
return caLunarMonthInfo{solarData.FirstDayWeek, solarData.Days, datas}, true
}
/**
* 公历某月日历
* year,month 公历年,月
* fill 是否用上下月数据补齐首尾空缺,首例数据从周日开始(7*6阵列)
*/
func getSolarCalendar(year, month int32, fill bool) (caSolarMonthInfo, bool) {
if !isYearValid(year) {
return caSolarMonthInfo{}, false
}
date := time.Date(int(year), time.Month(month), 1, 0, 0, 0, 0, time.UTC)
week := int32(date.Weekday())
days, _ := getSolarMonthDays(year, month)
monthData := getMonthDatas(year, month, days, 1)
if fill {
if week > 0 { //前补
// 获取前一个月的日期
preYear := int32(0)
preMonth := int32(0)
if month-1 <= 0 {
preYear = year - 1
preMonth = 12
} else {
preMonth = month - 1
preYear = year
}
preDays, _ := getSolarMonthDays(preYear, preMonth)
preMonthData := getMonthDatas(preYear, preMonth,
week, preDays-week+1)
preMonthData = append(preMonthData, monthData...)
monthData = preMonthData
}
if 7*6-len(monthData) != 0 { // 后补
// 获取前一个月的日期
nextYear := int32(0)
nextMonth := int32(0)
if month+1 > 12 {
nextYear = year + 1
nextMonth = 1
} else {
nextMonth = month + 1
nextYear = year
}
fillLen := int32(7*6 - len(monthData))
nextMonthData := getMonthDatas(nextYear, nextMonth,
fillLen, 1)
monthData = append(monthData, nextMonthData...)
}
}
return caSolarMonthInfo{week, days, monthData}, true
}
func getMonthDatas(year, month, length, start int32) []CaYearInfo {
monthDatas := []CaYearInfo{}
if length < 1 {
return monthDatas
}
k := start | 0
for i := int32(0); i < length; i++ {
tmp := CaYearInfo{year, month, k}
monthDatas = append(monthDatas, tmp)
k++
}
return monthDatas
}