diff --git a/msgpack_test.go b/msgpack_test.go index 1b77027..ce30fa2 100644 --- a/msgpack_test.go +++ b/msgpack_test.go @@ -247,6 +247,33 @@ func TestEmbedding(t *testing.T) { } } +func TestEmptyTimeMarshalWithInterface(t *testing.T) { + a := time.Time{} + b, err := msgpack.Marshal(a) + if err != nil { + t.Fatal(err) + } + var out interface{} + err = msgpack.Unmarshal(b, &out) + if err != nil { + t.Fatal(err) + } + name, _ := out.(time.Time).Zone() + if name != "UTC" { + t.Fatal("Got wrong timezone") + } + + var out2 time.Time + err = msgpack.Unmarshal(b, &out2) + if err != nil { + t.Fatal(err) + } + name, _ = out2.Zone() + if name != "UTC" { + t.Fatal("Got wrong timezone") + } +} + func (t *MsgpackTest) TestSliceNil() { in := [][]*int{nil} var out [][]*int diff --git a/time.go b/time.go index 37cc432..1a4ba12 100644 --- a/time.go +++ b/time.go @@ -26,6 +26,11 @@ func timeDecoder(d *Decoder, v reflect.Value, extLen int) error { return err } + if tm.IsZero() { + // Zero time does not have timezone information. + tm = tm.UTC() + } + ptr := v.Addr().Interface().(*time.Time) *ptr = tm