Support more of the timestamp formats undestood by SQLite

This commit is contained in:
Micah Stetson 2012-12-29 16:51:15 -08:00
parent ce139f706b
commit 44496728c2
2 changed files with 21 additions and 5 deletions

View File

@ -46,7 +46,11 @@ import (
// datetime column, the formats are tried in order. // datetime column, the formats are tried in order.
var SQLiteTimestampFormats = []string{ var SQLiteTimestampFormats = []string{
"2006-01-02 15:04:05.999999999", "2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04:05", "2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02", "2006-01-02",
} }

View File

@ -252,18 +252,30 @@ func TestTimestamp(t *testing.T) {
timestamp1 := time.Date(2012, time.April, 6, 22, 50, 0, 0, time.UTC) 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) 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 { tests := []struct {
value interface{} value interface{}
expected time.Time expected time.Time
}{ }{
{timestamp1, timestamp1},
{timestamp1.Unix(), timestamp1},
{"nonsense", time.Time{}}, {"nonsense", time.Time{}},
{"0000-00-00 00:00:00", time.Time{}}, {"0000-00-00 00:00:00", time.Time{}},
{timestamp1, timestamp1},
{timestamp1.Unix(), timestamp1},
{timestamp1.In(time.FixedZone("TEST", -7*3600)), 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}, {timestamp2, timestamp2},
{"2006-01-02 15:04:05.123456789", 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 { for i := range tests {
_, err = db.Exec("INSERT INTO foo(id, ts, dt) VALUES(?, ?, ?)", i, tests[i].value, tests[i].value) _, 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++ seen++
if !tests[id].expected.Equal(ts) { 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) { 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)
} }
} }