Skip to content

Commit

Permalink
cherry pick #21127 to release-4.0 (#21135)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Nov 19, 2020
1 parent 9a4d550 commit 1ff5fc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1ff5fc8

Please sign in to comment.