From c6edfb2cd89fae170bd54b703ae96ff13db3a069 Mon Sep 17 00:00:00 2001 From: "Zhuomin(Charming) Liu" Date: Mon, 25 Mar 2019 13:47:16 +0800 Subject: [PATCH] types: fix `time_format` is not compatible with MySQL (#9841) --- expression/integration_test.go | 6 ++++++ types/time.go | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 8190f6be87a01..6f76882eee0d0 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -1476,6 +1476,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 84ad2a69b66ef..6046ee5ce0f3c 100644 --- a/types/time.go +++ b/types/time.go @@ -1866,14 +1866,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) @@ -1889,6 +1889,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())