mirror of https://github.com/mattn/go-sqlite3.git
Handle 13 digit datetime values
This commit is contained in:
parent
e068bcd768
commit
0b05acc293
12
sqlite3.go
12
sqlite3.go
|
@ -64,6 +64,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -504,7 +505,18 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
||||||
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
||||||
switch rc.decltype[i] {
|
switch rc.decltype[i] {
|
||||||
case "timestamp", "datetime", "date":
|
case "timestamp", "datetime", "date":
|
||||||
|
unixTimestamp := strconv.FormatInt(val, 10)
|
||||||
|
if len(unixTimestamp) == 13 {
|
||||||
|
duration, err := time.ParseDuration(unixTimestamp + "ms")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error parsing %s value %d, %s", rc.decltype[i], val, err)
|
||||||
|
}
|
||||||
|
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
dest[i] = epoch.Add(duration)
|
||||||
|
} else {
|
||||||
dest[i] = time.Unix(val, 0).Local()
|
dest[i] = time.Unix(val, 0).Local()
|
||||||
|
}
|
||||||
|
|
||||||
case "boolean":
|
case "boolean":
|
||||||
dest[i] = val > 0
|
dest[i] = val > 0
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -309,6 +309,7 @@ func TestTimestamp(t *testing.T) {
|
||||||
{"0000-00-00 00:00:00", time.Time{}},
|
{"0000-00-00 00:00:00", time.Time{}},
|
||||||
{timestamp1, timestamp1},
|
{timestamp1, timestamp1},
|
||||||
{timestamp1.Unix(), timestamp1},
|
{timestamp1.Unix(), timestamp1},
|
||||||
|
{timestamp1.UnixNano() / int64(time.Millisecond), timestamp1},
|
||||||
{timestamp1.In(time.FixedZone("TEST", -7*3600)), timestamp1},
|
{timestamp1.In(time.FixedZone("TEST", -7*3600)), timestamp1},
|
||||||
{timestamp1.Format("2006-01-02 15:04:05.000"), timestamp1},
|
{timestamp1.Format("2006-01-02 15:04:05.000"), timestamp1},
|
||||||
{timestamp1.Format("2006-01-02T15:04:05.000"), timestamp1},
|
{timestamp1.Format("2006-01-02T15:04:05.000"), timestamp1},
|
||||||
|
|
Loading…
Reference in New Issue