Skip to content

Commit

Permalink
Fix and test for unmarshal date type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Qin committed Mar 24, 2017
1 parent b7597a1 commit 5c85086
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,13 @@ func marshalDate(info TypeInfo, value interface{}) ([]byte, error) {

func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case Unmarshaler:
return v.UnmarshalCQL(info, data)
case *time.Time:
if len(data) == 0 {
*v = time.Time{}
return nil
}
var origin uint32 = 1 << 31
var current uint32 = binary.BigEndian.Uint32(data)
timestamp := (int64(current) - int64(origin)) * 86400000
Expand Down
15 changes: 15 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,18 @@ func TestUnmarshalInetCopyBytes(t *testing.T) {
t.Fatalf("IP memory shared with data: ip=%v ip2=%v", ip, ip2)
}
}

func TestUnmarshalDate(t *testing.T) {
data := []uint8{0x80, 0x0, 0x43, 0x31}
var date time.Time
if err := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &date); err != nil {
t.Fatal(err)
}

expectedDate := "2017-02-04"
formattedDate := date.Format("2006-01-02")
if expectedDate != formattedDate {
t.Errorf("marshalTest: expected %x (%v), got %x (%v)", expectedDate, formattedDate)
return
}
}

0 comments on commit 5c85086

Please sign in to comment.