From 2f1d8ecb2752ed54afb4f10f69821f10b8a758b0 Mon Sep 17 00:00:00 2001 From: Zhang Jian Date: Thu, 20 Sep 2018 13:58:05 +0800 Subject: [PATCH] executor, expression: calculating the default value for datetime should consider the time zone (#7655) (#7672) --- executor/write_test.go | 13 +++++++++++++ expression/helper.go | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/executor/write_test.go b/executor/write_test.go index a10ba47c2420f..021730f7d6090 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -1628,3 +1628,16 @@ func (s *testSuite) TestUpdateDelete(c *C) { tk.MustExec("admin check table ttt;") tk.MustExec("drop table ttt") } + +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);`) + 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 6e21bed2f2525..be7c95172e274 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().GetTimeZone()) if err != nil { return d, errors.Trace(err)