forked from godcong/chronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.go
69 lines (59 loc) · 1.12 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
package chronos
import (
"time"
)
// DateFormat ...
const DateFormat = "2006/01/02 15:04"
const LunarDateFormat = "2006/01/02"
type calendar struct {
time time.Time
}
// Calendar ...
type Calendar interface {
Lunar() *Lunar
Solar() *Solar
LunarDate() string
}
// CalendarData ...
type CalendarData interface {
Type() string
Calendar() Calendar
}
//New can input 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{}) Calendar {
var c Calendar
if v == nil {
return &calendar{time.Now()}
}
switch vv := v[0].(type) {
case string:
c = formatDate(vv)
case time.Time:
c = &calendar{vv}
}
return c
}
func formatDate(s string) Calendar {
t, err := time.Parse(DateFormat, s)
if err != nil {
t = time.Now()
}
return &calendar{
time: t,
}
}
// Lunar ...
func (c *calendar) Lunar() *Lunar {
return CalculateLunar(c.time.Format(DateFormat))
}
// Solar ...
func (c *calendar) Solar() *Solar {
return &Solar{time: c.time}
}
// LunarDate ...
func (c *calendar) LunarDate() string {
return c.Lunar().Date()
}