Skip to content

Commit

Permalink
types: Fix checkMonthDay() to correctly handle leap year (#10342) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing authored and zz-jason committed May 11, 2019
1 parent 6cb7bd1 commit 7370996
Show file tree
Hide file tree
Showing 2 changed files with 41 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 @@ -1465,7 +1465,7 @@ func checkMonthDay(year, month, day int, allowInvalidDate bool) error {
if month > 0 {
maxDay = maxDaysInMonth[month-1]
}
if month == 2 && year%4 != 0 {
if month == 2 && !isLeapYear(uint16(year)) {
maxDay = 28
}
}
Expand Down
40 changes: 40 additions & 0 deletions types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,3 +1342,43 @@ func (s *testTimeSuite) TestgetFracIndex(c *C) {
c.Assert(index, Equals, testCase.expectIndex)
}
}

func (s *testTimeSuite) TestCheckMonthDay(c *C) {
dates := []struct {
date types.MysqlTime
isValidDate bool
}{
{types.FromDate(1900, 2, 29, 0, 0, 0, 0), false},
{types.FromDate(1900, 2, 28, 0, 0, 0, 0), true},
{types.FromDate(2000, 2, 29, 0, 0, 0, 0), true},
{types.FromDate(2000, 1, 1, 0, 0, 0, 0), true},
{types.FromDate(1900, 1, 1, 0, 0, 0, 0), true},
{types.FromDate(1900, 1, 31, 0, 0, 0, 0), true},
{types.FromDate(1900, 4, 1, 0, 0, 0, 0), true},
{types.FromDate(1900, 4, 31, 0, 0, 0, 0), false},
{types.FromDate(1900, 4, 30, 0, 0, 0, 0), true},
{types.FromDate(2000, 2, 30, 0, 0, 0, 0), false},
{types.FromDate(2000, 13, 1, 0, 0, 0, 0), false},
{types.FromDate(4000, 2, 29, 0, 0, 0, 0), true},
{types.FromDate(3200, 2, 29, 0, 0, 0, 0), true},
}

sc := &stmtctx.StatementContext{
TimeZone: time.UTC,
AllowInvalidDate: false,
}

for _, t := range dates {
tt := types.Time{
Time: t.date,
Type: mysql.TypeDate,
Fsp: types.DefaultFsp,
}
err := tt.Check(sc)
if t.isValidDate {
c.Check(err, IsNil)
} else {
c.Check(types.ErrInvalidTimeFormat.Equal(err), IsTrue)
}
}
}

0 comments on commit 7370996

Please sign in to comment.