From ce139f706b870082071a9d9fd18284c7b7fc9d65 Mon Sep 17 00:00:00 2001 From: Micah Stetson Date: Sat, 29 Dec 2012 16:36:29 -0800 Subject: Support time values with nanosecond precision --- sqlite3.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'sqlite3.go') diff --git a/sqlite3.go b/sqlite3.go index fb446cc..6c0fc61 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -40,9 +40,15 @@ import ( "unsafe" ) -const SQLiteTimestampFormat = "2006-01-02 15:04:05" -const SQLiteDateFormat = "2006-01-02" -const SQLiteDatetimeFormat = "2006-01-02 15:04:05.000" +// Timestamp formats understood by both this module and SQLite. +// The first format in the slice will be used when saving time values +// into the database. When parsing a string from a timestamp or +// datetime column, the formats are tried in order. +var SQLiteTimestampFormats = []string{ + "2006-01-02 15:04:05.999999999", + "2006-01-02 15:04:05", + "2006-01-02", +} func init() { sql.Register("sqlite3", &SQLiteDriver{}) @@ -213,7 +219,7 @@ func (s *SQLiteStmt) bind(args []driver.Value) error { } rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v))) case time.Time: - b := []byte(v.UTC().Format(SQLiteTimestampFormat)) + b := []byte(v.UTC().Format(SQLiteTimestampFormats[0])) rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b))) } if rv != C.SQLITE_OK { @@ -327,19 +333,14 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { switch rc.decltype[i] { case "timestamp", "datetime": - for { - if dest[i], err = time.Parse(SQLiteTimestampFormat, s); err == nil { - break - } - if dest[i], err = time.Parse(SQLiteDateFormat, s); err == nil { - break - } - if dest[i], err = time.Parse(SQLiteDatetimeFormat, s); err == nil { + for _, format := range SQLiteTimestampFormats { + if dest[i], err = time.Parse(format, s); err == nil { break } + } + if err != nil { // The column is a time value, so return the zero time on parse failure. dest[i] = time.Time{} - break } default: dest[i] = s -- cgit v1.2.3 From 44496728c24982a76893272b23d0c7e949b5818c Mon Sep 17 00:00:00 2001 From: Micah Stetson Date: Sat, 29 Dec 2012 16:51:15 -0800 Subject: Support more of the timestamp formats undestood by SQLite --- sqlite3.go | 4 ++++ sqlite3_test.go | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'sqlite3.go') diff --git a/sqlite3.go b/sqlite3.go index 6c0fc61..0a21d23 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -46,7 +46,11 @@ import ( // datetime column, the formats are tried in order. var SQLiteTimestampFormats = []string{ "2006-01-02 15:04:05.999999999", + "2006-01-02T15:04:05.999999999", "2006-01-02 15:04:05", + "2006-01-02T15:04:05", + "2006-01-02 15:04", + "2006-01-02T15:04", "2006-01-02", } diff --git a/sqlite3_test.go b/sqlite3_test.go index a5f324f..9bcc5f3 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -252,18 +252,30 @@ func TestTimestamp(t *testing.T) { timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC) timestamp2 := time.Date(2006, time.January, 2, 15, 4, 5, 123456789, time.UTC) + timestamp3 := time.Date(2012, time.November, 4, 0, 0, 0, 0, time.UTC) tests := []struct { value interface{} expected time.Time }{ - {timestamp1, timestamp1}, - {timestamp1.Unix(), timestamp1}, {"nonsense", time.Time{}}, {"0000-00-00 00:00:00", time.Time{}}, + {timestamp1, timestamp1}, + {timestamp1.Unix(), timestamp1}, {timestamp1.In(time.FixedZone("TEST", -7*3600)), timestamp1}, - {"2012-11-04", time.Date(2012, time.November, 4, 0, 0, 0, 0, time.UTC)}, + {timestamp1.Format("2006-01-02 15:04:05.000"), timestamp1}, + {timestamp1.Format("2006-01-02T15:04:05.000"), timestamp1}, + {timestamp1.Format("2006-01-02 15:04:05"), timestamp1}, + {timestamp1.Format("2006-01-02T15:04:05"), timestamp1}, {timestamp2, timestamp2}, {"2006-01-02 15:04:05.123456789", timestamp2}, + {"2006-01-02T15:04:05.123456789", timestamp2}, + {"2012-11-04", timestamp3}, + {"2012-11-04 00:00", timestamp3}, + {"2012-11-04 00:00:00", timestamp3}, + {"2012-11-04 00:00:00.000", timestamp3}, + {"2012-11-04T00:00", timestamp3}, + {"2012-11-04T00:00:00", timestamp3}, + {"2012-11-04T00:00:00.000", timestamp3}, } for i := range tests { _, err = db.Exec("INSERT INTO foo(id, ts, dt) VALUES(?, ?, ?)", i, tests[i].value, tests[i].value) @@ -293,10 +305,10 @@ func TestTimestamp(t *testing.T) { } seen++ if !tests[id].expected.Equal(ts) { - t.Errorf("Timestamp value for id %v should be %v, not %v", id, tests[id].expected, ts) + t.Errorf("Timestamp value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt) } if !tests[id].expected.Equal(dt) { - t.Errorf("Datetime value for id %v should be %v, not %v", id, tests[id].expected, dt) + t.Errorf("Datetime value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt) } } -- cgit v1.2.3