-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestamp.go
50 lines (40 loc) · 1013 Bytes
/
timestamp.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package picoconv
import (
"time"
"storj.io/picobuf"
)
// Timestamp implements protobuf timestamp conversion to standard time.Time.
type Timestamp time.Time
// PicoEncode implements custom encoding function.
func (t *Timestamp) PicoEncode(c *picobuf.Encoder, field picobuf.FieldNumber) bool {
if t == nil {
return false
}
z := time.Time(*t)
if z.IsZero() {
return false
}
seconds := z.Unix()
nanos := int32(z.Nanosecond())
c.Message(field, func(c *picobuf.Encoder) bool {
c.Int64(1, &seconds)
c.Int32(2, &nanos)
return true
})
return true
}
// PicoDecode implements custom decoding function.
func (t *Timestamp) PicoDecode(c *picobuf.Decoder, field picobuf.FieldNumber) {
if c.PendingField() != field {
return
}
var seconds int64
var nanos int32
c.Message(field, func(c *picobuf.Decoder) {
c.Int64(1, &seconds)
c.Int32(2, &nanos)
})
*t = Timestamp(time.Unix(seconds, int64(nanos)).UTC())
}