go-mysql-org / go-mysql

a powerful mysql toolset with Go
MIT License
4.52k stars 967 forks source link

time column type string formatting and test coverage #891

Closed jnewmano closed 1 month ago

jnewmano commented 1 month ago

This PR improves the string formatting of time type columns, FormatBinaryTime does not return properly formatted time strings in all cases.

In the zero value case, it was incorrectly returning a date formatted string instead of the expected zero time of 00:00:00. For positive times there was a leading null byte in the returned []byte.

CREATE TABLE time_test (ts TIME); INSERT INTO time_test (ts) VALUES ("00:00:00"), ("06:12:10");

Reading the values out (using a statement as direct queries don't use FormatBinaryTime)

SELECT * FROM %s WHERE ts > ? with query parameter -5 or some other number

resulted in

"0000-00-00"
"\x0006:12:10"

when the expected result is

"00:00:00"
"06:12:10"