From 9c8272d9f618952c06ff431485fea78ea15b5b57 Mon Sep 17 00:00:00 2001 From: B1nj0y Date: Mon, 16 Oct 2017 14:49:22 +0800 Subject: [PATCH] add a case in Next scan to fix issue 190 and issue 316 --- sqlite3.go | 21 +++++++++++++++++++++ sqlite3_test.go | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sqlite3.go b/sqlite3.go index 1ff58c3..2ed3860 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -1150,6 +1150,27 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { t = t.In(rc.s.c.loc) } dest[i] = t + // this case is for a nullable datetime typed colomn a chance to be parsed + // to resolve the problem in issue #190 and issue #316 + case "": + var t time.Time + s = strings.TrimSuffix(s, "Z") + for _, format := range SQLiteTimestampFormats { + if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil { + t = timeVal + break + } + } + // if not parsed as a datetime type successfully, do as the default case does + if err != nil { + dest[i] = []byte(s) + } else { + if rc.s.c.loc != nil { + t = t.In(rc.s.c.loc) + } + dest[i] = t + } + default: dest[i] = []byte(s) } diff --git a/sqlite3_test.go b/sqlite3_test.go index 9d4b373..a2b8125 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1111,7 +1111,7 @@ func TestDateTimeNow(t *testing.T) { defer db.Close() var d time.Time - err = db.QueryRow("SELECT datetime('now')").Scan(TimeStamp{&d}) + err = db.QueryRow("SELECT datetime('now')").Scan(&d) if err != nil { t.Fatal("Failed to scan datetime:", err) }