-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-stamp.go
71 lines (59 loc) · 1.32 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
66
67
68
69
70
71
package fleet
import (
"bytes"
"encoding/binary"
"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) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
err := binary.Write(&b, binary.BigEndian, time.Time(t).Unix())
if err != nil {
return nil, err
}
err = binary.Write(&b, binary.BigEndian, int64(time.Time(t).Nanosecond()))
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (t *DbStamp) UnmarshalBinary(data []byte) error {
// read a timestamp (inverse of MarshalBinary)
r := bytes.NewReader(data)
var ut, un int64
err := binary.Read(r, binary.BigEndian, &ut)
if err != nil {
return err
}
err = binary.Read(r, binary.BigEndian, &un)
if err != nil {
return err
}
*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)
}