Skip to content

Commit

Permalink
types: fix time_format is not compatible with MySQL (#9841) (#10474)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored and zz-jason committed May 15, 2019
1 parent 5b887f0 commit 205418a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,12 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result.Check(testkit.Rows("<nil>"))
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');`)
Expand Down
5 changes: 3 additions & 2 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down

0 comments on commit 205418a

Please sign in to comment.