fix insert time is nil error

This commit is contained in:
jeck.han 2018-11-13 23:12:25 +08:00 committed by Lunny Xiao
parent a7e010df2d
commit 4d25826538
1 changed files with 42 additions and 1 deletions

View File

@ -1232,7 +1232,48 @@ func (engine *Engine) nowTime(col *schemas.Column) (interface{}, time.Time) {
if !col.DisableTimeZone && col.TimeZone != nil {
tz = col.TimeZone
}
return dialects.FormatTime(engine.dialect, col.SQLType.Name, t.In(tz)), t.In(engine.TZLocation)
return engine.formatTime(col.SQLType.Name, t.In(tz)), t.In(engine.TZLocation)
}
func (engine *Engine) formatColTime(col *schemas.Column, t time.Time) (v interface{}) {
if t.IsZero() {
if col.Nullable {
return nil
}
if col.Default != "" {
return col.Default
}
return engine.formatTime(col.SQLType.Name, t)
}
if col.TimeZone != nil {
return engine.formatTime(col.SQLType.Name, t.In(col.TimeZone))
}
return engine.formatTime(col.SQLType.Name, t.In(engine.DatabaseTZ))
}
// formatTime format time as column type
func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}) {
switch sqlTypeName {
case schemas.Time:
s := t.Format("2006-01-02 15:04:05") //time.RFC3339
v = s[11:19]
case schemas.Date:
v = t.Format("2006-01-02")
case schemas.DateTime, schemas.TimeStamp:
v = t.Format("2006-01-02 15:04:05")
case schemas.TimeStampz:
if engine.dialect.URI().DBType == schemas.MSSQL {
v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
} else {
v = t.Format(time.RFC3339Nano)
}
case schemas.BigInt, schemas.Int:
v = t.Unix()
default:
v = t
}
return
}
// GetColumnMapper returns the column name mapper