Add binary encoding to date
This commit is contained in:
+8
-2
@@ -109,8 +109,9 @@ func init() {
|
|||||||
|
|
||||||
// date
|
// date
|
||||||
ValueTranscoders[Oid(1082)] = &ValueTranscoder{
|
ValueTranscoders[Oid(1082)] = &ValueTranscoder{
|
||||||
DecodeText: decodeDateFromText,
|
DecodeText: decodeDateFromText,
|
||||||
EncodeTo: encodeDate}
|
DecodeBinary: decodeDateFromBinary,
|
||||||
|
EncodeTo: encodeDate}
|
||||||
|
|
||||||
// timestamptz
|
// timestamptz
|
||||||
ValueTranscoders[Oid(1184)] = &ValueTranscoder{
|
ValueTranscoders[Oid(1184)] = &ValueTranscoder{
|
||||||
@@ -477,6 +478,11 @@ func decodeDateFromText(mr *MessageReader, size int32) interface{} {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func decodeDateFromBinary(mr *MessageReader, size int32) interface{} {
|
||||||
|
dayOffset := mr.ReadInt32()
|
||||||
|
return time.Date(2000, 1, int(1+dayOffset), 0, 0, 0, 0, time.Local)
|
||||||
|
}
|
||||||
|
|
||||||
func encodeDate(w *WriteBuf, value interface{}) error {
|
func encodeDate(w *WriteBuf, value interface{}) error {
|
||||||
t, ok := value.(time.Time)
|
t, ok := value.(time.Time)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
+27
-15
@@ -62,17 +62,6 @@ func TestDateTranscode(t *testing.T) {
|
|||||||
conn := mustConnect(t, *defaultConnConfig)
|
conn := mustConnect(t, *defaultConnConfig)
|
||||||
defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
actualDate := time.Date(2013, 1, 2, 0, 0, 0, 0, time.Local)
|
|
||||||
|
|
||||||
var v interface{}
|
|
||||||
var d time.Time
|
|
||||||
|
|
||||||
v = mustSelectValue(t, conn, "select $1::date", actualDate)
|
|
||||||
d = v.(time.Time)
|
|
||||||
if !actualDate.Equal(d) {
|
|
||||||
t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate)
|
|
||||||
}
|
|
||||||
|
|
||||||
mustPrepare(t, conn, "testTranscode", "select $1::date")
|
mustPrepare(t, conn, "testTranscode", "select $1::date")
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := conn.Deallocate("testTranscode"); err != nil {
|
if err := conn.Deallocate("testTranscode"); err != nil {
|
||||||
@@ -80,10 +69,33 @@ func TestDateTranscode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
v = mustSelectValue(t, conn, "testTranscode", actualDate)
|
dates := []time.Time{
|
||||||
d = v.(time.Time)
|
time.Date(1990, 1, 1, 0, 0, 0, 0, time.Local),
|
||||||
if !actualDate.Equal(d) {
|
time.Date(1999, 12, 31, 0, 0, 0, 0, time.Local),
|
||||||
t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate)
|
time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local),
|
||||||
|
time.Date(2001, 1, 2, 0, 0, 0, 0, time.Local),
|
||||||
|
time.Date(2004, 2, 29, 0, 0, 0, 0, time.Local),
|
||||||
|
time.Date(2013, 7, 4, 0, 0, 0, 0, time.Local),
|
||||||
|
time.Date(2013, 12, 25, 0, 0, 0, 0, time.Local),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, actualDate := range dates {
|
||||||
|
var v interface{}
|
||||||
|
var d time.Time
|
||||||
|
|
||||||
|
// Test text format
|
||||||
|
v = mustSelectValue(t, conn, "select $1::date", actualDate)
|
||||||
|
d = v.(time.Time)
|
||||||
|
if !actualDate.Equal(d) {
|
||||||
|
t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test binary format
|
||||||
|
v = mustSelectValue(t, conn, "testTranscode", actualDate)
|
||||||
|
d = v.(time.Time)
|
||||||
|
if !actualDate.Equal(d) {
|
||||||
|
t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user