From d6ab08bdadbcb3e77f4c25b44665f76b833142c9 Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 10 Sep 2018 15:14:12 +0800 Subject: [PATCH 1/2] executor, expression: calculating the default value for datetime should consider the time zone --- executor/insert_test.go | 14 ++++++++++++++ expression/helper.go | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/executor/insert_test.go b/executor/insert_test.go index 2b66f80bf31cf..34f32027f6e76 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -117,3 +117,17 @@ func (s *testSuite) TestInsertWrongValueForField(c *C) { _, err := tk.Exec(`insert into t1 values("asfasdfsajhlkhlksdaf");`) c.Assert(terror.ErrorEqual(err, table.ErrTruncatedWrongValueForField), IsTrue) } + +func (s *testSuite) TestInsertDateTimeWithTimeZone(c *C) { + tk := testkit.NewTestKit(c, s.store) + + tk.MustExec(`set time_zone="+09:00";`) + tk.MustExec(`drop table if exists t;`) + tk.MustExec(`create table t (id int, c1 datetime not null default CURRENT_TIMESTAMP);`) + tk.MustExec(`set TIMESTAMP = 1234;`) + tk.MustExec(`insert t (id) values (1);`) + + tk.MustQuery(`select * from t;`).Check(testkit.Rows( + `1 1970-01-01 09:20:34`, + )) +} diff --git a/expression/helper.go b/expression/helper.go index 813f45e903c85..24077e488b5ef 100644 --- a/expression/helper.go +++ b/expression/helper.go @@ -59,7 +59,7 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int) (d ty upperX := strings.ToUpper(x) if upperX == strings.ToUpper(ast.CurrentTimestamp) { value.Time = types.FromGoTime(defaultTime.Truncate(time.Duration(math.Pow10(9-fsp)) * time.Nanosecond)) - if tp == mysql.TypeTimestamp { + if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime { err = value.ConvertTimeZone(time.Local, ctx.GetSessionVars().Location()) if err != nil { return d, errors.Trace(err) From 587cbe46014923453fa2473c5fa9966f7416b56a Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 10 Sep 2018 15:34:26 +0800 Subject: [PATCH 2/2] fix ci --- executor/insert_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/executor/insert_test.go b/executor/insert_test.go index 34f32027f6e76..82196579f0e78 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -121,6 +121,7 @@ func (s *testSuite) TestInsertWrongValueForField(c *C) { func (s *testSuite) TestInsertDateTimeWithTimeZone(c *C) { tk := testkit.NewTestKit(c, s.store) + tk.MustExec(`use test;`) tk.MustExec(`set time_zone="+09:00";`) tk.MustExec(`drop table if exists t;`) tk.MustExec(`create table t (id int, c1 datetime not null default CURRENT_TIMESTAMP);`)