-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
47 lines (40 loc) · 1.22 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
package enc
import (
"time"
"github.com/shamaton/msgpack/v2/def"
)
// CalcTime check value and returns data size that need.
func (e *Encoder) CalcTime(t time.Time) int {
secs := uint64(t.Unix())
if secs>>34 == 0 {
data := uint64(t.Nanosecond())<<34 | secs
if data&0xffffffff00000000 == 0 {
return def.Byte1 + def.Byte1 + def.Byte4
}
return def.Byte1 + def.Byte1 + def.Byte8
}
return def.Byte1 + def.Byte1 + def.Byte1 + def.Byte4 + def.Byte8
}
// WriteTime sets the contents of t to the buffer.
func (e *Encoder) WriteTime(t time.Time, offset int) int {
secs := uint64(t.Unix())
if secs>>34 == 0 {
data := uint64(t.Nanosecond())<<34 | secs
if data&0xffffffff00000000 == 0 {
offset = e.setByte1Int(def.Fixext4, offset)
offset = e.setByte1Int(def.TimeStamp, offset)
offset = e.setByte4Uint64(data, offset)
return offset
}
offset = e.setByte1Int(def.Fixext8, offset)
offset = e.setByte1Int(def.TimeStamp, offset)
offset = e.setByte8Uint64(data, offset)
return offset
}
offset = e.setByte1Int(def.Ext8, offset)
offset = e.setByte1Int(12, offset)
offset = e.setByte1Int(def.TimeStamp, offset)
offset = e.setByte4Int(t.Nanosecond(), offset)
offset = e.setByte8Uint64(secs, offset)
return offset
}