timestamp.go raw
1 package types
2
3 import (
4 "bytes"
5 "io"
6
7 "next.orly.dev/pkg/lol/chk"
8 )
9
10 const TimestampLen = 8
11
12 type Timestamp struct{ val int64 }
13
14 func (ts *Timestamp) FromInt(t int) { ts.val = int64(t) }
15 func (ts *Timestamp) FromInt64(t int64) { ts.val = t }
16
17 func FromBytes(timestampBytes []byte) (ts *Timestamp, err error) {
18 v := new(Uint64)
19 if err = v.UnmarshalRead(bytes.NewBuffer(timestampBytes)); chk.E(err) {
20 return
21 }
22 ts = &Timestamp{val: int64(v.Get())}
23 return
24 }
25
26 func (ts *Timestamp) ToTimestamp() (timestamp int64) {
27 return ts.val
28 }
29 func (ts *Timestamp) Bytes() (b []byte, err error) {
30 v := new(Uint64)
31 v.Set(uint64(ts.val))
32 buf := new(bytes.Buffer)
33 if err = v.MarshalWrite(buf); chk.E(err) {
34 return
35 }
36 b = buf.Bytes()
37 return
38 }
39
40 func (ts *Timestamp) MarshalWrite(w io.Writer) (err error) {
41 v := new(Uint64)
42 v.Set(uint64(ts.val))
43 if err = v.MarshalWrite(w); chk.E(err) {
44 return
45 }
46 return
47 }
48
49 func (ts *Timestamp) UnmarshalRead(r io.Reader) (err error) {
50 v := new(Uint64)
51 if err = v.UnmarshalRead(r); chk.E(err) {
52 return
53 }
54 ts.val = int64(v.Get())
55 return
56 }
57