-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
58 lines (49 loc) · 958 Bytes
/
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
package chronos
import (
"time"
)
const (
_dateFormat = "2006/01/02 15:04"
_lunaDateFormat = "2006/01/02"
)
type calendar struct {
time time.Time
}
type Calendar interface {
Lunar() *Lunar
Solar() *Solar
}
type CalendarData interface {
Type() string
Calendar() Calendar
}
//Input support three type of time to create the calendar
//"2006/01/02 03:04" format string
// time.Time value or nil to create a new time.Now() value
func New(v ...interface{}) (c Calendar) {
if v == nil {
return &calendar{time.Now()}
}
switch vv := v[0].(type) {
case string:
return formatDate(vv)
case time.Time:
return &calendar{vv}
}
return
}
func formatDate(s string) Calendar {
t, err := time.Parse(_dateFormat, s)
if err != nil {
t = time.Now()
}
return &calendar{
time: t,
}
}
func (c *calendar) Lunar() *Lunar {
return CalculateLunar(c.time.Format(_dateFormat))
}
func (c *calendar) Solar() *Solar {
return &Solar{time: c.time}
}