-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
35 lines (28 loc) · 811 Bytes
/
common.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
package codec
import (
"time"
structform "github.com/urso/go-structform"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/dtfmt"
)
func MakeTimestampEncoder() func(*time.Time, structform.ExtVisitor) error {
formatter, err := dtfmt.NewFormatter("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
if err != nil {
panic(err)
}
buf := make([]byte, 0, formatter.EstimateSize())
return func(t *time.Time, v structform.ExtVisitor) error {
tmp, err := formatter.AppendTo(buf, (*t).UTC())
if err != nil {
return err
}
buf = tmp[:0]
return v.OnStringRef(tmp)
}
}
func MakeBCTimestampEncoder() func(*common.Time, structform.ExtVisitor) error {
enc := MakeTimestampEncoder()
return func(t *common.Time, v structform.ExtVisitor) error {
return enc((*time.Time)(t), v)
}
}