Ver Fonte

Enhancement of date datatype (#1276)

* Enhancing date datatype unmarshal into the string
Rishabh Gupta há 6 anos atrás
pai
commit
59a610c947
2 ficheiros alterados com 18 adições e 0 exclusões
  1. 10 0
      marshal.go
  2. 8 0
      marshal_test.go

+ 10 - 0
marshal.go

@@ -1261,6 +1261,16 @@ func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
 		timestamp := (int64(current) - int64(origin)) * 86400000
 		*v = time.Unix(0, timestamp*int64(time.Millisecond)).In(time.UTC)
 		return nil
+	case *string:
+                if len(data) == 0 {
+                        *v = ""
+                        return nil
+                }
+                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).Format("2006-01-02")
+                return nil
 	}
 	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
 }

+ 8 - 0
marshal_test.go

@@ -1430,6 +1430,14 @@ func TestUnmarshalDate(t *testing.T) {
 		t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
 		return
 	}
+	var stringDate string
+	if err2 := unmarshalDate(NativeType{proto: 2, typ: TypeDate}, data, &stringDate); err2 != nil {
+		t.Fatal(err2)
+	}
+	if expectedDate != stringDate {
+		t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
+		return
+	}
 }
 
 func TestMarshalDate(t *testing.T) {