ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

MsgPack lose time location value after encoding/decoding. #280

Closed ghts closed 5 years ago

ghts commented 5 years ago

msgPack lose location value with time.Time and return 'UTC'.

(Go 1.11.5, Windows 10 64bit)

I found this symptom after 'go get -u'.

Meanwhile, Json keep time location value, which is normal.

func TestMsgPackTime(t *testing.T) {
    msgPackHandler := new(codec.MsgpackHandle)
    msgPackHandler.WriteExt = true

    buffer := new(bytes.Buffer)

    original_value := time.Now()
    codec.NewEncoder(buffer, msgPackHandler).Encode(original_value)

    copied_value := time.Time{}
    codec.NewDecoderBytes(buffer.Bytes(), msgPackHandler).Decode(&copied_value)

    fmt.Printf("'%v' : '%v'\n", original_value.Location(), copied_value.Location())
}

This test prints < 'Local' : 'UTC' >.

I thinks this should be < 'Local' : 'Local' > just like JsonTest below.

func TestJsonPackTime(t *testing.T) {
    JsonPackHandler := new(codec.JsonHandle)

    buffer := new(bytes.Buffer)

    original_value := time.Now()
    codec.NewEncoder(buffer, JsonPackHandler).Encode(original_value)

    copied_value := time.Time{}
    codec.NewDecoderBytes(buffer.Bytes(), JsonPackHandler).Decode(&copied_value)

    fmt.Printf("'%v' : '%v'\n", original_value.Location(), copied_value.Location())
}

This test prints < 'Local' : 'Local' > as expected.

ugorji commented 5 years ago

See https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type

msgpack defines a time extension, that only stores UTC time. So this is a limitation of the format.

ghts commented 5 years ago

Got it.