date.go raw
1 package dara
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7 )
8
9 type Date struct {
10 date time.Time
11 }
12
13 func NewDate(dateInput string) (*Date, error) {
14 var t time.Time
15 var err error
16 // 解析输入时间,如果输入格式不对,返回错误
17 formats := []string{
18 "2006-01-02 15:04:05",
19 "2006-01-02 15:04:05.999999999 -0700 MST",
20 "2006-01-02T15:04:05-07:00",
21 "2006-01-02T15:04:05Z",
22 }
23
24 for _, format := range formats {
25 t, err = time.Parse(format, dateInput)
26 if err == nil {
27 return &Date{date: t}, nil
28 }
29 }
30
31 return nil, fmt.Errorf("unable to parse date: %v", dateInput)
32 }
33
34 func (t *Date) Format(layout string) string {
35
36 layout = strings.Replace(layout, "yyyy", "2006", 1)
37 layout = strings.Replace(layout, "MM", "01", 1)
38 layout = strings.Replace(layout, "dd", "02", 1)
39 // layout = strings.Replace(layout, "HH", "15", 1)
40 layout = strings.Replace(layout, "hh", "15", 1)
41 layout = strings.Replace(layout, "mm", "04", 1)
42 layout = strings.Replace(layout, "ss", "05", 1)
43 layout = strings.Replace(layout, "a", "PM", 1)
44 layout = strings.Replace(layout, "EEEE", "Monday", 1)
45 layout = strings.Replace(layout, "E", "Mon", 1)
46 return t.date.Format(layout)
47 }
48
49 func (t *Date) Unix() int64 {
50 return t.date.Unix()
51 }
52
53 func (t *Date) UTC() string {
54 return t.date.UTC().Format("2006-01-02 15:04:05.000000000 -0700 MST")
55 }
56
57 func (t *Date) Sub(amount int, unit string) *Date {
58 var duration time.Duration
59 switch unit {
60 case "second", "seconds":
61 duration = time.Duration(-amount) * time.Second
62 case "minute", "minutes":
63 duration = time.Duration(-amount) * time.Minute
64 case "hour", "hours":
65 duration = time.Duration(-amount) * time.Hour
66 case "day", "days":
67 duration = time.Duration(-amount) * 24 * time.Hour
68 case "week", "weeks":
69 duration = time.Duration(-amount) * 7 * 24 * time.Hour
70 case "month", "months":
71 return &Date{date: t.date.AddDate(0, -amount, 0)}
72 case "year", "years":
73 return &Date{date: t.date.AddDate(-amount, 0, 0)}
74 default:
75 return nil
76 }
77 newDate := t.date.Add(duration)
78 return &Date{date: newDate}
79 }
80
81 func (t *Date) Add(amount int, unit string) *Date {
82 var duration time.Duration
83 switch unit {
84 case "second", "seconds":
85 duration = time.Duration(amount) * time.Second
86 case "minute", "minutes":
87 duration = time.Duration(amount) * time.Minute
88 case "hour", "hours":
89 duration = time.Duration(amount) * time.Hour
90 case "day", "days":
91 duration = time.Duration(amount) * 24 * time.Hour
92 case "week", "weeks":
93 duration = time.Duration(amount) * 7 * 24 * time.Hour
94 case "month", "months":
95 return &Date{date: t.date.AddDate(0, amount, 0)}
96 case "year", "years":
97 return &Date{date: t.date.AddDate(amount, 0, 0)}
98 default:
99 return nil
100 }
101
102 newDate := t.date.Add(duration)
103 return &Date{date: newDate}
104 }
105
106 func (t *Date) Diff(amount string, diffDate *Date) int64 {
107 switch amount {
108 case "second", "seconds":
109 return int64(t.date.Sub(diffDate.date).Seconds())
110 case "minute", "minutes":
111 return int64(t.date.Sub(diffDate.date).Minutes())
112 case "hour", "hours":
113 return int64(t.date.Sub(diffDate.date).Hours())
114 case "day", "days":
115 return int64(t.date.Sub(diffDate.date).Hours() / 24)
116 case "week", "weeks":
117 return int64(t.date.Sub(diffDate.date).Hours() / (24 * 7))
118 case "month", "months":
119 return int64(diffDate.date.Year()*12 + int(diffDate.date.Month()) - (t.date.Year()*12 + int(t.date.Month())))
120 case "year", "years":
121 return int64(t.date.Year() - diffDate.date.Year())
122 default:
123 return 0
124 }
125 }
126
127 func (t *Date) Hour() int {
128 return t.date.Hour()
129 }
130
131 func (t *Date) Minute() int {
132 return t.date.Minute()
133 }
134
135 func (t *Date) Second() int {
136 return t.date.Second()
137 }
138
139 func (t *Date) Month() int {
140 return int(t.date.Month())
141 }
142
143 func (t *Date) Year() int {
144 return t.date.Year()
145 }
146
147 func (t *Date) DayOfMonth() int {
148 return t.date.Day()
149 }
150
151 func (t *Date) DayOfWeek() int {
152 weekday := int(t.date.Weekday())
153 if weekday == 0 {
154 return 7 // Sunday
155 }
156 return weekday
157 }
158
159 func (t *Date) WeekOfYear() int {
160 _, week := t.date.ISOWeek()
161 return week
162 }
163