diff --git a/expression/integration_test.go b/expression/integration_test.go index d52d49651412e..bad171ee28de2 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -1515,6 +1515,12 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) { result.Check(testkit.Rows("")) result = tk.MustQuery("SELECT TIME_FORMAT(123, '%H:%i:%s %p');") result.Check(testkit.Rows("00:01:23 AM")) + result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%r');") + result.Check(testkit.Rows("12:00:00 AM")) + result = tk.MustQuery("SELECT TIME_FORMAT('25:00:00', '%r');") + result.Check(testkit.Rows("01:00:00 AM")) + result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%l %p');") + result.Check(testkit.Rows("12 AM")) // for date_format result = tk.MustQuery(`SELECT DATE_FORMAT('2017-06-15', '%W %M %e %Y %r %y');`) diff --git a/types/time.go b/types/time.go index 7969a867c496e..cb96bb917edda 100644 --- a/types/time.go +++ b/types/time.go @@ -1921,14 +1921,14 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error { fmt.Fprintf(buf, "%d", t.Time.Hour()) case 'h', 'I': t := t.Time.Hour() - if t == 0 || t == 12 { + if t%12 == 0 { fmt.Fprintf(buf, "%02d", 12) } else { fmt.Fprintf(buf, "%02d", t%12) } case 'l': t := t.Time.Hour() - if t == 0 || t == 12 { + if t%12 == 0 { fmt.Fprintf(buf, "%d", 12) } else { fmt.Fprintf(buf, "%d", t%12) @@ -1944,6 +1944,7 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error { } case 'r': h := t.Time.Hour() + h %= 24 switch { case h == 0: fmt.Fprintf(buf, "%02d:%02d:%02d AM", 12, t.Time.Minute(), t.Time.Second())