From 10510927bd93c5bec7238ebb1735547105f93481 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Sat, 9 Mar 2019 15:15:31 +0800 Subject: [PATCH] expression,types: improve `str_to_date` builtin function compatibility (#9617) --- expression/builtin_time_test.go | 3 +++ types/time.go | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/expression/builtin_time_test.go b/expression/builtin_time_test.go index 5f51be8c24dc1..06d4ad1663a09 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -1244,6 +1244,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 2ea24cf5057cb..5010297404052 100644 --- a/types/time.go +++ b/types/time.go @@ -2346,11 +2346,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