Skip to content

Commit

Permalink
expression,types: improve str_to_date builtin function compatibility (
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored and ngaut committed Mar 20, 2019
1 parent 346009f commit 2129872
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
12 changes: 9 additions & 3 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2129872

Please sign in to comment.