diff --git a/server/util.go b/server/util.go index 7150229ebc0fc..88bcdc35086e2 100644 --- a/server/util.go +++ b/server/util.go @@ -208,12 +208,24 @@ func dumpBinaryDateTime(data []byte, t types.Time) []byte { switch t.Type() { case mysql.TypeTimestamp, mysql.TypeDatetime: if t.IsZero() { + // All zero. data = append(data, 0) - } else { + } else if t.Microsecond() != 0 { + // Has micro seconds. data = append(data, 11) data = dumpUint16(data, uint16(year)) data = append(data, byte(mon), byte(day), byte(t.Hour()), byte(t.Minute()), byte(t.Second())) data = dumpUint32(data, uint32(t.Microsecond())) + } else if t.Hour() != 0 || t.Minute() != 0 || t.Second() != 0 { + // Has HH:MM:SS + data = append(data, 7) + data = dumpUint16(data, uint16(year)) + data = append(data, byte(mon), byte(day), byte(t.Hour()), byte(t.Minute()), byte(t.Second())) + } else { + // Only YY:MM:DD + data = append(data, 4) + data = dumpUint16(data, uint16(year)) + data = append(data, byte(mon), byte(day)) } case mysql.TypeDate: if t.IsZero() { diff --git a/server/util_test.go b/server/util_test.go index d3148fc187693..a11641c33ffae 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -84,7 +84,7 @@ func (s *testUtilSuite) TestDumpBinaryTime(c *C) { c.Assert(err, IsNil) d = dumpBinaryDateTime(nil, t) // 201 & 7 composed to uint16 1993 (litter-endian) - c.Assert(d, DeepEquals, []byte{11, 201, 7, 7, 13, 1, 1, 1, 0, 0, 0, 0}) + c.Assert(d, DeepEquals, []byte{7, 201, 7, 7, 13, 1, 1, 1}) t, err = types.ParseDate(nil, "0000-00-00") c.Assert(err, IsNil)