Files
QSfera/vendor/github.com/shamaton/msgpack/v2/time/decode_stream.go
T
dependabot[bot]andRalf Haferkamp b3e5d80306 chore(deps): bump github.com/shamaton/msgpack/v2 from 2.2.0 to 2.2.2
Bumps [github.com/shamaton/msgpack/v2](https://github.com/shamaton/msgpack) from 2.2.0 to 2.2.2.
- [Release notes](https://github.com/shamaton/msgpack/releases)
- [Commits](https://github.com/shamaton/msgpack/compare/v2.2.0...v2.2.2)

---
updated-dependencies:
- dependency-name: github.com/shamaton/msgpack/v2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-09-23 12:57:52 +02:00

57 lines
1.4 KiB
Go

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:
return time.Unix(int64(binary.BigEndian.Uint32(data)), 0), 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)
}
return time.Unix(int64(data64&0x00000003ffffffff), nano), 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])
return time.Unix(int64(sec), int64(nano)), nil
}
return zero, fmt.Errorf("should not reach this line!! code %x decoding %v", code, k)
}