From dee1a37fe11067e97971fac64fa5bff4ef0934f4 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 15 Apr 2015 16:26:27 +0900 Subject: [PATCH] Z suffix should be no-op --- sqlite3.go | 1 + sqlite3_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/sqlite3.go b/sqlite3.go index 5620b88..233e7e9 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -650,6 +650,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { switch rc.decltype[i] { case "timestamp", "datetime", "date": 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 diff --git a/sqlite3_test.go b/sqlite3_test.go index 74cafa8..3a7d162 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -8,7 +8,9 @@ package sqlite3 import ( "crypto/rand" "database/sql" + "database/sql/driver" "encoding/hex" + "errors" "fmt" "net/url" "os" @@ -65,7 +67,7 @@ func TestOpen(t *testing.T) { for option, expectedPass := range cases { result, err := doTestOpen(t, option) if result == "" { - if ! expectedPass { + if !expectedPass { errmsg := fmt.Sprintf("_txlock error not caught at dbOpen with option: %s", option) t.Fatal(errmsg) } @@ -1018,3 +1020,41 @@ func TestStringContainingZero(t *testing.T) { t.Error("Failed to db.QueryRow: not matched results") } } + +const CurrentTimeStamp = "2006-01-02 15:04:05" + +type TimeStamp struct{ *time.Time } + +func (t TimeStamp) Scan(value interface{}) error { + fmt.Printf("%T\n", value) + + var err error + switch v := value.(type) { + case string: + *t.Time, err = time.Parse(CurrentTimeStamp, v) + case []byte: + *t.Time, err = time.Parse(CurrentTimeStamp, string(v)) + default: + err = errors.New("invalid type for current_timestamp") + } + return err +} + +func (t TimeStamp) Value() (driver.Value, error) { + return t.Time.Format(CurrentTimeStamp), nil +} + +func TestDateTimeNow(t *testing.T) { + tempFilename := TempFilename() + db, err := sql.Open("sqlite3", tempFilename) + if err != nil { + t.Fatal("Failed to open database:", err) + } + defer db.Close() + + var d time.Time + err = db.QueryRow("SELECT datetime('now')").Scan(TimeStamp{&d}) + if err != nil { + t.Fatal("Failed to scan datetime:", err) + } +}