Automatically convert datetime to int64 (#1715)
Fix #1714 Reviewed-on: https://gitea.com/xorm/xorm/pulls/1715 Reviewed-by: Jerry <jerry@noreply.gitea.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-committed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
26b248c569
commit
8284e5defa
|
@ -962,3 +962,26 @@ func TestDistinctAndCols(t *testing.T) {
|
||||||
assert.EqualValues(t, 1, len(names))
|
assert.EqualValues(t, 1, len(names))
|
||||||
assert.EqualValues(t, "test", names[0])
|
assert.EqualValues(t, "test", names[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDateTimeInt64(t *testing.T) {
|
||||||
|
type DateTimeInt64 struct {
|
||||||
|
Id int64
|
||||||
|
TimeStamp int64 `xorm:"datetime"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
assertSync(t, new(DateTimeInt64))
|
||||||
|
|
||||||
|
ts := time.Now().Unix()
|
||||||
|
cnt, err := testEngine.Insert(&DateTimeInt64{
|
||||||
|
TimeStamp: ts,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, cnt)
|
||||||
|
|
||||||
|
var dts []DateTimeInt64
|
||||||
|
err = testEngine.Find(&dts)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, len(dts))
|
||||||
|
assert.EqualValues(t, ts, dts[0].TimeStamp)
|
||||||
|
}
|
||||||
|
|
|
@ -168,7 +168,29 @@ func (session *Session) bytes2Value(col *schemas.Column, fieldValue *reflect.Val
|
||||||
} else if strings.EqualFold(sdata, "false") {
|
} else if strings.EqualFold(sdata, "false") {
|
||||||
x = 0
|
x = 0
|
||||||
} else {
|
} else {
|
||||||
x, err = strconv.ParseInt(sdata, 10, 64)
|
if col.SQLType.Name == schemas.DateTime {
|
||||||
|
if len(sdata) == 20 {
|
||||||
|
t, err := time.Parse("2006-01-02T15:04:05Z", sdata)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
||||||
|
}
|
||||||
|
x = t.Unix()
|
||||||
|
} else if len(sdata) == 19 {
|
||||||
|
var parseFormat = "2006-01-02 15:04:05"
|
||||||
|
if sdata[10] == 'T' {
|
||||||
|
parseFormat = "2006-01-02T15:04:05"
|
||||||
|
}
|
||||||
|
t, err := time.Parse(parseFormat, sdata)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
||||||
|
}
|
||||||
|
x = t.Unix()
|
||||||
|
} else {
|
||||||
|
x, err = strconv.ParseInt(sdata, 10, 64)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
x, err = strconv.ParseInt(sdata, 10, 64)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
||||||
|
|
Loading…
Reference in New Issue