diff --git a/expression/builtin_time_test.go b/expression/builtin_time_test.go index 9bf8cc788e94e..0025aaf5ff394 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -1257,6 +1257,9 @@ func (s *testEvaluatorSuite) TestStrToDate(c *C) { Success bool Expect time.Time }{ + {"10/28/2011 9:46:29 pm", "%m/%d/%Y %l:%i:%s %p", true, time.Date(2011, 10, 28, 21, 46, 29, 0, time.Local)}, + {"10/28/2011 9:46:29 Pm", "%m/%d/%Y %l:%i:%s %p", true, time.Date(2011, 10, 28, 21, 46, 29, 0, time.Local)}, + {"2011/10/28 9:46:29 am", "%Y/%m/%d %l:%i:%s %p", true, time.Date(2011, 10, 28, 9, 46, 29, 0, time.Local)}, {"20161122165022", `%Y%m%d%H%i%s`, true, time.Date(2016, 11, 22, 16, 50, 22, 0, time.Local)}, {"2016 11 22 16 50 22", `%Y%m%d%H%i%s`, true, time.Date(2016, 11, 22, 16, 50, 22, 0, time.Local)}, {"16-50-22 2016 11 22", `%H-%i-%s%Y%m%d`, true, time.Date(2016, 11, 22, 16, 50, 22, 0, time.Local)}, diff --git a/types/time.go b/types/time.go index ca7ec7c158f87..341137cb82e13 100644 --- a/types/time.go +++ b/types/time.go @@ -2355,11 +2355,17 @@ const ( ) func isAMOrPM(t *MysqlTime, input string, ctx map[string]int) (string, bool) { - if strings.HasPrefix(input, "AM") { + if len(input) < 2 { + return input, false + } + + s := strings.ToLower(input[:2]) + switch s { + case "am": ctx["%p"] = constForAM - } else if strings.HasPrefix(input, "PM") { + case "pm": ctx["%p"] = constForPM - } else { + default: return input, false } return input[2:], true