Skip to content

Commit

Permalink
mysql: fix time parse (pingcap#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Aug 16, 2016
1 parent 4a2f5ba commit 6ccbfb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 10 additions & 8 deletions mysql/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,19 +815,21 @@ func splitDuration(t time.Duration) (int, int, int, int, int) {
return sign, int(hours), int(minutes), int(seconds), int(fraction)
}

var maxDaysInMonth = []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

func checkTime(year int, month int, day int, hour int, minute int, second int, frac int) error {
// Notes: for datetime type, `insert t values("0001-01-01 00:00:00");` is valid
// so here only check year from 0~9999.
if (year < 0 || year > 9999) ||
(month <= 0 || month > 12) ||
(day <= 0 || day > 31) ||
(hour < 0 || hour >= 24) ||
(minute < 0 || minute >= 60) ||
(second < 0 || second >= 60) ||
(frac < 0) {
if year < 0 || year > 9999 ||
month <= 0 || month > 12 ||
day <= 0 || day > maxDaysInMonth[month-1] ||
(month == 2 && day == 29 && year%4 != 0) ||
hour < 0 || hour >= 24 ||
minute < 0 || minute >= 60 ||
second < 0 || second >= 60 ||
frac < 0 {
return errors.Trace(ErrInvalidTimeFormat)
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions mysql/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (s *testTimeSuite) TestDateTime(c *C) {
{"12-2-1 11:30:45", "2012-02-01 11:30:45"},
{"20121231113045", "2012-12-31 11:30:45"},
{"121231113045", "2012-12-31 11:30:45"},
{"2012-02-29", "2012-02-29 00:00:00"},
}

for _, test := range table {
Expand Down Expand Up @@ -80,6 +81,8 @@ func (s *testTimeSuite) TestDateTime(c *C) {
"1000-01-01 00:00:70",
"1000-13-00 00:00:00",
"10000-01-01 00:00:00",
"1000-09-31 00:00:00",
"1001-02-29 00:00:00",
}

for _, test := range errTable {
Expand Down

0 comments on commit 6ccbfb2

Please sign in to comment.