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

types: Fix potential timezone related bugs caused by gotime.Local #13752

Merged
merged 5 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,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 int8
Except string
}{
{"2019-11-25 07:25:45.123456", 4, "2019-11-25 07:25:45.1235"},
bb7133 marked this conversation as resolved.
Show resolved Hide resolved
{"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)
Expand Down