/
time.go
45 lines (36 loc) · 988 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
42
43
44
45
package inter
import (
"time"
"github.com/Fantom-foundation/lachesis-ex/common/bigendian"
)
type (
// Timestamp is a logical time.
Timestamp uint64
)
// Bytes gets the byte representation of the index.
func (t Timestamp) Bytes() []byte {
return bigendian.Int64ToBytes(uint64(t))
}
// BytesToTimestamp converts bytes to timestamp.
func BytesToTimestamp(b []byte) Timestamp {
return Timestamp(bigendian.BytesToInt64(b))
}
func FromUnix(t int64) Timestamp {
return Timestamp(int64(t) * int64(time.Second))
}
// Unix returns t as a Unix time, the number of seconds elapsed
// since January 1, 1970 UTC. The result does not depend on the
// location associated with t.
func (t Timestamp) Unix() int64 {
return int64(t) / int64(time.Second)
}
func (t Timestamp) Time() time.Time {
return time.Unix(int64(t)/int64(time.Second), int64(t)%int64(time.Second))
}
// MaxTimestamp return max value.
func MaxTimestamp(x, y Timestamp) Timestamp {
if x > y {
return x
}
return y
}