-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
46 lines (40 loc) · 1.13 KB
/
helpers.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
package uuid
import (
"encoding/binary"
"math"
"time"
)
// encodeDecimal takes nanoseconds and converts them to the binary-encoded arbitrary-precision
// byte array.
func encodeDecimal(sec float64, bits int) (val []byte, err error) {
len := int(math.Log10(sec)) + 1
sec = sec / math.Pow10(len)
num := math.Pow(2, float64(bits))
var part uint64 = uint64(sec * float64(num))
val = make([]byte, 8)
binary.BigEndian.PutUint64(val, part)
return val, nil
}
// decodeDecimal decodes an array of bits into a decimal
func decodeDecimal(val []byte, bits int) (sec float64, err error) {
num := math.Pow(2, float64(bits))
ss := float64(toUint64(val)) / float64(num)
return ss, nil
}
// toUint64 converts []byte to uint64
func toUint64(data []byte) uint64 {
var arr [8]byte
copy(arr[len(arr)-len(data):], data)
return binary.BigEndian.Uint64(arr[:])
}
// toUint64 converts []byte to uint64
func toBytes(data uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(data))
return b
}
func timeToBytes(t time.Time) []byte {
tsBytes := make([]byte, 8)
binary.BigEndian.PutUint64(tsBytes, uint64(t.Unix()))
return tsBytes
}