if the param is type of time.Time, it will be automatic shifted to Engine.DatabaseTZ timezone
This commit is contained in:
parent
7a9249de33
commit
5c8daa6983
|
@ -10,6 +10,7 @@ import (
|
|||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/builder"
|
||||
"github.com/go-xorm/core"
|
||||
|
@ -191,7 +192,24 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
|||
|
||||
for _, v := range bValue.MapKeys() {
|
||||
colNames = append(colNames, session.engine.Quote(v.String())+" = ?")
|
||||
args = append(args, bValue.MapIndex(v).Interface())
|
||||
|
||||
val := bValue.MapIndex(v).Interface()
|
||||
fType := reflect.TypeOf(val)
|
||||
if fType.Kind() == reflect.Struct {
|
||||
if fType.ConvertibleTo(core.TimeType) {
|
||||
t := val.(time.Time)
|
||||
// if the param is in specific timezone
|
||||
if t.Location() != session.statement.Engine.DatabaseTZ &&
|
||||
t.Location() != session.statement.Engine.TZLocation {
|
||||
val = t
|
||||
} else {
|
||||
// shift to Engine.DatabaseTZ timezone
|
||||
val = t.In(session.statement.Engine.DatabaseTZ)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
args = append(args, val)
|
||||
}
|
||||
} else {
|
||||
return 0, ErrParamsType
|
||||
|
|
|
@ -22,6 +22,7 @@ func TestUpdateMap(t *testing.T) {
|
|||
Id int64
|
||||
Name string
|
||||
Age int
|
||||
UpdateAt time.Time
|
||||
}
|
||||
|
||||
assert.NoError(t, testEngine.Sync2(new(UpdateTable)))
|
||||
|
@ -38,6 +39,21 @@ func TestUpdateMap(t *testing.T) {
|
|||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, cnt)
|
||||
|
||||
now := time.Now()
|
||||
cnt, err = testEngine.Table("update_table").Where("id = ?", tb.Id).Update(map[string]interface{}{
|
||||
"name": "test2",
|
||||
"age": 36,
|
||||
"update_at": now,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, cnt)
|
||||
|
||||
var data UpdateTable
|
||||
has, err := testEngine.Table("update_table").Where("id = ?", tb.Id).Get(&data)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, true, has)
|
||||
assert.EqualValues(t, now.Unix(), data.UpdateAt.Unix())
|
||||
}
|
||||
|
||||
func TestUpdateLimit(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue