-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-stamp.go
65 lines (51 loc) · 1.28 KB
/
db-stamp.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
package fleet
import (
"encoding/binary"
"errors"
"time"
)
// a timestamp for db
type DbStamp time.Time
func DbNow() DbStamp {
return DbStamp(time.Now())
}
func DbZero() DbStamp {
return DbStamp(time.Unix(0, 0))
}
func (t DbStamp) Unix() int64 {
return time.Time(t).Unix()
}
func (t DbStamp) UnixNano() int64 {
return time.Time(t).UnixNano()
}
func (t DbStamp) String() string {
return time.Time(t).String()
}
func (t DbStamp) Bytes() []byte {
r := make([]byte, 16)
binary.BigEndian.PutUint64(r[:8], uint64(time.Time(t).Unix()))
binary.BigEndian.PutUint64(r[8:], uint64(time.Time(t).Nanosecond()))
return r
}
func (t DbStamp) MarshalBinary() ([]byte, error) {
return t.Bytes(), nil
}
func (t *DbStamp) UnmarshalBinary(data []byte) error {
// read a timestamp (inverse of MarshalBinary)
if len(data) != 16 {
return errors.New("invalid DbStamp unmarshal: bad data length")
}
ut := int64(binary.BigEndian.Uint64(data[:8]))
un := int64(binary.BigEndian.Uint64(data[8:]))
*t = DbStamp(time.Unix(ut, un))
return nil
}
func (t DbStamp) After(t2 DbStamp) bool {
return time.Time(t).After(time.Time(t2))
}
func (t DbStamp) GobEncode() ([]byte, error) {
return time.Time(t).GobEncode()
}
func (t *DbStamp) GobDecode(data []byte) error {
return (*time.Time)(t).GobDecode(data)
}