-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_time_ext.go
135 lines (119 loc) · 3.88 KB
/
custom_time_ext.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
package fastjson
import (
"github.com/json-iterator/go"
"strconv"
"time"
"unsafe"
)
type CustomTimeExtension struct {
jsoniter.DummyExtension
}
func (extension *CustomTimeExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
for _, binding := range structDescriptor.Fields {
var typeErr error
var isPtr bool
typeName := binding.Field.Type().String()
if typeName == "time.Time" {
isPtr = false
} else if typeName == "*time.Time" {
isPtr = true
} else {
continue
}
formatTag := binding.Field.Tag().Get("time_format")
timeFormat := formatTag
if timeFormat == "sql_datetime" {
timeFormat = "2006-01-02 15:04:05"
} else if timeFormat == "sql_date" {
timeFormat = "2006-01-02"
}
locale := time.Local
if isUTC, _ := strconv.ParseBool(binding.Field.Tag().Get("time_utc")); isUTC {
locale = time.UTC
}
if locTag := binding.Field.Tag().Get("time_location"); locTag != "" {
loc, err := time.LoadLocation(locTag)
if err != nil {
typeErr = err
} else {
locale = loc
}
}
var isSnap bool
snapTag := binding.Field.Tag().Get("time_snap")
if snapTag == "" && (formatTag == "sql_datetime" || formatTag == "sql_date") {
isSnap = true
} else {
isSnap, _ = strconv.ParseBool(binding.Field.Tag().Get("time_snap"))
}
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
if typeErr != nil {
stream.Error = typeErr
return
}
var format string
if timeFormat == "" {
format = time.RFC3339Nano
} else {
format = timeFormat
}
var tp *time.Time
if isPtr {
tpp := (**time.Time)(ptr)
tp = *(tpp)
} else {
tp = (*time.Time)(ptr)
}
if tp != nil {
lt := tp.In(locale)
str := lt.Format(format)
if formatTag == "sql_date" && (str == "0000-01-01" || (isSnap && lt.Unix() <= 0)) {
str = "0000-00-00"
} else if formatTag == "sql_datetime" && (str == "0000-01-01 00:00:00" || (isSnap && lt.Unix() <= 0)) {
str = "0000-00-00 00:00:00"
}
stream.WriteString(str)
} else {
stream.WriteNil()
}
}}
binding.Decoder = &funcDecoder{fun: func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
if typeErr != nil {
iter.Error = typeErr
return
}
var format string
if timeFormat == "" {
format = time.RFC3339
} else {
format = timeFormat
}
str := iter.ReadString()
var t *time.Time
if str != "" {
var err error
tmp, err := time.ParseInLocation(format, str, locale)
if err != nil {
if _, ok := err.(*time.ParseError); ok {
tmp = time.Date(0, 1, 1, 0, 0, 0, 0, locale)
} else {
iter.Error = err
return
}
}
t = &tmp
} else {
t = nil
}
if isPtr {
tpp := (**time.Time)(ptr)
*tpp = t
} else {
tp := (*time.Time)(ptr)
if tp != nil && t != nil {
*tp = *t
}
}
}}
}
}