diff --git a/convert/string.go b/convert/string.go index 193052d5..e39f94a1 100644 --- a/convert/string.go +++ b/convert/string.go @@ -10,8 +10,8 @@ import ( "strconv" ) -// ConvertAssignString converts an interface to string -func ConvertAssignString(v interface{}) (string, error) { +// Interface2String converts an interface to string +func Interface2String(v interface{}) (string, error) { if v == nil { return "", nil } diff --git a/engine.go b/engine.go index fa6f0da5..36c618e5 100644 --- a/engine.go +++ b/engine.go @@ -82,7 +82,12 @@ func newEngine(driverName, dataSourceName string, dialect dialects.Dialect, db * dataSourceName: dataSourceName, db: db, logSessionID: false, - DatabaseTZ: time.Local, + } + + if dialect.URI().DBType == schemas.SQLITE { + engine.DatabaseTZ = time.UTC + } else { + engine.DatabaseTZ = time.Local } logger := log.NewSimpleLogger(os.Stdout) @@ -469,8 +474,6 @@ func formatColumnValue(dbLocation *time.Location, dstDialect dialects.Dialect, d return "NULL" } - fmt.Printf("%#v------%v\n", d, col.Name) - if dq, ok := d.(bool); ok && (dstDialect.URI().DBType == schemas.SQLITE || dstDialect.URI().DBType == schemas.MSSQL) { if dq { @@ -1297,6 +1300,14 @@ func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) { return session.Import(r) } +func (engine *Engine) columnTime(col *schemas.Column, t *time.Time) interface{} { + var tz = engine.DatabaseTZ + if !col.DisableTimeZone && col.TimeZone != nil { + tz = col.TimeZone + } + return t.In(tz) +} + // nowTime return current time func (engine *Engine) nowTime(col *schemas.Column) (interface{}, time.Time) { t := time.Now() diff --git a/session_insert.go b/session_insert.go index 7f8f3008..638b8992 100644 --- a/session_insert.go +++ b/session_insert.go @@ -562,6 +562,11 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac if err != nil { return colNames, args, err } + if t, ok := arg.(time.Time); ok { + arg = session.engine.columnTime(col, &t) + } else if t, ok := arg.(*time.Time); ok { + arg = session.engine.columnTime(col, t) + } args = append(args, arg) }