Skip to content

fix TIME value near zero will become zero #678

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

Merged
merged 1 commit into from
Feb 21, 2022
Merged
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
11 changes: 6 additions & 5 deletions replication/row_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,20 +1500,20 @@ func decodeTime2(data []byte, dec uint16) (string, int, error) {

case 5, 6:
tmp = int64(BFixedLengthInt(data[0:6])) - TIMEF_OFS
return timeFormat(tmp, n)
return timeFormat(tmp, dec, n)
default:
intPart = int64(BFixedLengthInt(data[0:3])) - TIMEF_INT_OFS
tmp = intPart << 24
}

if intPart == 0 {
if intPart == 0 && frac == 0 {
return "00:00:00", n, nil
}

return timeFormat(tmp, n)
return timeFormat(tmp, dec, n)
}

func timeFormat(tmp int64, n int) (string, int, error) {
func timeFormat(tmp int64, dec uint16, n int) (string, int, error) {
hms := int64(0)
sign := ""
if tmp < 0 {
Expand All @@ -1529,7 +1529,8 @@ func timeFormat(tmp int64, n int) (string, int, error) {
secPart := tmp % (1 << 24)

if secPart != 0 {
return fmt.Sprintf("%s%02d:%02d:%02d.%06d", sign, hour, minute, second, secPart), n, nil
s := fmt.Sprintf("%s%02d:%02d:%02d.%06d", sign, hour, minute, second, secPart)
return s[0 : len(s)-(6-int(dec))], n, nil
}

return fmt.Sprintf("%s%02d:%02d:%02d", sign, hour, minute, second), n, nil
Expand Down
30 changes: 30 additions & 0 deletions replication/row_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,36 @@ func (_ *testDecodeSuite) TestInvalidEvent(c *C) {
c.Assert(err, NotNil)
}

func (_ *testDecodeSuite) TestDecodeTime2(c *C) {
testcases := []struct {
data []byte
dec uint16
getFracTime bool
expected string
}{
{[]byte("\xb4\x6e\xfb"), 0, false, "838:59:59"},
{[]byte("\x80\xf1\x05"), 0, false, "15:04:05"},
{[]byte("\x80\x00\x00"), 0, false, "00:00:00"},
{[]byte("\x7f\xff\xff"), 0, false, "-00:00:01"},
{[]byte("\x7f\x0e\xfb"), 0, false, "-15:04:05"},
{[]byte("\x4b\x91\x05"), 0, false, "-838:59:59"},
{[]byte("\x7f\xff\xff\xff"), 2, true, "-00:00:00.01"},
{[]byte("\x7f\x0e\xfa\xf4"), 2, true, "-15:04:05.12"},
{[]byte("\x4b\x91\x05\xf4"), 2, true, "-838:59:58.12"},
{[]byte("\x7f\xff\xff\xff\xff"), 4, true, "-00:00:00.0001"},
{[]byte("\x7f\x0e\xfa\xfb\x2d"), 4, true, "-15:04:05.1235"},
{[]byte("\x4b\x91\x05\xfb\x2d"), 4, true, "-838:59:58.1235"},
{[]byte("\x7f\xff\xff\xff\xff\xff"), 6, true, "-00:00:00.000001"},
{[]byte("\x7f\x0e\xfa\xfe\x1d\xc0"), 6, true, "-15:04:05.123456"},
{[]byte("\x4b\x91\x05\xfe\x1d\xc0"), 6, true, "-838:59:58.123456"},
}
for _, tc := range testcases {
value, _, err := decodeTime2(tc.data, tc.dec)
c.Assert(err, IsNil)
c.Assert(value, Equals, tc.expected)
}
}

type decimalTest struct {
num string
dumpData []byte
Expand Down