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 checkMonthDay() to correctly handle leap year (#10342) #10417

Merged
merged 6 commits into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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)
}
}
}