Automatically convert datetime to int64 #1715
|
@ -962,3 +962,26 @@ func TestDistinctAndCols(t *testing.T) {
|
|||
assert.EqualValues(t, 1, len(names))
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -167,9 +167,31 @@ func (session *Session) bytes2Value(col *schemas.Column, fieldValue *reflect.Val
|
|||
x = 1
|
||||
} else if strings.EqualFold(sdata, "false") {
|
||||
x = 0
|
||||
} else {
|
||||
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"
|
||||
lunny marked this conversation as resolved
Outdated
jerry
commented
这个地方,即使满足 len = 19 ,我急着出来的时间,中间也带个 T 这个地方,即使满足 len = 19 ,我急着出来的时间,中间也带个 T
|
||||
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()
|
||||
jerry
commented
这个 else ,跟上面的有点重复了 这个 else ,跟上面的有点重复了
|
||||
} else {
|
||||
x, err = strconv.ParseInt(sdata, 10, 64)
|
||||
}
|
||||
} else {
|
||||
x, err = strconv.ParseInt(sdata, 10, 64)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user
这个 20 估计不会满足,默认出来的时间,sdata 是 : 带 +8:00 的,不会满足 len() = 20这个条件,