-
Notifications
You must be signed in to change notification settings - Fork 1
/
carbon.go
290 lines (244 loc) · 6.07 KB
/
carbon.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
package carbon
import (
"math"
"time"
"strings"
)
const (
DATE_TIME_LAYOUT = "2006-01-02 15:04:05"
)
// form @github.com/jinzhu/now and more
var TimeFormats = []string{
DATE_TIME_LAYOUT,
"1/2/2006", "1/2/2006 15:4:5", "2006-1-2 15:4:5", "2006-1-2 15:4", "2006-1-2", "1-2", "15:4:5", "15:4",
"2013-02-03", "15:4:5 Jan 2, 2006 MST", "15:04:05", "2013/02/03",
"2013-02-03 19:54:00 PST",
time.RFC822, time.RubyDate, time.RFC822Z, time.RFC3339,
}
type Carbon struct {
time.Time
}
// return Carbon with time Now
func Now() *Carbon {
c := &Carbon{time.Now()}
return c
}
// return Carbon with time Now
func CreateFromTime(timeObject time.Time) *Carbon {
c := &Carbon{timeObject}
return c
}
// create Carbon with year, month, day, hour, minute, second, nanosecond, int, TZ string
// Create(2001, 12, 01, 15, 25, 55, 0, "UTC") = 2001-12-01 13:25:55 +0000 UTC
func Create(year, month, day int, args ...interface{}) *Carbon {
var tz string
Month := time.Month(month)
hour := time.Now().Hour()
minute := time.Now().Minute()
second := time.Now().Second()
nanosecond := time.Now().Nanosecond()
location := time.Now().Location()
for key, val := range args {
if key == 0 {
hour = val.(int)
}
if key == 1 {
minute = val.(int)
}
if key == 2 {
second = val.(int)
}
if key == 3 {
nanosecond = val.(int)
}
if key == 4 {
tz = val.(string)
}
}
c := &Carbon{time.Date(year, Month, day, hour, minute, second, nanosecond, location)}
if tz != "" {
c.SetTZ(tz)
}
return c
}
// create Carbon from time string, return in UTC, if tz not specified
// CreateFrom("2015-01-25 15:4:5") = 2015-01-25 15:04:55 +0000 UTC
func CreateFrom(stringDate string) (*Carbon, error) {
var carbon = Now()
var err error
var tm time.Time
for _, format := range TimeFormats {
tm, err = time.Parse(format, stringDate)
if err == nil {
carbon.Time = tm
break
}
}
return carbon, err
}
// custom Unmarshal func form many times formats
func (t *Carbon) UnmarshalJSON(buf []byte) error {
tt, err := CreateFrom(strings.Trim(string(buf), `"`))
if err != nil {
return err
}
t.Time = tt.Time
return nil
}
// set tx location, ex. "UTC"
func (c *Carbon) SetTZ(tz string) *Carbon {
location, _ := time.LoadLocation(tz)
if location != nil {
c.Time = c.Time.In(location)
}
return c
}
// set time, ex. "23.59.29"
func (c *Carbon) SetTime(hours, minutes, seconds int) *Carbon {
c.Time = time.Date(
c.Year(),
c.Month(),
c.Day(),
hours,
minutes,
seconds,
c.Nanosecond(),
c.Location(),
)
return c
}
func (c *Carbon) SubDay() *Carbon {
return c.SubDays(1)
}
func (c *Carbon) SubDays(days int) *Carbon {
c.Time = c.Time.AddDate(0, 0, -days)
return c
}
func (c *Carbon) SubMonth(days int) *Carbon {
return c.SubMonths(1)
}
func (c *Carbon) SubMonths(months int) *Carbon {
c.Time = c.Time.AddDate(0, -months, 0)
return c
}
func (c *Carbon) SubYear(days int) *Carbon {
return c.SubYears(1)
}
func (c *Carbon) SubYears(years int) *Carbon {
c.Time = c.Time.AddDate(-years, 0, 0)
return c
}
func (c *Carbon) AddDay() *Carbon {
return c.AddDays(1)
}
func (c *Carbon) AddDays(days int) *Carbon {
c.Time = c.Time.AddDate(0, 0, days)
return c
}
func (c *Carbon) AddMonth(days int) *Carbon {
return c.AddMonths(1)
}
func (c *Carbon) AddMonths(months int) *Carbon {
c.Time = c.Time.AddDate(0, months, 0)
return c
}
func (c *Carbon) AddYear(days int) *Carbon {
return c.AddYears(1)
}
func (c *Carbon) AddYears(years int) *Carbon {
c.AddDate(years, 0, 0)
return c
}
func (c *Carbon) DiffInSeconds(from *Carbon) int {
return round(c.Sub(from.Time).Seconds())
}
func (c *Carbon) DiffInMinutes(from *Carbon) int {
return round(c.Sub(from.Time).Minutes())
}
func (c *Carbon) DiffInHours(from *Carbon) int {
return round(c.Sub(from.Time).Hours())
}
// Determines if the instance is equal to another
func (c *Carbon) Eq(another *Carbon) bool {
return c.Equal(another.Time)
}
// Determines if the instance is greater (after) than another
func (c *Carbon) Gt(another *Carbon) bool {
return c.After(another.Time)
}
// Determines if the instance is less (Before) than another
func (c *Carbon) Lt(another *Carbon) bool {
return c.Before(another.Time)
}
// Determines if the instance is greater than before and less than after
func (c *Carbon) Between(before, after *Carbon) bool {
return c.After(before.Time) && c.Before(after.Time)
}
func (c *Carbon) StartOfHour() *Carbon {
c.Time = c.Truncate(time.Hour)
return c
}
func (c *Carbon) EndOfHour() *Carbon {
c.StartOfHour()
c.Time = c.Add(time.Hour - time.Second)
return c
}
func (c *Carbon) StartOfDay() *Carbon {
c.Time = c.StartOfHour().Add(-time.Hour * time.Duration(c.Hour()))
return c
}
func (c *Carbon) EndOfDay() *Carbon {
c.Time = c.StartOfDay().Add(time.Hour*time.Duration(24) - time.Second)
return c
}
func (c *Carbon) StartOfWeek(firstDayOfWeekIsMonday ...bool) *Carbon {
firstDay := time.Monday
corrFirstDay := 1
if len(firstDayOfWeekIsMonday) > 0 {
if !firstDayOfWeekIsMonday[0] {
firstDay = time.Sunday
corrFirstDay = 0
}
}
c.StartOfDay()
if c.Weekday() != firstDay {
c.Time = c.Add(-time.Hour * 24 * time.Duration(-corrFirstDay+int(c.Weekday())))
}
return c
}
func (c *Carbon) EndOfWeek() *Carbon {
c.Time = c.StartOfWeek().Add(time.Hour*time.Duration(24*7) - time.Second)
return c
}
func (c *Carbon) StartOfMonth() *Carbon {
year := c.Year()
Month := c.Month()
location := time.Now().Location()
c = &Carbon{time.Date(year, Month, 1, 0, 0, 0, 0, location)}
return c
}
func (c *Carbon) EndOfMonth() *Carbon {
c.Time = c.StartOfMonth().AddDate(0, 1, 0).Add(-time.Second)
return c
}
func (c *Carbon) StartOfYear() *Carbon {
year := c.Year()
location := time.Now().Location()
c = &Carbon{time.Date(year, time.Month(1), 1, 0, 0, 0, 0, location)}
return c
}
func (c *Carbon) EndOfYear() *Carbon {
c.Time = c.StartOfYear().AddDate(1, 0, 0).Add(-time.Second)
return c
}
//formatters
// return string with DateTime format "2006-01-25 15:04:05"
func (c *Carbon) ToDateTimeString() string {
return c.Format(DATE_TIME_LAYOUT)
}
func round(f float64) int {
if math.Abs(f) < 0.5 {
return 0
}
return int(f + math.Copysign(0.5, f))
}