From b9abc3a6d28161daa4c53d46dd75bdf87a67fdb5 Mon Sep 17 00:00:00 2001 From: baishen Date: Fri, 12 Jul 2019 15:10:31 +0800 Subject: [PATCH] types: fix incorrect `weekday` for `ALLOW_INVALID_DATES` mode (#10864) --- types/format_test.go | 2 +- types/mytime.go | 3 ++- types/mytime_test.go | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/types/format_test.go b/types/format_test.go index 493ac31e75ccf..d4434dc301a8c 100644 --- a/types/format_test.go +++ b/types/format_test.go @@ -62,7 +62,7 @@ func (s *testTimeSuite) TestTimeFormatMethod(c *C) { // %W %w %a is not compatible in this case because Week() use GoTime() currently. "0000-01-00 00:00:00.123456", `%b %M %m %c %D %d %e %j %k %h %i %p %r %T %s %f %U %u %V %v %a %W %w %X %x %Y %y %%`, - `Jan January 01 1 0th 00 0 000 0 12 00 AM 12:00:00 AM 00:00:00 00 123456 00 00 00 52 Sun Sunday 0 4294967295 4294967295 0000 00 %`, + `Jan January 01 1 0th 00 0 000 0 12 00 AM 12:00:00 AM 00:00:00 00 123456 00 00 00 52 Fri Friday 5 4294967295 4294967295 0000 00 %`, }, } for i, t := range tblDate { diff --git a/types/mytime.go b/types/mytime.go index 5ee12ec20aa78..733571f0239a1 100644 --- a/types/mytime.go +++ b/types/mytime.go @@ -70,8 +70,9 @@ func (t MysqlTime) Microsecond() int { func (t MysqlTime) Weekday() gotime.Weekday { // TODO: Consider time_zone variable. t1, err := t.GoTime(gotime.Local) + // allow invalid dates if err != nil { - return 0 + return t1.Weekday() } return t1.Weekday() } diff --git a/types/mytime_test.go b/types/mytime_test.go index f2897c878908b..76a32f4b40a94 100644 --- a/types/mytime_test.go +++ b/types/mytime_test.go @@ -271,3 +271,19 @@ func (s *testMyTimeSuite) TestAddDate(c *C) { c.Assert(res.Year(), Equals, t.year+t.ot.Year()) } } + +func (s *testMyTimeSuite) TestWeekday(c *C) { + tests := []struct { + Input MysqlTime + Expect string + }{ + {MysqlTime{2019, 01, 01, 0, 0, 0, 0}, "Tuesday"}, + {MysqlTime{2019, 02, 31, 0, 0, 0, 0}, "Sunday"}, + {MysqlTime{2019, 04, 31, 0, 0, 0, 0}, "Wednesday"}, + } + + for _, tt := range tests { + weekday := tt.Input.Weekday() + c.Check(weekday.String(), Equals, tt.Expect) + } +}