From 1ff5fc8a23f9438e1575fad217ff0389f45bebcd Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 19 Nov 2020 15:25:27 +0800 Subject: [PATCH] cherry pick #21127 to release-4.0 (#21135) --- server/util.go | 14 +++++++++++++- server/util_test.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) 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)