forked from mirror/go-sqlite3
Add support for extracting `2006-01-02 15:04:05.000` formatted datetime.
This commit is contained in:
parent
68952ca066
commit
430103cc9e
|
@ -28,6 +28,7 @@ import (
|
|||
|
||||
const SQLiteTimestampFormat = "2006-01-02 15:04:05"
|
||||
const SQLiteDateFormat = "2006-01-02"
|
||||
const SQLiteDatetimeFormat = "2006-01-02 15:04:05.000"
|
||||
|
||||
func init() {
|
||||
sql.Register("sqlite3", &SQLiteDriver{})
|
||||
|
@ -311,14 +312,17 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
|||
case C.SQLITE_TEXT:
|
||||
var err error
|
||||
s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))))
|
||||
if rc.decltype[i] == "timestamp" {
|
||||
if rc.decltype[i] == "timestamp" || rc.decltype[i] == "datetime" {
|
||||
dest[i], err = time.Parse(SQLiteTimestampFormat, s)
|
||||
if err != nil {
|
||||
dest[i], err = time.Parse(SQLiteDateFormat, s)
|
||||
if err != nil {
|
||||
dest[i], err = time.Parse(SQLiteDatetimeFormat, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dest[i] = s
|
||||
}
|
||||
|
|
|
@ -460,3 +460,71 @@ func TestDateOnlyTimestamp(t *testing.T) {
|
|||
t.Errorf("Extracted time does not match inserted value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatetime(t *testing.T) {
|
||||
db, err := sql.Open("sqlite3", "./datetime.db")
|
||||
if err != nil {
|
||||
t.Fatal("Failed to open database:", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
db.Close()
|
||||
os.Remove("./datetime.db")
|
||||
}()
|
||||
|
||||
_, err = db.Exec("DROP TABLE datetimetest")
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE datetimetest
|
||||
( entry_id INTEGER
|
||||
, my_datetime DATETIME
|
||||
)
|
||||
`)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal("Failed to create table:", err)
|
||||
}
|
||||
datetime := "2006-01-02 15:04:05.003"
|
||||
_, err = db.Exec(`
|
||||
INSERT INTO datetimetest(entry_id, my_datetime)
|
||||
VALUES(1, ?)`, datetime)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to insert datetime:", err)
|
||||
}
|
||||
|
||||
rows, err := db.Query(
|
||||
"SELECT entry_id, my_datetime FROM datetimetest ORDER BY entry_id ASC")
|
||||
if err != nil {
|
||||
t.Fatal("Unable to query datetimetest table:", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if !rows.Next() {
|
||||
if er := rows.Err(); er != nil {
|
||||
t.Fatal(er)
|
||||
} else {
|
||||
t.Fatalf("Unable to extract row containing datetime")
|
||||
}
|
||||
}
|
||||
|
||||
var entryId int
|
||||
var myDatetime time.Time
|
||||
|
||||
if err := rows.Scan(&entryId, &myDatetime); err != nil {
|
||||
t.Error("Unable to scan results:", err)
|
||||
}
|
||||
|
||||
if entryId != 1 {
|
||||
t.Errorf("EntryId does not match inserted value")
|
||||
}
|
||||
|
||||
if myDatetime.Year() != 2006 ||
|
||||
myDatetime.Month() != 1 ||
|
||||
myDatetime.Day() != 2 ||
|
||||
myDatetime.Hour() != 15 ||
|
||||
myDatetime.Minute() != 4 ||
|
||||
myDatetime.Second() != 5 ||
|
||||
myDatetime.Nanosecond() != 3000000 {
|
||||
t.Errorf("Extracted time does not match inserted value")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue