Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append only time #1240

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,12 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
buf = append(buf, '0')
}
case time.Time:
if v.IsZero() {
buf = append(buf, "'0000-00-00'"...)
} else {
buf = append(buf, '\'')
buf, err = appendDateTime(buf, v.In(mc.cfg.Loc))
if err != nil {
return "", err
}
buf = append(buf, '\'')
buf = append(buf, '\'')
buf, err = appendTime(buf, v.In(mc.cfg.Loc))
if err != nil {
return "", err
}
buf = append(buf, '\'')
case json.RawMessage:
buf = append(buf, '\'')
if mc.status&statusNoBackslashEscapes == 0 {
Expand Down
10 changes: 3 additions & 7 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,13 +1113,9 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
var a [64]byte
var b = a[:0]

if v.IsZero() {
b = append(b, "0000-00-00"...)
} else {
b, err = appendDateTime(b, v.In(mc.cfg.Loc))
if err != nil {
return err
}
b, err = appendTime(b, v.In(mc.cfg.Loc))
if err != nil {
return err
}

paramValues = appendLengthEncodedInteger(paramValues,
Expand Down
19 changes: 18 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,29 @@ func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Va
return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
}

// appendTime serializes a time.Time t and appends it to buf.
func appendTime(buf []byte, t time.Time) ([]byte, error) {
// this is the layout string for the "time of day" format of the MySQL TIME type
// see https://dev.mysql.com/doc/refman/8.0/en/time.html
const mysqlTimeLayout = "15:04:05"

switch {
case t.IsZero():
return append(buf, "0000-00-00"...), nil
case t.Year() == 0 && t.Month() == 1 && t.Day() == 1:
return t.AppendFormat(buf, mysqlTimeLayout), nil
default:
return appendDateTime(buf, t)
}
}

// appendDateTime is a special case of appendTime.
func appendDateTime(buf []byte, t time.Time) ([]byte, error) {
year, month, day := t.Date()
hour, min, sec := t.Clock()
nsec := t.Nanosecond()

if year < 1 || year > 9999 {
if year < 0 || year > 9999 {
return buf, errors.New("year is not in the range [1, 9999]: " + strconv.Itoa(year)) // use errors.New instead of fmt.Errorf to avoid year escape to heap
}
year100 := year / 100
Expand Down
23 changes: 17 additions & 6 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestIsolationLevelMapping(t *testing.T) {
}
}

func TestAppendDateTime(t *testing.T) {
func TestAppendTime(t *testing.T) {
tests := []struct {
t time.Time
str string
Expand Down Expand Up @@ -333,23 +333,34 @@ func TestAppendDateTime(t *testing.T) {
str: "9999-12-31 23:59:59.999999999",
},
{
// zero date
t: time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC),
str: "0001-01-01",
str: "0000-00-00",
},
{
// valid date with year 0
t: time.Date(0, 2, 2, 12, 34, 56, 0, time.UTC),
str: "0000-02-02 12:34:56",
},
{
// only time
t: time.Date(0, 1, 1, 8, 30, 0, 0, time.UTC),
str: "08:30:00",
},
}
for _, v := range tests {
buf := make([]byte, 0, 32)
buf, _ = appendDateTime(buf, v.t)
buf, _ = appendTime(buf, v.t)
if str := string(buf); str != v.str {
t.Errorf("appendDateTime(%v), have: %s, want: %s", v.t, str, v.str)
}
}

// year out of range
{
v := time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
v := time.Date(-1, 1, 1, 0, 0, 0, 0, time.UTC)
buf := make([]byte, 0, 32)
_, err := appendDateTime(buf, v)
_, err := appendTime(buf, v)
if err == nil {
t.Error("want an error")
return
Expand All @@ -358,7 +369,7 @@ func TestAppendDateTime(t *testing.T) {
{
v := time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC)
buf := make([]byte, 0, 32)
_, err := appendDateTime(buf, v)
_, err := appendTime(buf, v)
if err == nil {
t.Error("want an error")
return
Expand Down