Skip to content

Commit

Permalink
types: Fix "Invalid time format" caused by daily saving time (#13614) (
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and bb7133 committed Nov 21, 2019
1 parent a215408 commit 39b8370
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ func checkTimestampType(sc *stmtctx.StatementContext, t MysqlTime) error {
return errors.Trace(ErrInvalidTimeFormat.GenWithStackByArgs(t))
}

if _, err := t.GoTime(gotime.Local); err != nil {
if _, err := t.GoTime(sc.TimeZone); err != nil {
return errors.Trace(err)
}

Expand Down
61 changes: 59 additions & 2 deletions types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,66 @@ func (s *testTimeSuite) TestCheckTimestamp(c *C) {
for _, t := range tests {
validTimestamp := types.CheckTimestampTypeForTest(&stmtctx.StatementContext{TimeZone: t.tz}, t.input)
if t.expectRetError {
c.Assert(validTimestamp, NotNil, Commentf("For %s", t.input))
c.Assert(validTimestamp, NotNil, Commentf("For %s %s", t.input, t.tz))
} else {
c.Assert(validTimestamp, IsNil, Commentf("For %s", t.input))
c.Assert(validTimestamp, IsNil, Commentf("For %s %s", t.input, t.tz))
}
}

// Issue #13605: "Invalid time format" caused by time zone issue
// Some regions like Los Angeles use daylight saving time, see https://en.wikipedia.org/wiki/Daylight_saving_time
losAngelesTz, _ := time.LoadLocation("America/Los_Angeles")
LondonTz, _ := time.LoadLocation("Europe/London")

tests = []struct {
tz *time.Location
input types.MysqlTime
expectRetError bool
}{{
tz: losAngelesTz,
input: types.FromDate(2018, 3, 11, 1, 0, 50, 0),
expectRetError: false,
}, {
tz: losAngelesTz,
input: types.FromDate(2018, 3, 11, 2, 0, 16, 0),
expectRetError: true,
}, {
tz: losAngelesTz,
input: types.FromDate(2018, 3, 11, 3, 0, 20, 0),
expectRetError: false,
}, {
tz: shanghaiTz,
input: types.FromDate(2018, 3, 11, 1, 0, 50, 0),
expectRetError: false,
}, {
tz: shanghaiTz,
input: types.FromDate(2018, 3, 11, 2, 0, 16, 0),
expectRetError: false,
}, {
tz: shanghaiTz,
input: types.FromDate(2018, 3, 11, 3, 0, 20, 0),
expectRetError: false,
}, {
tz: LondonTz,
input: types.FromDate(2019, 3, 31, 0, 0, 20, 0),
expectRetError: false,
}, {
tz: LondonTz,
input: types.FromDate(2019, 3, 31, 1, 0, 20, 0),
expectRetError: true,
}, {
tz: LondonTz,
input: types.FromDate(2019, 3, 31, 2, 0, 20, 0),
expectRetError: false,
},
}

for _, t := range tests {
validTimestamp := types.CheckTimestampTypeForTest(&stmtctx.StatementContext{TimeZone: t.tz}, t.input)
if t.expectRetError {
c.Assert(validTimestamp, NotNil, Commentf("For %s %s", t.input, t.tz))
} else {
c.Assert(validTimestamp, IsNil, Commentf("For %s %s", t.input, t.tz))
}
}
}
Expand Down

0 comments on commit 39b8370

Please sign in to comment.