Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: fix potential timezone related bugs caused by time.Local (#14572) #16186

Merged
merged 8 commits into from
Apr 22, 2020
20 changes: 3 additions & 17 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"strconv"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -201,16 +200,7 @@ func dumpBinaryTime(dur time.Duration) (data []byte) {
return
}

func dumpBinaryDateTime(data []byte, t types.Time, loc *time.Location) ([]byte, error) {
if t.Type == mysql.TypeTimestamp && loc != nil {
// TODO: Consider time_zone variable.
t1, err := t.Time.GoTime(time.Local)
if err != nil {
return nil, errors.Errorf("FATAL: convert timestamp %v go time return error!", t.Time)
}
t.Time = types.FromGoTime(t1.In(loc))
}

func dumpBinaryDateTime(data []byte, t types.Time) []byte {
year, mon, day := t.Time.Year(), t.Time.Month(), t.Time.Day()
switch t.Type {
case mysql.TypeTimestamp, mysql.TypeDatetime:
Expand All @@ -223,7 +213,7 @@ func dumpBinaryDateTime(data []byte, t types.Time, loc *time.Location) ([]byte,
data = dumpUint16(data, uint16(year)) //year
data = append(data, byte(mon), byte(day))
}
return data, nil
return data
}

func dumpBinaryRow(buffer []byte, columns []*ColumnInfo, row chunk.Row) ([]byte, error) {
Expand Down Expand Up @@ -259,11 +249,7 @@ func dumpBinaryRow(buffer []byte, columns []*ColumnInfo, row chunk.Row) ([]byte,
mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob, mysql.TypeBlob:
buffer = dumpLengthEncodedString(buffer, row.GetBytes(i))
case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
var err error
buffer, err = dumpBinaryDateTime(buffer, row.GetTime(i), nil)
if err != nil {
return buffer, err
}
buffer = dumpBinaryDateTime(buffer, row.GetTime(i))
case mysql.TypeDuration:
buffer = append(buffer, dumpBinaryTime(row.GetDuration(i, 0).Duration)...)
case mysql.TypeEnum:
Expand Down
9 changes: 3 additions & 6 deletions server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,16 @@ func (s *testUtilSuite) TearDownSuite(c *C) {
func (s *testUtilSuite) TestDumpBinaryTime(c *C) {
t, err := types.ParseTimestamp(nil, "0000-00-00 00:00:00.0000000")
c.Assert(err, IsNil)
d, err := dumpBinaryDateTime(nil, t, nil)
c.Assert(err, IsNil)
d := dumpBinaryDateTime(nil, t)
c.Assert(d, DeepEquals, []byte{11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
t, err = types.ParseDatetime(nil, "0000-00-00 00:00:00.0000000")
c.Assert(err, IsNil)
d, err = dumpBinaryDateTime(nil, t, nil)
c.Assert(err, IsNil)
d = dumpBinaryDateTime(nil, t)
c.Assert(d, DeepEquals, []byte{11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})

t, err = types.ParseDate(nil, "0000-00-00")
c.Assert(err, IsNil)
d, err = dumpBinaryDateTime(nil, t, nil)
c.Assert(err, IsNil)
d = dumpBinaryDateTime(nil, t)
c.Assert(d, DeepEquals, []byte{4, 0, 0, 0, 0})

myDuration, err := types.ParseDuration(nil, "0000-00-00 00:00:00.0000000", 6)
Expand Down