Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
support null timestamp types
Browse files Browse the repository at this point in the history
  • Loading branch information
iand committed Apr 24, 2015
1 parent 3ef81d6 commit c288259
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
8 changes: 6 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,14 @@ var doubleConverter = valueConverterFunc(func(val interface{}) (driver.Value, er

// timestampConverter converts a value from the underlying json response into a time.Time.
var timestampConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
if vv, ok := val.(string); ok {
// BUG: should parse using session time zone.
ts, err := time.ParseInLocation("2006-01-02 15:04:05.000", vv, time.Local)
return ts, err
if ts, err := time.ParseInLocation("2006-01-02 15:04:05.000", vv, time.Local); err == nil {
return ts, nil
}
}
return nil, fmt.Errorf("%s: failed to convert %v (%T) into type time.Time", DriverName, val, val)
})
58 changes: 58 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,61 @@ func TestBigIntConverter(t *testing.T) {

}
}

func TestTimestampConverter(t *testing.T) {
testCases := []struct {
val interface{}
expected driver.Value
err bool
}{

{
val: "2015-04-23 10:00:08.123",
expected: time.Date(2015, 04, 23, 10, 0, 8, int(123*time.Millisecond), time.Local),
err: false,
},

{
val: 1000.0,
expected: nil,
err: true,
},

{
val: "foo",
expected: nil,
err: true,
},

{
val: "Infinity",
expected: nil,
err: true,
},

{
val: "NaN",
expected: nil,
err: true,
},

{
val: nil,
expected: nil,
err: false,
},
}

for _, tc := range testCases {
v, err := timestampConverter(tc.val)

if tc.err == (err == nil) {
t.Errorf("%v: got error %v, wanted %v", tc.val, err, tc.err)
}

if v != tc.expected {
t.Errorf("%v: got %v, wanted %v", tc.val, v, tc.expected)
}

}
}

0 comments on commit c288259

Please sign in to comment.