Skip to content

Commit

Permalink
Merge pull request #333 from aviadl/v5
Browse files Browse the repository at this point in the history
Fix unmarshaling an empty time struct when an interface is supplied to unmarshal function
  • Loading branch information
vmihailenco authored Oct 1, 2023
2 parents 2f6f7d2 + d650250 commit 8a4375f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8a4375f

Please sign in to comment.