switch to go vendoring
This commit is contained in:
+75
@@ -0,0 +1,75 @@
|
||||
package time
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/shamaton/msgpack/v2/def"
|
||||
"github.com/shamaton/msgpack/v2/ext"
|
||||
)
|
||||
|
||||
var zero = time.Unix(0, 0)
|
||||
|
||||
var Decoder = new(timeDecoder)
|
||||
|
||||
type timeDecoder struct {
|
||||
ext.DecoderCommon
|
||||
}
|
||||
|
||||
func (td *timeDecoder) Code() int8 {
|
||||
return def.TimeStamp
|
||||
}
|
||||
|
||||
func (td *timeDecoder) IsType(offset int, d *[]byte) bool {
|
||||
code, offset := td.ReadSize1(offset, d)
|
||||
|
||||
if code == def.Fixext4 {
|
||||
t, _ := td.ReadSize1(offset, d)
|
||||
return int8(t) == td.Code()
|
||||
} else if code == def.Fixext8 {
|
||||
t, _ := td.ReadSize1(offset, d)
|
||||
return int8(t) == td.Code()
|
||||
} else if code == def.Ext8 {
|
||||
l, offset := td.ReadSize1(offset, d)
|
||||
t, _ := td.ReadSize1(offset, d)
|
||||
return l == 12 && int8(t) == td.Code()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (td *timeDecoder) AsValue(offset int, k reflect.Kind, d *[]byte) (interface{}, int, error) {
|
||||
code, offset := td.ReadSize1(offset, d)
|
||||
|
||||
switch code {
|
||||
case def.Fixext4:
|
||||
_, offset = td.ReadSize1(offset, d)
|
||||
bs, offset := td.ReadSize4(offset, d)
|
||||
return time.Unix(int64(binary.BigEndian.Uint32(bs)), 0), offset, nil
|
||||
|
||||
case def.Fixext8:
|
||||
_, offset = td.ReadSize1(offset, d)
|
||||
bs, offset := td.ReadSize8(offset, d)
|
||||
data64 := binary.BigEndian.Uint64(bs)
|
||||
nano := int64(data64 >> 34)
|
||||
if nano > 999999999 {
|
||||
return zero, 0, fmt.Errorf("In timestamp 64 formats, nanoseconds must not be larger than 999999999 : %d", nano)
|
||||
}
|
||||
return time.Unix(int64(data64&0x00000003ffffffff), nano), offset, nil
|
||||
|
||||
case def.Ext8:
|
||||
_, offset = td.ReadSize1(offset, d)
|
||||
_, offset = td.ReadSize1(offset, d)
|
||||
nanobs, offset := td.ReadSize4(offset, d)
|
||||
secbs, offset := td.ReadSize8(offset, d)
|
||||
nano := binary.BigEndian.Uint32(nanobs)
|
||||
if nano > 999999999 {
|
||||
return zero, 0, fmt.Errorf("In timestamp 96 formats, nanoseconds must not be larger than 999999999 : %d", nano)
|
||||
}
|
||||
sec := binary.BigEndian.Uint64(secbs)
|
||||
return time.Unix(int64(sec), int64(nano)), offset, nil
|
||||
}
|
||||
|
||||
return zero, 0, fmt.Errorf("should not reach this line!! code %x decoding %v", code, k)
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package time
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/shamaton/msgpack/v2/def"
|
||||
"github.com/shamaton/msgpack/v2/ext"
|
||||
)
|
||||
|
||||
var Encoder = new(timeEncoder)
|
||||
|
||||
type timeEncoder struct {
|
||||
ext.EncoderCommon
|
||||
}
|
||||
|
||||
var typeOf = reflect.TypeOf(time.Time{})
|
||||
|
||||
func (td *timeEncoder) Code() int8 {
|
||||
return def.TimeStamp
|
||||
}
|
||||
|
||||
func (s *timeEncoder) Type() reflect.Type {
|
||||
return typeOf
|
||||
}
|
||||
|
||||
func (s *timeEncoder) CalcByteSize(value reflect.Value) (int, error) {
|
||||
t := value.Interface().(time.Time)
|
||||
secs := uint64(t.Unix())
|
||||
if secs>>34 == 0 {
|
||||
data := uint64(t.Nanosecond())<<34 | secs
|
||||
if data&0xffffffff00000000 == 0 {
|
||||
return def.Byte1 + def.Byte4, nil
|
||||
}
|
||||
return def.Byte1 + def.Byte8, nil
|
||||
}
|
||||
|
||||
return def.Byte1 + def.Byte1 + def.Byte4 + def.Byte8, nil
|
||||
}
|
||||
|
||||
func (s *timeEncoder) WriteToBytes(value reflect.Value, offset int, bytes *[]byte) int {
|
||||
t := value.Interface().(time.Time)
|
||||
|
||||
secs := uint64(t.Unix())
|
||||
if secs>>34 == 0 {
|
||||
data := uint64(t.Nanosecond())<<34 | secs
|
||||
if data&0xffffffff00000000 == 0 {
|
||||
offset = s.SetByte1Int(def.Fixext4, offset, bytes)
|
||||
offset = s.SetByte1Int(def.TimeStamp, offset, bytes)
|
||||
offset = s.SetByte4Uint64(data, offset, bytes)
|
||||
return offset
|
||||
}
|
||||
|
||||
offset = s.SetByte1Int(def.Fixext8, offset, bytes)
|
||||
offset = s.SetByte1Int(def.TimeStamp, offset, bytes)
|
||||
offset = s.SetByte8Uint64(data, offset, bytes)
|
||||
return offset
|
||||
}
|
||||
|
||||
offset = s.SetByte1Int(def.Ext8, offset, bytes)
|
||||
offset = s.SetByte1Int(12, offset, bytes)
|
||||
offset = s.SetByte1Int(def.TimeStamp, offset, bytes)
|
||||
offset = s.SetByte4Int(t.Nanosecond(), offset, bytes)
|
||||
offset = s.SetByte8Uint64(secs, offset, bytes)
|
||||
return offset
|
||||
}
|
||||
Reference in New Issue
Block a user