Skip to content

Commit 205418a

Browse files
qw4990zz-jason
authored andcommitted
types: fix time_format is not compatible with MySQL (#9841) (#10474)
1 parent 5b887f0 commit 205418a

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
@@ -1476,6 +1476,12 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
14761476
result.Check(testkit.Rows("<nil>"))
14771477
result = tk.MustQuery("SELECT TIME_FORMAT(123, '%H:%i:%s %p');")
14781478
result.Check(testkit.Rows("00:01:23 AM"))
1479+
result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%r');")
1480+
result.Check(testkit.Rows("12:00:00 AM"))
1481+
result = tk.MustQuery("SELECT TIME_FORMAT('25:00:00', '%r');")
1482+
result.Check(testkit.Rows("01:00:00 AM"))
1483+
result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%l %p');")
1484+
result.Check(testkit.Rows("12 AM"))
14791485

14801486
// for date_format
14811487
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
@@ -1866,14 +1866,14 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error {
18661866
fmt.Fprintf(buf, "%d", t.Time.Hour())
18671867
case 'h', 'I':
18681868
t := t.Time.Hour()
1869-
if t == 0 || t == 12 {
1869+
if t%12 == 0 {
18701870
fmt.Fprintf(buf, "%02d", 12)
18711871
} else {
18721872
fmt.Fprintf(buf, "%02d", t%12)
18731873
}
18741874
case 'l':
18751875
t := t.Time.Hour()
1876-
if t == 0 || t == 12 {
1876+
if t%12 == 0 {
18771877
fmt.Fprintf(buf, "%d", 12)
18781878
} else {
18791879
fmt.Fprintf(buf, "%d", t%12)
@@ -1889,6 +1889,7 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error {
18891889
}
18901890
case 'r':
18911891
h := t.Time.Hour()
1892+
h %= 24
18921893
switch {
18931894
case h == 0:
18941895
fmt.Fprintf(buf, "%02d:%02d:%02d AM", 12, t.Time.Minute(), t.Time.Second())

0 commit comments

Comments
 (0)