-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.go
41 lines (33 loc) · 861 Bytes
/
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
package plugin
import (
"encoding/binary"
"fmt"
"time"
"github.com/vmihailenco/msgpack/v5"
)
const eventTimeBytesLen = 8
type EventTime struct {
time.Time
}
var (
_ msgpack.Marshaler = (*EventTime)(nil)
_ msgpack.Unmarshaler = (*EventTime)(nil)
)
func init() {
msgpack.RegisterExt(0, (*EventTime)(nil))
}
func (tm *EventTime) MarshalMsgpack() ([]byte, error) {
b := make([]byte, eventTimeBytesLen)
binary.BigEndian.PutUint32(b, uint32(tm.Unix()))
binary.BigEndian.PutUint32(b[4:], uint32(tm.Nanosecond()))
return b, nil
}
func (tm *EventTime) UnmarshalMsgpack(b []byte) error {
if len(b) != eventTimeBytesLen {
return fmt.Errorf("invalid data length: got %d, wanted %d", len(b), eventTimeBytesLen)
}
sec := binary.BigEndian.Uint32(b)
usec := binary.BigEndian.Uint32(b[4:])
tm.Time = time.Unix(int64(sec), int64(usec))
return nil
}