From 16781a3fe915ad4e9778a0b10c83c3a92ccd6ae9 Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Thu, 28 Nov 2019 11:08:08 +0800 Subject: [PATCH] types: Fix potential timezone related bugs caused by `gotime.Local` (#13752) (#13793) --- types/time.go | 2 +- types/time_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/types/time.go b/types/time.go index 53b20d57b1ff4..36d8f7fd35ad4 100644 --- a/types/time.go +++ b/types/time.go @@ -425,7 +425,7 @@ func (t Time) RoundFrac(sc *stmtctx.StatementContext, fsp int) (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 diff --git a/types/time_test.go b/types/time_test.go index 4d23f198b683b..0ea602f7080c1 100644 --- a/types/time_test.go +++ b/types/time_test.go @@ -732,6 +732,30 @@ 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 int + 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"}, + {"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 { v, err := types.ParseTime(sc, t.Input, mysql.TypeDatetime, types.MaxFsp) c.Assert(err, IsNil)