package msgpack
import (
)
var timeExtID int8 = -1
func () {
RegisterExtEncoder(timeExtID, time.Time{}, timeEncoder)
RegisterExtDecoder(timeExtID, time.Time{}, timeDecoder)
}
func ( *Encoder, reflect.Value) ([]byte, error) {
return .encodeTime(.Interface().(time.Time)), nil
}
func ( *Decoder, reflect.Value, int) error {
, := .decodeTime()
if != nil {
return
}
:= .Addr().Interface().(*time.Time)
* =
return nil
}
func ( *Encoder) ( time.Time) error {
:= .encodeTime()
if := .encodeExtLen(len()); != nil {
return
}
if := .w.WriteByte(byte(timeExtID)); != nil {
return
}
return .write()
}
func ( *Encoder) ( time.Time) []byte {
if .timeBuf == nil {
.timeBuf = make([]byte, 12)
}
:= uint64(.Unix())
if >>34 == 0 {
:= uint64(.Nanosecond())<<34 |
if &0xffffffff00000000 == 0 {
:= .timeBuf[:4]
binary.BigEndian.PutUint32(, uint32())
return
}
:= .timeBuf[:8]
binary.BigEndian.PutUint64(, )
return
}
:= .timeBuf[:12]
binary.BigEndian.PutUint32(, uint32(.Nanosecond()))
binary.BigEndian.PutUint64([4:], )
return
}
func ( *Decoder) () (time.Time, error) {
, := .readCode()
if != nil {
return time.Time{},
}
if == msgpcode.FixedArrayLow|2 {
, := .DecodeInt64()
if != nil {
return time.Time{},
}
, := .DecodeInt64()
if != nil {
return time.Time{},
}
return time.Unix(, ), nil
}
if msgpcode.IsString() {
, := .string()
if != nil {
return time.Time{},
}
return time.Parse(time.RFC3339Nano, )
}
, , := .extHeader()
if != nil {
return time.Time{},
}
if != timeExtID {
return time.Time{}, fmt.Errorf("msgpack: invalid time ext id=%d", )
}
, := .decodeTime()
if != nil {
return ,
}
if .IsZero() {
return .UTC(), nil
}
return , nil
}
func ( *Decoder) ( int) (time.Time, error) {
, := .readN()
if != nil {
return time.Time{},
}
switch len() {
case 4:
:= binary.BigEndian.Uint32()
return time.Unix(int64(), 0), nil
case 8:
:= binary.BigEndian.Uint64()
:= int64( >> 34)
&= 0x00000003ffffffff
return time.Unix(int64(), ), nil
case 12:
:= binary.BigEndian.Uint32()
:= binary.BigEndian.Uint64([4:])
return time.Unix(int64(), int64()), nil
default:
= fmt.Errorf("msgpack: invalid ext len=%d decoding time", )
return time.Time{},
}
}