Initial QSfera import
This commit is contained in:
+89
@@ -0,0 +1,89 @@
|
||||
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
|
||||
}
|
||||
|
||||
var _ ext.Decoder = (*timeDecoder)(nil)
|
||||
|
||||
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)
|
||||
v := time.Unix(int64(binary.BigEndian.Uint32(bs)), 0)
|
||||
if decodeAsLocal {
|
||||
return v, offset, nil
|
||||
}
|
||||
return v.UTC(), 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)
|
||||
}
|
||||
v := time.Unix(int64(data64&0x00000003ffffffff), nano)
|
||||
if decodeAsLocal {
|
||||
return v, offset, nil
|
||||
}
|
||||
return v.UTC(), 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)
|
||||
v := time.Unix(int64(sec), int64(nano))
|
||||
if decodeAsLocal {
|
||||
return v, offset, nil
|
||||
}
|
||||
return v.UTC(), offset, nil
|
||||
}
|
||||
|
||||
return zero, 0, fmt.Errorf("should not reach this line!! code %x decoding %v", code, k)
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package time
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/shamaton/msgpack/v2/def"
|
||||
"github.com/shamaton/msgpack/v2/ext"
|
||||
)
|
||||
|
||||
var StreamDecoder = new(timeStreamDecoder)
|
||||
|
||||
type timeStreamDecoder struct{}
|
||||
|
||||
var _ ext.StreamDecoder = (*timeStreamDecoder)(nil)
|
||||
|
||||
func (td *timeStreamDecoder) Code() int8 {
|
||||
return def.TimeStamp
|
||||
}
|
||||
|
||||
func (td *timeStreamDecoder) IsType(code byte, innerType int8, dataLength int) bool {
|
||||
switch code {
|
||||
case def.Fixext4, def.Fixext8:
|
||||
return innerType == td.Code()
|
||||
case def.Ext8:
|
||||
return innerType == td.Code() && dataLength == 12
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (td *timeStreamDecoder) ToValue(code byte, data []byte, k reflect.Kind) (interface{}, error) {
|
||||
switch code {
|
||||
case def.Fixext4:
|
||||
v := time.Unix(int64(binary.BigEndian.Uint32(data)), 0)
|
||||
if decodeAsLocal {
|
||||
return v, nil
|
||||
}
|
||||
return v.UTC(), nil
|
||||
|
||||
case def.Fixext8:
|
||||
data64 := binary.BigEndian.Uint64(data)
|
||||
nano := int64(data64 >> 34)
|
||||
if nano > 999999999 {
|
||||
return zero, fmt.Errorf("in timestamp 64 formats, nanoseconds must not be larger than 999999999 : %d", nano)
|
||||
}
|
||||
v := time.Unix(int64(data64&0x00000003ffffffff), nano)
|
||||
if decodeAsLocal {
|
||||
return v, nil
|
||||
}
|
||||
return v.UTC(), nil
|
||||
|
||||
case def.Ext8:
|
||||
nano := binary.BigEndian.Uint32(data[:4])
|
||||
if nano > 999999999 {
|
||||
return zero, fmt.Errorf("in timestamp 96 formats, nanoseconds must not be larger than 999999999 : %d", nano)
|
||||
}
|
||||
sec := binary.BigEndian.Uint64(data[4:12])
|
||||
v := time.Unix(int64(sec), int64(nano))
|
||||
if decodeAsLocal {
|
||||
return v, nil
|
||||
}
|
||||
return v.UTC(), nil
|
||||
}
|
||||
|
||||
return zero, 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.Byte1 + def.Byte4, nil
|
||||
}
|
||||
return def.Byte1 + def.Byte1 + def.Byte8, nil
|
||||
}
|
||||
|
||||
return def.Byte1 + 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
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package time
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/shamaton/msgpack/v2/def"
|
||||
"github.com/shamaton/msgpack/v2/ext"
|
||||
)
|
||||
|
||||
var StreamEncoder = new(timeStreamEncoder)
|
||||
|
||||
type timeStreamEncoder struct{}
|
||||
|
||||
var _ ext.StreamEncoder = (*timeStreamEncoder)(nil)
|
||||
|
||||
func (timeStreamEncoder) Code() int8 {
|
||||
return def.TimeStamp
|
||||
}
|
||||
|
||||
func (timeStreamEncoder) Type() reflect.Type {
|
||||
return typeOf
|
||||
}
|
||||
|
||||
func (e timeStreamEncoder) Write(w ext.StreamWriter, value reflect.Value) error {
|
||||
t := value.Interface().(time.Time)
|
||||
|
||||
secs := uint64(t.Unix())
|
||||
if secs>>34 == 0 {
|
||||
data := uint64(t.Nanosecond())<<34 | secs
|
||||
if data&0xffffffff00000000 == 0 {
|
||||
if err := w.WriteByte1Int(def.Fixext4); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte1Int(def.TimeStamp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte4Uint64(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := w.WriteByte1Int(def.Fixext8); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte1Int(def.TimeStamp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte8Uint64(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := w.WriteByte1Int(def.Ext8); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte1Int(12); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte1Int(def.TimeStamp); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte4Int(t.Nanosecond()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteByte8Uint64(secs); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package time
|
||||
|
||||
var decodeAsLocal = true
|
||||
|
||||
// SetDecodedAsLocal sets the decoded time to local time.
|
||||
func SetDecodedAsLocal(b bool) {
|
||||
decodeAsLocal = b
|
||||
}
|
||||
Reference in New Issue
Block a user