-
Notifications
You must be signed in to change notification settings - Fork 82
/
lunar.go
556 lines (489 loc) · 12.7 KB
/
lunar.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
package calendar
import (
"fmt"
"strconv"
"time"
)
const (
// MinYear smallest year of support
MinYear = 1900
// MaxYear largest year of support
MaxYear = 2050
)
var (
// 具体的方法是:用一位来表示一个月的大小,大月记为1,小月记为0,
// 这样就用掉12位(无闰月)或13位(有闰月),再用高四位来表示闰月的月份,没有闰月记为0。
// 例如:2000年的信息数据是0xc96,化成二进制就是110010010110B,表示的
// 含义是:1、2、5、8、10、11月大,其余月份小。
// Since 1900~2050
lunarTable = []int{
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260,
0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255,
0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40,
0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0,
0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4,
0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0,
0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570,
0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4,
0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a,
0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50,
0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552,
0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60,
0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0,
0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577,
0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,
}
lunarMonthNameTable = []string{"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊"}
monthStr1 = []string{"初", "十", "廿", "卅"}
monthStr2 = []string{"日", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
weekday = []string{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}
base = time.Date(MinYear, 1, 31, 0, 0, 0, 0, CST)
// CST CST China Standard Time UT 8:00
CST = time.FixedZone("CST", 3600*8)
)
// Lunar structure
type Lunar struct {
year int
month int
day int
hour int
minute int
second int
nanosecond int
leapMonth int
isLeapMonth bool
}
// NewLunarNow creates current lunar time.
func NewLunarNow() *Lunar {
return NewSolarNow().Convert()
}
// NewLunarTime creates a lunar time from time.Time.
func NewLunarTime(t time.Time) *Lunar {
return NewSolarTime(t).Convert()
}
// NewLunar creates a lunar time.
// NOTE:
// The time zone is +0800(CST)
func NewLunar(year, month, day, hour, min, sec int, nsec int, leapFirst bool) *Lunar {
if !isYearValid(year) {
return nil
}
if year < 0 || month < 0 || day < 0 || hour < 0 || min < 0 || sec < 0 {
return nil
}
leapMonth := LeapMonth(year)
if leapMonth > 0 {
if month > leapMonth || leapFirst && month == leapMonth {
month++
}
}
l := Lunar{}.Add(year, month, day, hour, min, sec, nsec)
return l
}
// LunarZero lunar zero time.
var LunarZero = NewLunar(MinYear, 1, 1, 0, 0, 0, 0, false)
// IsLunarZero judge whether the lunar time is equal to lunar zero time.
func IsLunarZero(l *Lunar) bool {
return l.Equal(LunarZero)
}
const (
hourSecond = 60 * 60
daySecond = 24 * hourSecond
)
// Add adds lunar time, and returns the result of lunar time.
func (l Lunar) Add(years, months, days, hours, mins, secs, nsecs int) *Lunar {
// add years
l.addYear(years)
// add months or years
l.addMonth(months)
// add day, months or years
l.addDay(days)
// add hours, mins, secs, nsecs, day, months or years
l.addSecond(hours*3600+mins*60+secs, nsecs)
return &l
}
// add years
func (l *Lunar) addYear(years int) {
if years == 0 {
return
}
l.year += years
_leapMonth := l.leapMonth
l.leapMonth = LeapMonth(l.year)
if l.leapMonth != _leapMonth {
l.isLeapMonth = false
}
}
// add months or years
func (l *Lunar) addMonth(months int) {
if months == 0 {
return
}
var op int
if months > 0 {
op = 1
} else {
op = -1
months *= -1
}
var r int
for i := 0; i < months; i++ {
if !l.isLeapMonth && l.leapMonth > 0 && l.month == l.leapMonth {
l.isLeapMonth = true
continue
}
r = l.month + op
if r <= 0 {
l.addYear(op)
l.month = 12
if l.month == l.leapMonth {
l.isLeapMonth = true
} else {
l.isLeapMonth = false
}
continue
}
if r > 12 {
l.addYear(op)
l.month = 1
if l.month == l.leapMonth {
l.isLeapMonth = true
} else {
l.isLeapMonth = false
}
continue
}
l.month = r
if l.leapMonth == 0 || l.month != l.leapMonth {
l.isLeapMonth = false
}
}
}
// add day, months or years
func (l *Lunar) addDay(days int) {
if days == 0 {
return
}
var op int
if days > 0 {
op = 1
} else {
op = -1
days *= -1
}
var r int
for i := 0; i < days; i++ {
r = l.day + op
if r <= 0 {
l.addMonth(op)
l.day = LunarMonthDays(l.year, l.month)
continue
}
if r > LunarMonthDays(l.year, l.month) {
l.addMonth(op)
l.day = 1
continue
}
l.day = r
}
}
// add hours, mins, secs, nsecs, day, months or years
func (l *Lunar) addSecond(secs, nsecs int) {
if secs == 0 && nsecs == 0 {
return
}
secs += l.hour*3600 + l.minute*60 + l.second
var days int
days, l.hour, l.minute, l.second, l.nanosecond = SplitDuration(secs, l.nanosecond+nsecs)
l.addDay(days)
}
// Convert converts to a solar calendar time.
func (l *Lunar) Convert() *Solar {
lyear := l.year
lmonth := l.month
lday := l.day
offset := 0
// increment year
for i := MinYear; i < lyear; i++ {
offset += LunarYearDays(i)
}
// increment month
// add days in all months up to the current month
var cur int
for cur = 1; cur < lmonth; cur++ {
offset += LunarMonthDays(lyear, cur)
if cur == l.leapMonth {
// add extra days for leap month
offset += LeapDays(lyear)
}
}
if l.isLeapMonth && l.leapMonth == lmonth {
offset += LunarMonthDays(lyear, cur)
}
// increment
offset += lday - 1
//BUG: maybe overflow
d := time.Duration(offset*24) * time.Hour
solar := base.Add(d)
year := solar.Year()
month := int(solar.Month())
day := solar.Day()
return NewSolar(year, month, day, l.Hour(), l.Minute(), l.Second(), l.Nanosecond(), CST)
}
// Truncate returns the result of rounding t down to a multiple of d (since the zero time).
// If d <= 0, Truncate returns t unchanged.
//
// Truncate operates on the time as an absolute duration since the
// zero time; it does not operate on the presentation form of the
// time. Thus, Truncate(Hour) may return a time with a non-zero
// minute, depending on the time's Location.
func (l Lunar) Truncate(d time.Duration) *Lunar {
return l.Convert().Truncate(d).Convert()
}
// MonthFirst returns lunar time of the month 1 day.
func (l Lunar) MonthFirst() *Lunar {
l.day = 1
return &l
}
// MonthLast returns to the last day of the month.
func (l Lunar) MonthLast() *Lunar {
if l.isLeapMonth {
l.day = LunarMonthDays(l.year, 13)
return &l
}
l.day = LunarMonthDays(l.year, l.month)
return &l
}
// Equal returns whether it is equal to the lunar time.
func (l *Lunar) Equal(lunar *Lunar) bool {
return l.year == lunar.year &&
l.month == lunar.month &&
l.day == lunar.day &&
l.hour == lunar.hour &&
l.minute == lunar.minute &&
l.second == lunar.second &&
l.nanosecond == lunar.nanosecond &&
l.isLeapMonth == lunar.isLeapMonth
}
// GetTime returns the time.Time
func (l *Lunar) GetTime() time.Time {
return l.Convert().Time
}
// String formats time.
func (l *Lunar) String() string {
return fmt.Sprintf("%s%s%s %2d时%2d分%2d秒", LunarYearString(l.Year()), LunarMonthString(l.Month(), l.IsLeapMonth()), LunarDayString(l.Day()), l.Hour(), l.Minute(), l.Second())
}
// Year returns year.
func (l *Lunar) Year() int {
return l.year
}
// Month returns month.
func (l *Lunar) Month() int {
return l.month
}
// MonthStr returns month string
func (l *Lunar) MonthStr() string {
return LunarMonthString(l.Month(), l.IsLeapMonth())
}
// LeapMonth returns leap month number.
func (l *Lunar) LeapMonth() int {
return l.leapMonth
}
// IsLeapMonth returns whether it is leap month.
func (l *Lunar) IsLeapMonth() bool {
return l.isLeapMonth
}
// Day returns day.
func (l *Lunar) Day() int {
return l.day
}
// DayStr returns day string.
func (l *Lunar) DayStr() string {
return LunarDayString(l.Day())
}
// Weekday returns weekday.
func (l *Lunar) Weekday() time.Weekday {
return l.Convert().Weekday()
}
// WeekdayStr returns weekday.
func (l *Lunar) WeekdayStr() string {
return weekday[int(l.Convert().Weekday())]
}
// Hour returns hour.
func (l *Lunar) Hour() int {
return l.hour
}
// Minute returns minute.
func (l *Lunar) Minute() int {
return l.minute
}
// Second returns second.
func (l *Lunar) Second() int {
return l.second
}
// Nanosecond returns nanosecond.
func (l *Lunar) Nanosecond() int {
return l.nanosecond
}
// AnimalYear returns the animal year.
func (l *Lunar) AnimalYear() string {
ganzhiY, _, _ := l.Convert().GanzhiYMD()
return fmt.Sprintf("%s%s", ganzhiY, AnimalYear(l.year))
}
// SetHour sets hour.
// NOTE: hour range [0:23].
func (l *Lunar) SetHour(hour int) *Lunar {
if hour < 0 || hour > 23 {
panic("hour range [0:23]")
}
l.hour = hour
return l
}
// SetMinute sets minute.
// NOTE: minute range [0:59].
func (l *Lunar) SetMinute(minute int) *Lunar {
if minute < 0 || minute > 59 {
panic("minute range [0:59]")
}
l.minute = minute
return l
}
// SetSecond sets second.
// NOTE: second range [0:59].
func (l *Lunar) SetSecond(second int) *Lunar {
if second < 0 || second > 59 {
panic("second range [0:59]")
}
l.second = second
return l
}
// SetNanosecond sets nanosecond.
// NOTE: nanosecond range [0:999999999].
func (l *Lunar) SetNanosecond(nanosecond int) *Lunar {
if nanosecond < 0 || nanosecond > 999999999 {
panic("nanosecond range [0:999999999]")
}
l.nanosecond = nanosecond
return l
}
// Copy returns a copy .
func (l Lunar) Copy() *Lunar {
return &l
}
// Festival returns festival.
func (l *Lunar) Festival(fm FestivalMap) (string, error) {
m := fmt.Sprintf("%2d", l.month)
d := fmt.Sprintf("%2d", l.day)
return fm.Get(m + d)
}
// LunarYearDays the total days of this year
func LunarYearDays(year int) int {
sum := 348
for i := 0x8000; i > 0x8; i >>= 1 {
if (lunarTable[year-MinYear] & i) != 0 {
sum++
}
}
return sum + LeapDays(year)
}
// LeapMonth which month leaps in this year?
// return 1-12(if there is one) or 0(no leap month).
func LeapMonth(year int) int {
return int(lunarTable[year-MinYear] & 0xf)
}
// LunarMonths the total lunar months of this year
func LunarMonths(year int) int {
if LeapMonth(year) == 0 {
return 12
}
return 13
}
// LeapDays the days of this year's leap month
func LeapDays(year int) int {
if LeapMonth(year) != 0 {
if (lunarTable[year-MinYear] & 0x10000) != 0 {
return 30
}
return 29
}
return 0
}
// LunarMonthDays the days of the m-th month of this year.
func LunarMonthDays(year, month int) int {
if (lunarTable[year-MinYear] & (0x10000 >> uint(month))) != 0 {
return 30
}
return 29
}
// LunarYearString used Only by Lunar Object
func LunarYearString(year int) string {
return strconv.Itoa(year) + "年"
}
// LunarMonthString used Only by Lunar Object
func LunarMonthString(month int, leap bool) string {
if leap {
return "闰" + lunarMonthNameTable[(month-1)%12] + "月"
}
return lunarMonthNameTable[(month-1)%12] + "月"
}
// LunarDayString used Only by Lunar Object
func LunarDayString(day int) (s string) {
switch day {
case 10:
s = "初十"
case 20:
s = "二十"
case 30:
s = "三十"
default:
s = monthStr1[int(day/10)]
s += monthStr2[day%10]
}
return
}
// SplitDuration split duration, accurate to second.
func SplitDuration(second, nanosecond int) (days, hours, minutes, seconds, nsecs int) {
seconds, nsecs = norm(second, nanosecond, 1e9)
minutes, seconds = norm(0, seconds, 60)
hours, minutes = norm(0, minutes, 60)
days, hours = norm(0, hours, 24)
return
}
// norm returns nhi, nlo such that
// hi * base + lo == nhi * base + nlo
// 0 <= nlo < base
func norm(hi, lo, base int) (nhi, nlo int) {
if lo < 0 {
n := (-lo-1)/base + 1
hi -= n
lo += n * base
}
if lo >= base {
n := lo / base
hi += n
lo -= n * base
}
return hi, lo
}
func isYearValid(year int) bool {
if year > MaxYear || year < MinYear {
fmt.Printf("Invalid Year: %d, Year Range[%d - %d].\n", year, MinYear, MaxYear)
return false
}
return true
}