Skip to content

Commit

Permalink
mockcopr: remove the constructTimeZone function (pingcap#47578)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangKeao authored Oct 12, 2023
1 parent 12c5909 commit 14d3cb5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 104 deletions.
1 change: 0 additions & 1 deletion store/mockstore/mockcopr/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ go_test(
name = "mockcopr_test",
timeout = "short",
srcs = [
"cop_handler_dag_test.go",
"executor_test.go",
"main_test.go",
],
Expand Down
3 changes: 2 additions & 1 deletion store/mockstore/mockcopr/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion store/mockstore/mockcopr/cop_handler_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
101 changes: 0 additions & 101 deletions store/mockstore/mockcopr/cop_handler_dag_test.go

This file was deleted.

80 changes: 80 additions & 0 deletions util/timeutil/time_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -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")
}

0 comments on commit 14d3cb5

Please sign in to comment.