瀏覽代碼

Merge pull request #872 from evildecay/master

fix to support the date type
Chris Bannister 8 年之前
父節點
當前提交
88d476e254
共有 2 個文件被更改,包括 16 次插入0 次删除
  1. 2 0
      helpers.go
  2. 14 0
      marshal.go

+ 2 - 0
helpers.go

@@ -57,6 +57,8 @@ func goType(t TypeInfo) reflect.Type {
 		return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
 	case TypeUDT:
 		return reflect.TypeOf(make(map[string]interface{}))
+	case TypeDate:
+		return reflect.TypeOf(*new(time.Time))
 	default:
 		return nil
 	}

+ 14 - 0
marshal.go

@@ -155,6 +155,8 @@ func Unmarshal(info TypeInfo, data []byte, value interface{}) error {
 		return unmarshalTuple(info, data, value)
 	case TypeUDT:
 		return unmarshalUDT(info, data, value)
+	case TypeDate:
+		return unmarshalDate(info, data, value)
 	}
 
 	// detect protocol 2 UDT
@@ -1116,6 +1118,18 @@ func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error {
 	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
 }
 
+func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
+	switch v := value.(type) {
+	case *time.Time:
+		var origin uint32 = 1 << 31
+		var current uint32 = binary.BigEndian.Uint32(data)
+		timestamp := (int64(current) - int64(origin)) * 86400000
+		*v = time.Unix(0, timestamp*int64(time.Millisecond)).In(time.UTC)
+		return nil
+	}
+	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
+}
+
 func writeCollectionSize(info CollectionType, n int, buf *bytes.Buffer) error {
 	if info.proto > protoVersion2 {
 		if n > math.MaxInt32 {