数据库的创建时间字段设置成timestamp,golang中的model中,xorm只指定了created的标签,自动生成的时间默认格式是:2006-01-02 15:04:05,所以需要指定xorm的类型标签为timestampz,进入另一个format分支
type Batch struct {
CreatedTime types.LocalTime `json:"created_time" xorm:"timestampz created"`
ModifyTime types.LocalTime `json:"modify_time" xorm:"timestampz updated"`
}
源码参考:engine.go中的formatTime()方法(由方法nowTime()调用)
// formatTime format time as column type
func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}) {
switch sqlTypeName {
case core.Time:
s := t.Format("2006-01-02 15:04:05") // time.RFC3339
v = s[11:19]
case core.Date:
v = t.Format("2006-01-02")
case core.DateTime, core.TimeStamp:
v = t.Format("2006-01-02 15:04:05")
case core.TimeStampz:
if engine.dialect.DBType() == core.MSSQL {
v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
} else {
v = t.Format(time.RFC3339Nano)
}
case core.BigInt, core.Int:
v = t.Unix()
default:
v = t
}
return
}