Skip to content

Commit 99defc0

Browse files
lzmhhh123zz-jason
authored andcommitted
types: fix time_format is not compatible with MySQL (#9841)
1 parent 630671e commit 99defc0

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

expression/integration_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,12 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
15151515
result.Check(testkit.Rows("<nil>"))
15161516
result = tk.MustQuery("SELECT TIME_FORMAT(123, '%H:%i:%s %p');")
15171517
result.Check(testkit.Rows("00:01:23 AM"))
1518+
result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%r');")
1519+
result.Check(testkit.Rows("12:00:00 AM"))
1520+
result = tk.MustQuery("SELECT TIME_FORMAT('25:00:00', '%r');")
1521+
result.Check(testkit.Rows("01:00:00 AM"))
1522+
result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%l %p');")
1523+
result.Check(testkit.Rows("12 AM"))
15181524

15191525
// for date_format
15201526
result = tk.MustQuery(`SELECT DATE_FORMAT('2017-06-15', '%W %M %e %Y %r %y');`)

types/time.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,14 +1921,14 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error {
19211921
fmt.Fprintf(buf, "%d", t.Time.Hour())
19221922
case 'h', 'I':
19231923
t := t.Time.Hour()
1924-
if t == 0 || t == 12 {
1924+
if t%12 == 0 {
19251925
fmt.Fprintf(buf, "%02d", 12)
19261926
} else {
19271927
fmt.Fprintf(buf, "%02d", t%12)
19281928
}
19291929
case 'l':
19301930
t := t.Time.Hour()
1931-
if t == 0 || t == 12 {
1931+
if t%12 == 0 {
19321932
fmt.Fprintf(buf, "%d", 12)
19331933
} else {
19341934
fmt.Fprintf(buf, "%d", t%12)
@@ -1944,6 +1944,7 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error {
19441944
}
19451945
case 'r':
19461946
h := t.Time.Hour()
1947+
h %= 24
19471948
switch {
19481949
case h == 0:
19491950
fmt.Fprintf(buf, "%02d:%02d:%02d AM", 12, t.Time.Minute(), t.Time.Second())

0 commit comments

Comments
 (0)