aboutsummaryrefslogtreecommitdiff
path: root/sqlite3.go
diff options
context:
space:
mode:
authormattn <mattn.jp@gmail.com>2012-12-29 23:19:31 -0800
committermattn <mattn.jp@gmail.com>2012-12-29 23:19:31 -0800
commit8706f7baf06c3193ac09c594f107028f7795967d (patch)
treefb473d76db44f96f27eb47ba84443d3361b4ae6a /sqlite3.go
parentcheck destination type whether it's *time.Time or not. (diff)
parentConvert times to UTC before storage (diff)
downloadgolite-8706f7baf06c3193ac09c594f107028f7795967d.tar.gz
golite-8706f7baf06c3193ac09c594f107028f7795967d.tar.xz
Merge pull request #35 from mstetson/master
Fix #33 and #34
Diffstat (limited to 'sqlite3.go')
-rw-r--r--sqlite3.go32
1 files changed, 16 insertions, 16 deletions
diff --git a/sqlite3.go b/sqlite3.go
index 1207bf1..fb446cc 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -213,7 +213,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.Format(SQLiteTimestampFormat))
+ b := []byte(v.UTC().Format(SQLiteTimestampFormat))
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
}
if rv != C.SQLITE_OK {
@@ -299,7 +299,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
case C.SQLITE_INTEGER:
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
switch rc.decltype[i] {
- case "timestamp":
+ case "timestamp", "datetime":
dest[i] = time.Unix(val, 0)
case "boolean":
dest[i] = val > 0
@@ -325,21 +325,21 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
var err error
s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))))
- switch dest[i].(type) {
- case *time.Time:
- 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 {
- dest[i] = s
- }
- }
+ 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 {
+ break
}
- } else {
- dest[i] = s
+ // The column is a time value, so return the zero time on parse failure.
+ dest[i] = time.Time{}
+ break
}
default:
dest[i] = s