-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
58 lines (42 loc) · 906 Bytes
/
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
package unmarshal
import (
"time"
"github.com/buger/jsonparser"
)
type Time time.Time
func (i *Time) UnmarshalJSON(b []byte) error {
var data, dataType, _, err = jsonparser.Get(b)
if err != nil {
return err
}
if len(data) == 0 {
*i = Time{}
return nil
}
var str = string(data)
switch dataType {
case jsonparser.Number, jsonparser.String:
var t time.Time
strLen := len(str)
var dur time.Duration
if strLen < 12 {
dur, err = time.ParseDuration(str + "s") // Second
} else if strLen < 15 {
dur, err = time.ParseDuration(str + "ms") // Milli
} else if strLen < 18 {
dur, err = time.ParseDuration(str + "us") // Micro
} else {
dur, err = time.ParseDuration(str + "ns") // Nano
}
if err != nil {
return err
}
t.Add(dur)
*i = Time(t)
return nil
case jsonparser.Null:
*i = Time{}
return nil
}
return newError(dataType, "time.Time")
}