diff --git a/store/mockstore/mockcopr/BUILD.bazel b/store/mockstore/mockcopr/BUILD.bazel index f881d161790a5..86ed50c32a984 100644 --- a/store/mockstore/mockcopr/BUILD.bazel +++ b/store/mockstore/mockcopr/BUILD.bazel @@ -49,7 +49,6 @@ go_test( name = "mockcopr_test", timeout = "short", srcs = [ - "cop_handler_dag_test.go", "executor_test.go", "main_test.go", ], diff --git a/store/mockstore/mockcopr/analyze.go b/store/mockstore/mockcopr/analyze.go index a5548b25264f4..33f37b9be4b04 100644 --- a/store/mockstore/mockcopr/analyze.go +++ b/store/mockstore/mockcopr/analyze.go @@ -31,6 +31,7 @@ import ( "github.com/pingcap/tidb/util/codec" "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/rowcodec" + "github.com/pingcap/tidb/util/timeutil" "github.com/pingcap/tipb/go-tipb" ) @@ -128,7 +129,7 @@ type analyzeColumnsExec struct { func (h coprHandler) handleAnalyzeColumnsReq(req *coprocessor.Request, analyzeReq *tipb.AnalyzeReq) (_ *coprocessor.Response, err error) { sc := flagsToStatementContext(analyzeReq.Flags) - sc.TimeZone, err = constructTimeZone("", int(analyzeReq.TimeZoneOffset)) + sc.TimeZone, err = timeutil.ConstructTimeZone("", int(analyzeReq.TimeZoneOffset)) if err != nil { return nil, errors.Trace(err) } diff --git a/store/mockstore/mockcopr/cop_handler_dag.go b/store/mockstore/mockcopr/cop_handler_dag.go index d9e8f84646fc7..399e424323966 100644 --- a/store/mockstore/mockcopr/cop_handler_dag.go +++ b/store/mockstore/mockcopr/cop_handler_dag.go @@ -104,7 +104,7 @@ func (h coprHandler) buildDAGExecutor(req *coprocessor.Request) (*dagContext, ex } sc := flagsToStatementContext(dagReq.Flags) - sc.TimeZone, err = constructTimeZone(dagReq.TimeZoneName, int(dagReq.TimeZoneOffset)) + sc.TimeZone, err = timeutil.ConstructTimeZone(dagReq.TimeZoneName, int(dagReq.TimeZoneOffset)) if err != nil { return nil, nil, nil, errors.Trace(err) } diff --git a/store/mockstore/mockcopr/cop_handler_dag_test.go b/store/mockstore/mockcopr/cop_handler_dag_test.go deleted file mode 100644 index 5b11a668b854c..0000000000000 --- a/store/mockstore/mockcopr/cop_handler_dag_test.go +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2018-present, PingCAP, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package mockcopr - -import ( - "testing" - "time" - - "github.com/stretchr/testify/require" -) - -func TestConstructTimezone(t *testing.T) { - secondsEastOfUTC := int((8 * time.Hour).Seconds()) - loc, err := constructTimeZone("", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc := time.Date(2018, 8, 15, 20, 0, 0, 0, loc) - timeInUTC := time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - secondsEastOfUTC = int((-8 * time.Hour).Seconds()) - loc, err = constructTimeZone("", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 20, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - secondsEastOfUTC = 0 - loc, err = constructTimeZone("", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 20, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the seconds east of UTC is ignored by the function - // constructTimeZone(). - secondsEastOfUTC = int((23 * time.Hour).Seconds()) - loc, err = constructTimeZone("UTC", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the seconds east of UTC is ignored by the function - // constructTimeZone(). - secondsEastOfUTC = int((-23 * time.Hour).Seconds()) - loc, err = constructTimeZone("UTC", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the seconds east of UTC is ignored by the function - // constructTimeZone(). - loc, err = constructTimeZone("UTC", 0) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the seconds east of UTC is ignored by the function - // constructTimeZone(). - secondsEastOfUTC = int((-23 * time.Hour).Seconds()) - loc, err = constructTimeZone("Asia/Shanghai", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the seconds east of UTC is ignored by the function - // constructTimeZone(). - secondsEastOfUTC = int((23 * time.Hour).Seconds()) - loc, err = constructTimeZone("Asia/Shanghai", secondsEastOfUTC) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the seconds east of UTC is ignored by the function - // constructTimeZone(). - loc, err = constructTimeZone("Asia/Shanghai", 0) - require.NoError(t, err) - timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) - timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) - require.True(t, timeInLoc.Equal(timeInUTC)) - - // test the timezone name is not existed. - _, err = constructTimeZone("asia/not-exist", 0) - require.EqualError(t, err, "invalid name for timezone asia/not-exist") -} diff --git a/util/timeutil/time_zone_test.go b/util/timeutil/time_zone_test.go index 7019aca3721c8..83f24ad486f7e 100644 --- a/util/timeutil/time_zone_test.go +++ b/util/timeutil/time_zone_test.go @@ -23,6 +23,7 @@ import ( "path/filepath" "strings" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -131,3 +132,82 @@ func TestParseTimeZone(t *testing.T) { require.Equal(t, c.offset, offset, c.name) } } + +func TestConstructTimeZone(t *testing.T) { + secondsEastOfUTC := int((8 * time.Hour).Seconds()) + loc, err := ConstructTimeZone("", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc := time.Date(2018, 8, 15, 20, 0, 0, 0, loc) + timeInUTC := time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + secondsEastOfUTC = int((-8 * time.Hour).Seconds()) + loc, err = ConstructTimeZone("", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 20, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + secondsEastOfUTC = 0 + loc, err = ConstructTimeZone("", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 20, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the seconds east of UTC is ignored by the function + // ConstructTimeZone(). + secondsEastOfUTC = int((23 * time.Hour).Seconds()) + loc, err = ConstructTimeZone("UTC", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the seconds east of UTC is ignored by the function + // ConstructTimeZone(). + secondsEastOfUTC = int((-23 * time.Hour).Seconds()) + loc, err = ConstructTimeZone("UTC", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the seconds east of UTC is ignored by the function + // ConstructTimeZone(). + loc, err = ConstructTimeZone("UTC", 0) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 12, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the seconds east of UTC is ignored by the function + // ConstructTimeZone(). + secondsEastOfUTC = int((-23 * time.Hour).Seconds()) + loc, err = ConstructTimeZone("Asia/Shanghai", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the seconds east of UTC is ignored by the function + // ConstructTimeZone(). + secondsEastOfUTC = int((23 * time.Hour).Seconds()) + loc, err = ConstructTimeZone("Asia/Shanghai", secondsEastOfUTC) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the seconds east of UTC is ignored by the function + // ConstructTimeZone(). + loc, err = ConstructTimeZone("Asia/Shanghai", 0) + require.NoError(t, err) + timeInLoc = time.Date(2018, 8, 15, 20, 0, 0, 0, loc) + timeInUTC = time.Date(2018, 8, 15, 12, 0, 0, 0, time.UTC) + require.True(t, timeInLoc.Equal(timeInUTC)) + + // test the timezone name is not existed. + _, err = ConstructTimeZone("asia/not-exist", 0) + require.EqualError(t, err, "invalid name for timezone asia/not-exist") +}