-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtime.go
399 lines (348 loc) · 7.99 KB
/
time.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
//Copyright 2018 The axx Authors. All rights reserved.
package bast
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
"time"
)
var loc *time.Location
func init() {
loc, _ = time.LoadLocation("Local")
}
//Time yyyy-MM-dd HH:mm:ss format time
//1: auto handle string to time
type Time struct {
time.Time
}
//MarshalJSON JSON MarshalJSON
func (t Time) MarshalJSON() ([]byte, error) {
stamp := "\"\""
if !t.IsZero() {
stamp = fmt.Sprintf("\"%s\"", t.Time.Format("2006-01-02 15:04:05"))
}
return []byte(stamp), nil
}
//UnmarshalJSON JSON UnmarshalJSON
func (t *Time) UnmarshalJSON(b []byte) error {
tt, err := byteToTime(b, "")
if err == nil {
*t = tt
}
return err
}
//Value support sql.Driver interface
func (t Time) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
//Scan support scan
func (t *Time) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = Time{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to Time", v)
}
//T get time.Time
//if zero time return nil
func (t *Time) T() *time.Time {
if !t.IsZero() {
return &t.Time
}
return nil
}
//Now returns the current local time.
func Now() Time {
tt := Time{Time: time.Now()}
return tt
}
//Nows returns the current local *time.
func Nows() *Time {
tt := Time{Time: time.Now()}
return &tt
}
//NowsNil returns the nil *bast.time.
func NowsNil() *Time {
var t Time
return &t
}
//NowTime returns the current local *time.
func NowTime() *Time {
tt := Time{Time: time.Now()}
return &tt
}
//TimeByTime time.Time to Time
func TimeByTime(t time.Time) Time {
tt := Time{Time: t}
return Time(tt)
}
//TimesByTime time.Time to *Time
func TimesByTime(t time.Time) *Time {
tt := TimeByTime(t)
return &tt
}
//TimeByDate Date to Time
func TimeByDate(d Date) Time {
t := Time(d)
return t
}
//TimeByDates *Date to Time
func TimeByDates(d *Date) Time {
var t Time
if d != nil {
t = Time(*d)
}
return t
}
//TimesByDate Date to *Time
func TimesByDate(d Date) *Time {
tt := Time(d)
return &tt
}
//TimesByDates *Date to *Time
func TimesByDates(d *Date) *Time {
if d != nil {
t := Time(*d)
return &t
}
return nil
}
//TimeWithString string to Time
func TimeWithString(t string, layout ...string) (Time, error) {
l := ""
if layout != nil {
l = layout[0]
}
return strToTime(t, l)
}
//TimesWithString string to *Time
func TimesWithString(t string, layout ...string) (*Time, error) {
l := ""
if layout != nil {
l = layout[0]
}
tt, err := strToTime(t, l)
if err == nil {
t := Time(tt)
return &t, nil
}
return nil, err
}
//strToTime
func strToTime(t string, layout string) (Time, error) {
return byteToTime([]byte(t), layout)
}
//byteToTime
func byteToTime(b []byte, layout string) (Time, error) {
var t Time
var err error
if b != nil && len(b) > 0 {
s := strings.Trim(string(b), "\"")
l := len(s)
var v time.Time
if l > 0 {
if layout == "" {
if l <= 8 {
layout = "2006-1-2"
} else if l <= 10 {
layout = "2006-01-02"
} else if l <= 16 {
layout = "2006-01-02 15:04"
} else {
layout = "2006-01-02 15:04:05"
}
}
v, err = time.ParseInLocation(layout, s, loc)
if err == nil {
t = Time{Time: v}
} else {
err = errors.New("bytes to time error")
}
}
}
return t, err
}
//String
func (t Time) String() string {
return t.Time.Format("2006-01-02 15:04:05")
}
//Format return format string time
//layout support yyyy、MM、dd、hh、HH、mm、ss
func (t Time) Format(layout string) string {
if layout == "" {
layout = "2006-01-02"
} else {
layout = strings.Replace(layout, "yyyy", "2006", 1)
layout = strings.Replace(layout, "MM", "01", 1)
layout = strings.Replace(layout, "dd", "02", 1)
layout = strings.Replace(layout, "hh", "15", 1)
layout = strings.Replace(layout, "mm", "04", 1)
layout = strings.Replace(layout, "ss", "05", 1)
}
return t.Time.Format(layout)
}
// After reports whether the time instant t is after u.
func (t Time) After(u Time) bool {
return t.Time.After(u.Time)
}
// Before reports whether the time instant t is before u.
func (t Time) Before(u Time) bool {
return t.Time.Before(u.Time)
}
// Equal reports whether t and u represent the same time instant.
// Two times can be equal even if they are in different locations.
// For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
// See the documentation on the Time type for the pitfalls of using == with
// Time values; most code should use Equal instead.
func (t Time) Equal(u Time) bool {
return t.Time.Equal(u.Time)
}
//Date yyyy-MM-dd format date
//1: auto handle string to time
type Date Time
//NowDate returns the current local date.
func NowDate() Date {
y, m, d := time.Now().Date()
t := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
return Date(Time{Time: t})
}
//NowDates returns the current local *date.
func NowDates() *Date {
d := NowDate()
return &d
}
//DateByTime time.Time to Date
func DateByTime(t time.Time) Date {
y, m, d := t.Date()
t = time.Date(y, m, d, 0, 0, 0, 0, t.Location())
return Date(Time{Time: t})
}
//DatesByTime time.Time to *Date
func DatesByTime(t time.Time) *Date {
tt := DateByTime(t)
return &tt
}
//DateByBTime bast.Time to Date
func DateByBTime(t Time) Date {
return DateByTime(t.Time)
}
//DatesByBTime *bast.Time to *Date
func DatesByBTime(t Time) *Date {
return DatesByTime(t.Time)
}
//DateByBTimes *bast.Time to Date
func DateByBTimes(t *Time) Date {
var tt Date
if t != nil {
tt = DateByTime(t.Time)
}
return tt
}
//DatesByBTimes *bast.Time to *Date
func DatesByBTimes(t *Time) *Date {
if t != nil {
return DatesByBTime(*t)
}
return nil
}
//DateWithString string to Date
func DateWithString(t string, layout ...string) (Date, error) {
l := ""
if layout != nil {
l = layout[0]
}
tt, err := strToTime(t, l)
return Date(tt), err
}
//DatesWithString string to *Date
func DatesWithString(t string, layout ...string) (*Date, error) {
l := ""
if layout != nil {
l = layout[0]
}
tt, err := strToTime(t, l)
if err == nil {
d := Date(tt)
return &d, nil
}
return nil, err
}
// After reports whether the time instant t is after u.
func (t Date) After(u Date) bool {
return t.Time.After(u.Time)
}
// Before reports whether the time instant t is before u.
func (t Date) Before(u Date) bool {
return t.Time.Before(u.Time)
}
// Equal reports whether t and u represent the same time instant.
// Two times can be equal even if they are in different locations.
// For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
// See the documentation on the Time type for the pitfalls of using == with
// Time values; most code should use Equal instead.
func (t Date) Equal(u Date) bool {
return t.Time.Equal(u.Time)
}
//MarshalJSON JSON MarshalJSON
func (t Date) MarshalJSON() ([]byte, error) {
stamp := "\"\""
if !t.IsZero() {
stamp = fmt.Sprintf("\"%s\"", t.Time.Format("2006-01-02"))
}
return []byte(stamp), nil
}
//UnmarshalJSON JSON UnmarshalJSON
func (t *Date) UnmarshalJSON(b []byte) error {
if t != nil {
tt := Time(*t)
if err := tt.UnmarshalJSON(b); err == nil {
*t = Date(tt)
}
}
return nil
}
//Value support sql.Driver interface
func (t Date) Value() (driver.Value, error) {
var zt time.Time
if t.Time.UnixNano() == zt.UnixNano() {
return nil, nil
}
return t.Time, nil
}
//Scan support scan
func (t *Date) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = DateByTime(value)
return nil
}
return fmt.Errorf("can not convert %v to Date", v)
}
//String
func (t Date) String() string {
return t.Time.Format("2006-01-02")
}
//TimeRangeWithYearMonth returns the time(year,month) of the specified mininum and maxnum time
func TimeRangeWithYearMonth(year, month int) (start, end time.Time) {
m := time.Month(month)
s := time.Date(year, m, 1, 0, 0, 0, 0, loc)
e := time.Date(year, m, MaxDay(month), 23, 59, 59, 0, loc)
return s, e
}
//MaxDay return the maxnum day of the month
func MaxDay(month int) int {
switch month {
case 1, 3, 5, 7, 8, 10, 12:
return 31
case 2:
return 28
default:
return 30
}
}