-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.go
253 lines (209 loc) · 6.37 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
package plenccodec
import (
"fmt"
"time"
"unsafe"
"github.com/philpearl/plenc/plenccore"
)
// ptime is a representation of time in UTC. It is used to encode time.Time
type ptime struct {
Seconds int64 `plenc:"1"`
Nanoseconds int32 `plenc:"2"`
}
// Set sets the time from a time.Time
func (e *ptime) Set(t time.Time) {
e.Seconds = t.Unix()
n := t.Nanosecond()
e.Nanoseconds = int32(n)
}
func (e *ptime) Standard() time.Time {
return time.Unix(e.Seconds, int64(e.Nanoseconds)).UTC()
}
// TimeCodec is a codec for Time
// Note that this is not compatible with google.protobuf.Timestamp. The structure
// is the same, but they don't use zigzag encoding for the fields. They still do
// allow negative values though
type TimeCodec struct{}
// size returns the number of bytes needed to encode a Time
func (tc TimeCodec) size(ptr unsafe.Pointer) (size int) {
t := *(*time.Time)(ptr)
var e ptime
e.Set(t)
return IntCodec[int64]{}.Size(unsafe.Pointer(&e.Seconds), varInt1Tag) +
IntCodec[int32]{}.Size(unsafe.Pointer(&e.Nanoseconds), varInt2Tag)
}
var (
varInt1Tag = plenccore.AppendTag(nil, plenccore.WTVarInt, 1)
varInt2Tag = plenccore.AppendTag(nil, plenccore.WTVarInt, 2)
)
// append encodes a Time
func (tc TimeCodec) append(data []byte, ptr unsafe.Pointer) []byte {
t := *(*time.Time)(ptr)
var e ptime
e.Set(t)
data = IntCodec[int64]{}.Append(data, unsafe.Pointer(&e.Seconds), varInt1Tag)
data = IntCodec[int32]{}.Append(data, unsafe.Pointer(&e.Nanoseconds), varInt2Tag)
return data
}
// Read decodes a Time
func (tc TimeCodec) Read(data []byte, ptr unsafe.Pointer, wt plenccore.WireType) (n int, err error) {
l := len(data)
if l == 0 {
*(*time.Time)(ptr) = time.Time{}
return 0, nil
}
var e ptime
var offset int
for offset < l {
wt, index, n := plenccore.ReadTag(data[offset:])
offset += n
switch index {
case 1:
n, err := IntCodec[int64]{}.Read(data[offset:], unsafe.Pointer(&e.Seconds), wt)
if err != nil {
return 0, fmt.Errorf("failed reading seconds field of time. %w", err)
}
offset += n
case 2:
n, err := IntCodec[int32]{}.Read(data[offset:], unsafe.Pointer(&e.Nanoseconds), wt)
if err != nil {
return 0, fmt.Errorf("failed reading nanoseconds field of time. %w", err)
}
offset += n
default:
// Field corresponding to index does not exist
n, err := plenccore.Skip(data[offset:], wt)
if err != nil {
return 0, fmt.Errorf("failed to skip field %d of time. %w", index, err)
}
offset += n
}
}
*(*time.Time)(ptr) = e.Standard()
return offset, nil
}
func (TimeCodec) New() unsafe.Pointer {
return unsafe.Pointer(&time.Time{})
}
func (TimeCodec) Omit(ptr unsafe.Pointer) bool {
return (*time.Time)(ptr).IsZero()
}
func (tc TimeCodec) WireType() plenccore.WireType {
return plenccore.WTLength
}
func (tc TimeCodec) Descriptor() Descriptor {
return Descriptor{Type: FieldTypeTime, LogicalType: LogicalTypeTimestamp}
}
func (c TimeCodec) Size(ptr unsafe.Pointer, tag []byte) int {
l := c.size(ptr)
if len(tag) != 0 {
l += len(tag) + plenccore.SizeVarUint(uint64(l))
}
return l
}
func (c TimeCodec) Append(data []byte, ptr unsafe.Pointer, tag []byte) []byte {
if len(tag) != 0 {
data = append(data, tag...)
data = plenccore.AppendVarUint(data, uint64(c.size(ptr)))
}
return c.append(data, ptr)
}
type TimeCompatCodec struct {
TimeCodec
}
// size returns the number of bytes needed to encode a Time
func (tc TimeCompatCodec) size(ptr unsafe.Pointer) (size int) {
t := *(*time.Time)(ptr)
var e ptime
e.Set(t)
return UintCodec[uint64]{}.Size(unsafe.Pointer(&e.Seconds), varInt1Tag) +
+UintCodec[uint32]{}.Size(unsafe.Pointer(&e.Nanoseconds), varInt2Tag)
}
// append encodes a Time
func (tc TimeCompatCodec) append(data []byte, ptr unsafe.Pointer) []byte {
t := *(*time.Time)(ptr)
var e ptime
e.Set(t)
data = UintCodec[uint64]{}.Append(data, unsafe.Pointer(&e.Seconds), varInt1Tag)
data = UintCodec[uint32]{}.Append(data, unsafe.Pointer(&e.Nanoseconds), varInt2Tag)
return data
}
func (c TimeCompatCodec) Size(ptr unsafe.Pointer, tag []byte) int {
l := c.size(ptr)
if len(tag) != 0 {
l += len(tag) + plenccore.SizeVarUint(uint64(l))
}
return l
}
func (c TimeCompatCodec) Append(data []byte, ptr unsafe.Pointer, tag []byte) []byte {
if len(tag) != 0 {
data = append(data, tag...)
data = plenccore.AppendVarUint(data, uint64(c.size(ptr)))
}
return c.append(data, ptr)
}
// Read decodes a Time
func (tc TimeCompatCodec) Read(data []byte, ptr unsafe.Pointer, wt plenccore.WireType) (n int, err error) {
l := len(data)
if l == 0 {
*(*time.Time)(ptr) = time.Time{}
return 0, nil
}
var e ptime
var offset int
for offset < l {
wt, index, n := plenccore.ReadTag(data[offset:])
offset += n
switch index {
case 1:
n, err := UintCodec[uint64]{}.Read(data[offset:], unsafe.Pointer(&e.Seconds), wt)
if err != nil {
return 0, fmt.Errorf("failed reading seconds field of time. %w", err)
}
offset += n
case 2:
n, err := UintCodec[uint32]{}.Read(data[offset:], unsafe.Pointer(&e.Nanoseconds), wt)
if err != nil {
return 0, fmt.Errorf("failed reading nanoseconds field of time. %w", err)
}
offset += n
default:
// Field corresponding to index does not exist
n, err := plenccore.Skip(data[offset:], wt)
if err != nil {
return 0, fmt.Errorf("failed to skip field %d of time. %w", index, err)
}
offset += n
}
}
*(*time.Time)(ptr) = e.Standard()
return offset, nil
}
// BQTimestampStruct encodes a time.Time as a flat (not zigzag) int64 of
// microseconds since the Epoch. This is how the BigQuery write API expects a
// timestamp to be encoded
type BQTimestampCodec struct {
FlatIntCodec[uint64]
}
func (BQTimestampCodec) New() unsafe.Pointer {
return unsafe.Pointer(&time.Time{})
}
func (BQTimestampCodec) Omit(ptr unsafe.Pointer) bool {
return (*time.Time)(ptr).IsZero()
}
func (c BQTimestampCodec) Read(data []byte, ptr unsafe.Pointer, wt plenccore.WireType) (n int, err error) {
var ts int64
n, err = c.FlatIntCodec.Read(data, unsafe.Pointer(&ts), wt)
if err != nil {
return n, err
}
*(*time.Time)(ptr) = time.UnixMicro(ts).UTC()
return n, nil
}
func (c BQTimestampCodec) Append(data []byte, ptr unsafe.Pointer, tag []byte) []byte {
ts := (*time.Time)(ptr).UnixMicro()
return c.FlatIntCodec.Append(data, unsafe.Pointer(&ts), tag)
}
func (c BQTimestampCodec) Descriptor() Descriptor {
return Descriptor{Type: FieldTypeFlatInt, LogicalType: LogicalTypeTimestamp}
}