-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
175 lines (154 loc) · 3.81 KB
/
encoding.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
package time
import (
"strconv"
"time"
)
type EncodeType int8
const (
EncodeTypeLayout EncodeType = iota
EncodeTypeUnixSeconds
EncodeTypeUnixMilliseconds
EncodeTypeUnixMicroseconds
EncodeTypeUnixNanoseconds
)
func SetJsonType(typ EncodeType) {
encoding.SetJsonType(typ)
}
func SetLayout(l string) {
encoding.SetLayout(l)
}
var encoding = &Encoding{
Layout: time.RFC3339Nano,
}
func MarshalJSON(t time.Time) ([]byte, error) {
return encoding.marshalJSON(t)
}
func UnmarshalJSON(t *time.Time, data []byte) error {
return encoding.unmarshalJSON(t, data)
}
func MarshalText(t time.Time) ([]byte, error) {
return encoding.marshalText(t)
}
func UnmarshalText(t *time.Time, data []byte) error {
return encoding.unmarshalText(t, data)
}
type Encoding struct {
EncodeType
Layout string
}
func (u *Encoding) SetJsonType(typ EncodeType) {
u.EncodeType = typ
}
func (u *Encoding) SetLayout(l string) {
u.Layout = l
}
func (u *Encoding) marshalText(t time.Time) ([]byte, error) {
switch u.EncodeType {
case EncodeTypeLayout:
if u.Layout == time.RFC3339Nano {
return t.MarshalText()
}
return []byte(t.Format(u.Layout)), nil
case EncodeTypeUnixSeconds:
return strconv.AppendInt(nil, t.Unix(), 10), nil
case EncodeTypeUnixMilliseconds:
return strconv.AppendInt(nil, t.UnixMilli(), 10), nil
case EncodeTypeUnixMicroseconds:
return strconv.AppendInt(nil, t.UnixMicro(), 10), nil
case EncodeTypeUnixNanoseconds:
return strconv.AppendInt(nil, t.UnixNano(), 10), nil
}
return t.MarshalText()
}
func (u *Encoding) unmarshalText(t *time.Time, data []byte) error {
tstr := string(data)
if tstr == "" {
return nil
}
if u.EncodeType == EncodeTypeLayout {
data = data[1 : len(data)-1]
if u.Layout == time.RFC3339Nano {
return t.UnmarshalText(data)
} else {
var err error
*t, err = time.ParseInLocation(u.Layout, string(data), time.Local)
return err
}
} else {
parseInt, err := strconv.ParseInt(tstr, 10, 64)
if err != nil {
return err
}
switch u.EncodeType {
case EncodeTypeUnixSeconds:
*t = time.Unix(parseInt, 0)
return nil
case EncodeTypeUnixMilliseconds:
*t = time.UnixMilli(parseInt)
return nil
case EncodeTypeUnixMicroseconds:
*t = time.UnixMicro(parseInt)
return nil
case EncodeTypeUnixNanoseconds:
*t = time.Unix(0, parseInt)
return nil
}
}
return t.UnmarshalText(data)
}
func (u *Encoding) marshalJSON(t time.Time) ([]byte, error) {
if u.EncodeType == EncodeTypeLayout {
if u.Layout == time.RFC3339Nano {
return t.MarshalJSON()
}
return []byte(`"` + t.Format(u.Layout) + `"`), nil
} else {
switch u.EncodeType {
case EncodeTypeUnixSeconds:
return strconv.AppendInt(nil, t.Unix(), 10), nil
case EncodeTypeUnixMilliseconds:
return strconv.AppendInt(nil, t.UnixMilli(), 10), nil
case EncodeTypeUnixMicroseconds:
return strconv.AppendInt(nil, t.UnixMicro(), 10), nil
case EncodeTypeUnixNanoseconds:
return strconv.AppendInt(nil, t.UnixNano(), 10), nil
}
}
return t.MarshalJSON()
}
func (u *Encoding) unmarshalJSON(t *time.Time, data []byte) error {
tstr := string(data)
if tstr == "null" {
return nil
}
if u.EncodeType == EncodeTypeLayout {
data = data[1 : len(data)-1]
if u.Layout == time.RFC3339Nano {
return t.UnmarshalJSON(data)
} else {
var err error
*t, err = time.ParseInLocation(`"`+u.Layout+`"`, string(data), time.Local)
return err
}
} else {
parseInt, err := strconv.ParseInt(tstr, 10, 64)
if err != nil {
return err
}
switch u.EncodeType {
case EncodeTypeUnixSeconds:
*t = time.Unix(parseInt, 0)
return nil
case EncodeTypeUnixMilliseconds:
*t = time.UnixMilli(parseInt)
return nil
case EncodeTypeUnixMicroseconds:
*t = time.UnixMicro(parseInt)
return nil
case EncodeTypeUnixNanoseconds:
*t = time.Unix(0, parseInt)
return nil
}
}
return t.UnmarshalJSON(data)
}