-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
71 lines (56 loc) · 1.83 KB
/
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
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 tempo
import (
"encoding/binary"
)
const (
range0 byte = 0
range64 byte = 1 << 7
range256 byte = 1 << 6
range2048 byte = 1 << 5
rangeOther byte = 1 << 4
)
// Maps time ranges to the number of bits used to store the time
var timeBytes = map[byte]int{
range0: 0,
range64: 1, // when decoding, cast time as int8
range256: 2, // when decoding, cast time as int16
range2048: 2, // when decoding, cast time as int16
rangeOther: 4, // when decoding, cast time as int32
}
func (ts *TimeSeries) timeEncode(time int64) {
if ts.Opened.Length == 0 {
dod := time - ts.TimeHeader
dodBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(dodBytes, uint16(dod))
ts.Opened.Stream = append(ts.Opened.Stream, dodBytes...)
ts.SecondLatestTime = ts.TimeHeader
ts.LatestTime = time
return
}
dod := (time - ts.LatestTime) - (ts.LatestTime - ts.SecondLatestTime)
if dod == 0 {
ts.Opened.Stream = append(ts.Opened.Stream, 0)
} else if (-63 <= dod) && (dod <= 64) {
tagAndValue := make([]byte, 1)
tagAndValue[0] = range64
putInt64(&tagAndValue, dod, timeBytes[range64])
ts.Opened.Stream = append(ts.Opened.Stream, tagAndValue...)
} else if (-255 <= dod) && (dod <= 256) {
tagAndValue := make([]byte, 1)
tagAndValue[0] = range256
putInt64(&tagAndValue, dod, timeBytes[range256])
ts.Opened.Stream = append(ts.Opened.Stream, tagAndValue...)
} else if (-2047 <= dod) && (dod <= 2048) {
tagAndValue := make([]byte, 1)
tagAndValue[0] = range2048
putInt64(&tagAndValue, dod, timeBytes[range2048])
ts.Opened.Stream = append(ts.Opened.Stream, tagAndValue...)
} else {
tagAndValue := make([]byte, 1)
tagAndValue[0] = rangeOther
putInt64(&tagAndValue, dod, timeBytes[rangeOther])
ts.Opened.Stream = append(ts.Opened.Stream, tagAndValue...)
}
ts.SecondLatestTime = ts.LatestTime
ts.LatestTime = time
}