From 7a404899b846ce6f91e4ab05faefdc95c7b36c52 Mon Sep 17 00:00:00 2001 From: DarthPestilane Date: Thu, 28 Jun 2018 15:54:40 +0800 Subject: [PATCH 1/4] format time when sqlTypeName is core.Varchar. Same with core.DateTime or core.TimeStamp --- engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine.go b/engine.go index 08e2ace5c..92519b491 100644 --- a/engine.go +++ b/engine.go @@ -1554,7 +1554,7 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{} v = s[11:19] case core.Date: v = t.Format("2006-01-02") - case core.DateTime, core.TimeStamp: + case core.DateTime, core.TimeStamp, core.Varchar: v = t.Format("2006-01-02 15:04:05") case core.TimeStampz: if engine.dialect.DBType() == core.MSSQL { From bb59fcc543bc11a0f59c5acb0cd3514439576fe3 Mon Sep 17 00:00:00 2001 From: DarthPestilane Date: Thu, 28 Jun 2018 16:03:10 +0800 Subject: [PATCH 2/4] sign codes according to contributing rules. --- engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine.go b/engine.go index 92519b491..5ba8c3723 100644 --- a/engine.go +++ b/engine.go @@ -1554,7 +1554,7 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{} v = s[11:19] case core.Date: v = t.Format("2006-01-02") - case core.DateTime, core.TimeStamp, core.Varchar: + case core.DateTime, core.TimeStamp, core.Varchar: // !DarthPestilane! format time when sqlTypeName is core.Varchar. v = t.Format("2006-01-02 15:04:05") case core.TimeStampz: if engine.dialect.DBType() == core.MSSQL { From 6a8dbd8e95f9777dc59a067e2c5cb9ab131c7cb4 Mon Sep 17 00:00:00 2001 From: DarthPestilane Date: Thu, 9 Aug 2018 12:07:01 +0800 Subject: [PATCH 3/4] add test for time format --- .gitignore | 1 + session_insert_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/.gitignore b/.gitignore index 22486d5f7..61a7101ff 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # Folders _obj _test +vendor/ # Architecture specific extensions/prefixes *.[568vq] diff --git a/session_insert_test.go b/session_insert_test.go index 50943032d..f99e833e5 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -30,6 +30,23 @@ func TestInsertOne(t *testing.T) { assert.NoError(t, err) } +func TestInsertTimeFormat(t *testing.T) { // !DarthPestilane! format time when sqlTypeName is core.Varchar. + assert.NoError(t, prepareEngine()) + type Test struct { + Id int64 `xorm:"autoincr pk"` + Msg string `xorm:"varchar(255)"` + Created string `xorm:"created"` + } + assert.NoError(t, testEngine.Sync2(new(Test))) + data := Test{Msg: "hi"} + _, err := testEngine.InsertOne(data) + assert.NoError(t, err) + _, err = testEngine.Get(&data) + assert.NoError(t, err) + _, err = time.Parse("2006-01-02 15:04:05", data.Created) + assert.NoError(t, err) +} + func TestInsertMulti(t *testing.T) { assert.NoError(t, prepareEngine()) From bdacdfe7d465e7655771650126909d209baad1b0 Mon Sep 17 00:00:00 2001 From: DarthPestilane Date: Thu, 9 Aug 2018 14:05:56 +0800 Subject: [PATCH 4/4] fix time test --- session_insert_test.go | 17 ----------------- time_test.go | 11 +++++++---- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/session_insert_test.go b/session_insert_test.go index f99e833e5..50943032d 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -30,23 +30,6 @@ func TestInsertOne(t *testing.T) { assert.NoError(t, err) } -func TestInsertTimeFormat(t *testing.T) { // !DarthPestilane! format time when sqlTypeName is core.Varchar. - assert.NoError(t, prepareEngine()) - type Test struct { - Id int64 `xorm:"autoincr pk"` - Msg string `xorm:"varchar(255)"` - Created string `xorm:"created"` - } - assert.NoError(t, testEngine.Sync2(new(Test))) - data := Test{Msg: "hi"} - _, err := testEngine.InsertOne(data) - assert.NoError(t, err) - _, err = testEngine.Get(&data) - assert.NoError(t, err) - _, err = time.Parse("2006-01-02 15:04:05", data.Created) - assert.NoError(t, err) -} - func TestInsertMulti(t *testing.T) { assert.NoError(t, prepareEngine()) diff --git a/time_test.go b/time_test.go index b7e4d12b2..9a8219831 100644 --- a/time_test.go +++ b/time_test.go @@ -255,10 +255,12 @@ func TestTimeUserDeleted(t *testing.T) { assert.NoError(t, prepareEngine()) type UserDeleted struct { - Id string - CreatedAt time.Time `xorm:"created"` - UpdatedAt time.Time `xorm:"updated"` - DeletedAt time.Time `xorm:"deleted"` + Id string + CreatedAt time.Time `xorm:"created"` + UpdatedAt time.Time `xorm:"updated"` + DeletedAt time.Time `xorm:"deleted"` + CreatedAtStr string `xorm:"datetime created"` + UpdatedAtStr string `xorm:"datetime updated"` } assertSync(t, new(UserDeleted)) @@ -282,6 +284,7 @@ func TestTimeUserDeleted(t *testing.T) { assert.EqualValues(t, formatTime(user.UpdatedAt), formatTime(user2.UpdatedAt)) assert.True(t, isTimeZero(user2.DeletedAt)) fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt, user2.DeletedAt) + fmt.Println("user2 str", user2.CreatedAtStr, user2.UpdatedAtStr) var user3 UserDeleted cnt, err = testEngine.Where("id = ?", "lunny").Delete(&user3)