-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
time.go
368 lines (286 loc) · 7.58 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
package types
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
var numOfDigitsOfUnixTimestamp = len(strconv.FormatInt(time.Now().Unix(), 10))
var numOfDigitsOfMilliSecondUnixTimestamp = len(strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10))
var numOfDigitsOfNanoSecondsUnixTimestamp = len(strconv.FormatInt(time.Now().UnixNano(), 10))
type NanosecondTimestamp time.Time
func (t NanosecondTimestamp) Time() time.Time {
return time.Time(t)
}
func (t *NanosecondTimestamp) UnmarshalJSON(data []byte) error {
var v int64
var err = json.Unmarshal(data, &v)
if err != nil {
return err
}
*t = NanosecondTimestamp(time.Unix(0, v))
return nil
}
type MillisecondTimestamp time.Time
func NewMillisecondTimestampFromInt(i int64) MillisecondTimestamp {
return MillisecondTimestamp(time.Unix(0, i*int64(time.Millisecond)))
}
func MustParseMillisecondTimestamp(a string) MillisecondTimestamp {
m, err := strconv.ParseInt(a, 10, 64) // startTime
if err != nil {
panic(fmt.Errorf("millisecond timestamp parse error %v", err))
}
return NewMillisecondTimestampFromInt(m)
}
func MustParseUnixTimestamp(a string) time.Time {
m, err := strconv.ParseInt(a, 10, 64) // startTime
if err != nil {
panic(fmt.Errorf("millisecond timestamp parse error %v", err))
}
return time.Unix(m, 0)
}
func (t MillisecondTimestamp) String() string {
return time.Time(t).String()
}
func (t MillisecondTimestamp) Time() time.Time {
return time.Time(t)
}
func (t *MillisecondTimestamp) UnmarshalJSON(data []byte) error {
var v interface{}
var err = json.Unmarshal(data, &v)
if err != nil {
return err
}
switch vt := v.(type) {
case string:
if vt == "" {
// treat empty string as 0
*t = MillisecondTimestamp(time.Time{})
return nil
}
f, err := strconv.ParseFloat(vt, 64)
if err == nil {
tt, err := convertFloat64ToTime(vt, f)
if err != nil {
return err
}
*t = MillisecondTimestamp(tt)
return nil
}
tt, err := time.Parse(time.RFC3339Nano, vt)
if err == nil {
*t = MillisecondTimestamp(tt)
return nil
}
return err
case float64:
str := strconv.FormatFloat(vt, 'f', -1, 64)
tt, err := convertFloat64ToTime(str, vt)
if err != nil {
return err
}
*t = MillisecondTimestamp(tt)
return nil
default:
return fmt.Errorf("can not parse %T %+v as millisecond timestamp", vt, vt)
}
// Unreachable
}
func convertFloat64ToTime(vt string, f float64) (time.Time, error) {
idx := strings.Index(vt, ".")
if idx > 0 {
vt = vt[0 : idx-1]
}
if len(vt) <= numOfDigitsOfUnixTimestamp {
return time.Unix(0, int64(f*float64(time.Second))), nil
} else if len(vt) <= numOfDigitsOfMilliSecondUnixTimestamp {
return time.Unix(0, int64(f)*int64(time.Millisecond)), nil
} else if len(vt) <= numOfDigitsOfNanoSecondsUnixTimestamp {
return time.Unix(0, int64(f)), nil
}
return time.Time{}, fmt.Errorf("the floating point value %f is out of the timestamp range", f)
}
// Time type implements the driver value for sqlite
type Time time.Time
// layout defines the time layout without timezone
// because sqlite3 and mysql does not output datetime with timezone.
//
// sqlite3 format (in UTC): 2022-06-01 11:38:45
// mysql format in (in Local): 2022-06-02 20:28:10 or 2022-06-02 20:28:10.000
var layout = "2006-01-02 15:04:05.999Z07:00"
func (t *Time) UnmarshalJSON(data []byte) error {
// fallback to RFC3339
return (*time.Time)(t).UnmarshalJSON(data)
}
func (t Time) MarshalJSON() ([]byte, error) {
return time.Time(t).MarshalJSON()
}
func (t Time) String() string {
return time.Time(t).String()
}
func (t Time) Time() time.Time {
return time.Time(t)
}
func (t Time) Unix() int64 {
return time.Time(t).Unix()
}
func (t Time) UnixMilli() int64 {
return time.Time(t).UnixMilli()
}
func (t Time) Equal(time2 time.Time) bool {
return time.Time(t).Equal(time2)
}
func (t Time) After(time2 time.Time) bool {
return time.Time(t).After(time2)
}
func (t Time) Before(time2 time.Time) bool {
return time.Time(t).Before(time2)
}
func NewTimeFromUnix(sec int64, nsec int64) Time {
return Time(time.Unix(sec, nsec))
}
// Value implements the driver.Valuer interface
// see http://jmoiron.net/blog/built-in-interfaces/
func (t Time) Value() (driver.Value, error) {
if time.Time(t) == (time.Time{}) {
return nil, nil
}
return time.Time(t), nil
}
func (t *Time) Scan(src interface{}) error {
// skip nil time
if src == nil {
return nil
}
switch d := src.(type) {
case *time.Time:
*t = Time(*d)
return nil
case time.Time:
*t = Time(d)
return nil
case string:
// 2020-12-16 05:17:12.994+08:00
tt, err := time.Parse(layout, d)
if err != nil {
return err
}
*t = Time(tt)
return nil
case []byte:
// 2019-10-20 23:01:43.77+08:00
tt, err := time.Parse(layout, string(d))
if err != nil {
return err
}
*t = Time(tt)
return nil
default:
}
return fmt.Errorf("datatype.Time scan error, type: %T is not supported, value; %+v", src, src)
}
var looseTimeFormats = []string{
time.RFC3339,
time.RFC822,
"2006-01-02T15:04:05",
"2006-01-02",
}
// LooseFormatTime parses date time string with a wide range of formats.
type LooseFormatTime time.Time
func ParseLooseFormatTime(s string) (LooseFormatTime, error) {
var t time.Time
switch s {
case "now":
t = time.Now()
return LooseFormatTime(t), nil
case "yesterday":
t = time.Now().AddDate(0, 0, -1)
return LooseFormatTime(t), nil
case "last month":
t = time.Now().AddDate(0, -1, 0)
return LooseFormatTime(t), nil
case "last 30 days":
t = time.Now().AddDate(0, 0, -30)
return LooseFormatTime(t), nil
case "last year":
t = time.Now().AddDate(-1, 0, 0)
return LooseFormatTime(t), nil
}
tv, err := ParseTimeWithFormats(s, looseTimeFormats)
if err != nil {
return LooseFormatTime{}, err
}
return LooseFormatTime(tv), nil
}
func (t *LooseFormatTime) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}
lt, err := ParseLooseFormatTime(str)
if err != nil {
return err
}
*t = lt
return nil
}
func (t *LooseFormatTime) UnmarshalJSON(data []byte) error {
var v string
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
tv, err := ParseTimeWithFormats(v, looseTimeFormats)
if err != nil {
return err
}
*t = LooseFormatTime(tv)
return nil
}
func (t LooseFormatTime) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(time.Time(t).Format(time.RFC3339))), nil
}
func (t LooseFormatTime) Time() time.Time {
return time.Time(t)
}
// Timestamp is used for parsing unix timestamp (seconds)
type Timestamp time.Time
func (t Timestamp) Format(layout string) string {
return time.Time(t).Format(layout)
}
func (t Timestamp) Time() time.Time {
return time.Time(t)
}
func (t Timestamp) String() string {
return time.Time(t).String()
}
func (t Timestamp) MarshalJSON() ([]byte, error) {
ts := time.Time(t).Unix()
return json.Marshal(ts)
}
func (t *Timestamp) UnmarshalJSON(o []byte) error {
var timestamp int64
if err := json.Unmarshal(o, ×tamp); err != nil {
return err
}
*t = Timestamp(time.Unix(timestamp, 0))
return nil
}
func ParseTimeWithFormats(strTime string, formats []string) (time.Time, error) {
for _, format := range formats {
tt, err := time.Parse(format, strTime)
if err == nil {
return tt, nil
}
}
return time.Time{}, fmt.Errorf("failed to parse time %s, valid formats are %+v", strTime, formats)
}
func BeginningOfTheDay(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func Over24Hours(since time.Time) bool {
return time.Since(since) >= 24*time.Hour
}