Skip to content

Commit

Permalink
types: Fix potential timezone related bugs caused by gotime.Local (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin-bot authored and XiaTianliang committed Dec 21, 2019
1 parent 48a31f3 commit b967ff3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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"},
{"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

0 comments on commit b967ff3

Please sign in to comment.