From 15197bd7433161a8dd5634f8748b5520b38c26d4 Mon Sep 17 00:00:00 2001 From: Rustin-liu Date: Tue, 26 Nov 2019 19:19:20 +0800 Subject: [PATCH 1/3] types: Fix potential timezone related bugs caused by `gotime.Local` --- types/time.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/time.go b/types/time.go index 3127aa32bb696..3001744ae76a4 100644 --- a/types/time.go +++ b/types/time.go @@ -430,7 +430,7 @@ func (t Time) RoundFrac(sc *stmtctx.StatementContext, fsp int8) (Time, error) { } else { // Take the hh:mm:ss part out to avoid handle month or day = 0. hour, minute, second, microsecond := t.Time.Hour(), t.Time.Minute(), t.Time.Second(), t.Time.Microsecond() - t1 := gotime.Date(1, 1, 1, hour, minute, second, microsecond*1000, gotime.Local) + t1 := gotime.Date(1, 1, 1, hour, minute, second, microsecond*1000, sc.TimeZone) t2 := roundTime(t1, fsp) hour, minute, second = t2.Clock() microsecond = t2.Nanosecond() / 1000 From 851ddf5db27aad44d8cfcdcf9ddd1156f5bb14fc Mon Sep 17 00:00:00 2001 From: Rustin-liu Date: Tue, 26 Nov 2019 19:29:30 +0800 Subject: [PATCH 2/3] add more test for RoundFrac --- types/time_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/types/time_test.go b/types/time_test.go index f9e3dd20a57ff..ac2cf8dcf305b 100644 --- a/types/time_test.go +++ b/types/time_test.go @@ -733,6 +733,27 @@ func (s *testTimeSuite) TestRoundFrac(c *C) { // {"2012-01-00 23:59:59.999999", 3, "2012-01-01 00:00:00.000"}, } + for _, t := range tbl { + v, err := types.ParseTime(sc, t.Input, mysql.TypeDatetime, types.MaxFsp) + c.Assert(err, IsNil) + nv, err := v.RoundFrac(sc, t.Fsp) + c.Assert(err, IsNil) + c.Assert(nv.String(), Equals, t.Except) + } + // test different time zone + losAngelesTz, _ := time.LoadLocation("America/Los_Angeles") + sc.TimeZone = losAngelesTz + tbl = []struct { + Input string + Fsp int8 + Except string + }{ + {"2019-11-25 07:25:45.123456", 4, "2019-11-25 07:25:45.1235"}, + {"2019-11-25 07:25:45.123456", 5, "2019-11-25 07:25:45.12346"}, + {"2019-11-25 07:25:45.123456", 0, "2019-11-25 07:25:45"}, + {"2019-11-25 07:25:45.123456", 2, "2019-11-25 07:25:45.12"}, + } + for _, t := range tbl { v, err := types.ParseTime(sc, t.Input, mysql.TypeDatetime, types.MaxFsp) c.Assert(err, IsNil) From 425495d0cff38efa43356d9d2d750c482d9361a0 Mon Sep 17 00:00:00 2001 From: Rustin-liu Date: Wed, 27 Nov 2019 11:17:22 +0800 Subject: [PATCH 3/3] add more test case for RoundFrac --- types/time_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/types/time_test.go b/types/time_test.go index ac2cf8dcf305b..22a9c98806718 100644 --- a/types/time_test.go +++ b/types/time_test.go @@ -752,6 +752,9 @@ func (s *testTimeSuite) TestRoundFrac(c *C) { {"2019-11-25 07:25:45.123456", 5, "2019-11-25 07:25:45.12346"}, {"2019-11-25 07:25:45.123456", 0, "2019-11-25 07:25:45"}, {"2019-11-25 07:25:45.123456", 2, "2019-11-25 07:25:45.12"}, + {"2019-11-26 11:30:45.999999", 4, "2019-11-26 11:30:46.0000"}, + {"2019-11-26 11:30:45.999999", 0, "2019-11-26 11:30:46"}, + {"2019-11-26 11:30:45.999999", 3, "2019-11-26 11:30:46.000"}, } for _, t := range tbl {