From faed7f2879510b9d59f601647208e3cbbafef976 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 19 Jul 2013 13:00:22 -0500 Subject: [PATCH] Add timestamptz transcoding --- sanitize.go | 2 +- value_transcoder.go | 21 +++++++++++++++++++++ value_transcoder_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/sanitize.go b/sanitize.go index a3b00d4e..f5ac49dc 100644 --- a/sanitize.go +++ b/sanitize.go @@ -45,7 +45,7 @@ func (c *Connection) SanitizeSql(sql string, args ...interface{}) (output string case int64: return strconv.FormatInt(int64(arg), 10) case time.Time: - return c.QuoteString(arg.Format("2006-01-02 15:04:05.999999999 -0700")) + return c.QuoteString(arg.Format("2006-01-02 15:04:05.999999 -0700")) case uint: return strconv.FormatUint(uint64(arg), 10) case uint8: diff --git a/value_transcoder.go b/value_transcoder.go index 025748c0..bf17e737 100644 --- a/value_transcoder.go +++ b/value_transcoder.go @@ -94,6 +94,11 @@ func init() { DecodeText: decodeDateFromText, EncodeTo: encodeDate} + // timestamptz + ValueTranscoders[Oid(1184)] = &ValueTranscoder{ + DecodeText: decodeTimestampTzFromText, + EncodeTo: encodeTimestampTz} + // use text transcoder for anything we don't understand defaultTranscoder = ValueTranscoders[Oid(25)] } @@ -284,3 +289,19 @@ func encodeDate(w *MessageWriter, value interface{}) { w.Write(int32(len(s))) w.WriteString(s) } + +func decodeTimestampTzFromText(mr *MessageReader, size int32) interface{} { + s := mr.ReadByteString(size) + t, err := time.Parse("2006-01-02 15:04:05.999999-07", s) + if err != nil { + panic(fmt.Sprintf("Can't decode timestamptz: %v", err)) + } + return t +} + +func encodeTimestampTz(w *MessageWriter, value interface{}) { + t := value.(time.Time) + s := t.Format("2006-01-02 15:04:05.999999 -0700") + w.Write(int32(len(s))) + w.WriteString(s) +} diff --git a/value_transcoder_test.go b/value_transcoder_test.go index 20daa0d8..c52307ab 100644 --- a/value_transcoder_test.go +++ b/value_transcoder_test.go @@ -32,3 +32,31 @@ func TestDateTranscode(t *testing.T) { t.Errorf("Did not transcode date successfully: %v is not %v", v, actualDate) } } + +func TestTimestampTzTranscode(t *testing.T) { + conn := getSharedConnection() + + inputTime := time.Date(2013, 1, 2, 3, 4, 5, 6000, time.Local) + + var v interface{} + var outputTime time.Time + + v = mustSelectValue(t, conn, "select $1::timestamptz", inputTime) + outputTime = v.(time.Time) + if !inputTime.Equal(outputTime) { + t.Errorf("Did not transcode time successfully: %v is not %v", outputTime, inputTime) + } + + mustPrepare(t, conn, "testTranscode", "select $1::timestamptz") + defer func() { + if err := conn.Deallocate("testTranscode"); err != nil { + t.Fatalf("Unable to deallocate prepared statement: %v", err) + } + }() + + v = mustSelectValue(t, conn, "testTranscode", inputTime) + outputTime = v.(time.Time) + if !inputTime.Equal(outputTime) { + t.Errorf("Did not transcode time successfully: %v is not %v", outputTime, inputTime) + } +}