forked from chronosphereio/calyptia-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
51 lines (38 loc) · 1.15 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
package plugin
import (
"encoding/binary"
"fmt"
"time"
"github.com/ugorji/go/codec"
)
const fFimeSize = 8
var _ codec.BytesExt = (*bigEndianTime)(nil)
// bigEndianTime implements codec.BytesExt to handle custom (de)serialization of types to/from []byte.
// It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
type bigEndianTime time.Time
func (*bigEndianTime) WriteExt(v interface{}) []byte {
ft, ok := v.(*bigEndianTime)
if !ok {
panic(fmt.Sprintf("unexpected big endian time type %T", v))
}
t := time.Time(*ft).UTC()
sec := t.Unix()
nsec := t.UnixNano()
b := make([]byte, fFimeSize)
binary.BigEndian.PutUint32(b, uint32(sec))
binary.BigEndian.PutUint32(b[4:], uint32(nsec))
return b
}
func (*bigEndianTime) ReadExt(dst interface{}, src []byte) {
ft, ok := dst.(*bigEndianTime)
if !ok {
panic(fmt.Sprintf("unexpected big endian time type %T", dst))
}
if len(src) != fFimeSize {
panic(fmt.Sprintf("unexpected big endian time length %d", len(src)))
}
sec := binary.BigEndian.Uint32(src)
nsec := binary.BigEndian.Uint32(src[4:])
t := time.Unix(int64(sec), int64(nsec)).UTC()
*ft = bigEndianTime(t)
}