Skip to content

Commit ea31f07

Browse files
sharangguptatsegismont
authored andcommitted
Fix encoding of negative fractional TIME values
Duration.getSeconds() is floored, so borrowing a second for the fraction wrote second = -1 whenever the floored value was a whole minute, and MySQL stored -59.5 seconds as zero. Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev> Assisted-by: Anthropic Claude (claude-opus-5-5)
1 parent b9c4b4b commit ea31f07

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

‎vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/datatype/DataTypeCodec.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,17 @@ private static void binaryEncodeTime(LocalTime value, ByteBuf buffer) {
387387
}
388388

389389
private static void binaryEncodeTime(Duration value, ByteBuf buffer) {
390-
long secondsOfDuration = value.getSeconds();
391-
int nanosOfDuration = value.getNano();
392-
if (secondsOfDuration == 0 && nanosOfDuration == 0) {
390+
if (value.isZero()) {
393391
buffer.writeByte(0);
394392
return;
395393
}
396394
byte isNegative = 0;
397-
if (secondsOfDuration < 0) {
395+
if (value.isNegative()) {
398396
isNegative = 1;
399-
secondsOfDuration = -secondsOfDuration;
397+
value = value.negated();
400398
}
399+
long secondsOfDuration = value.getSeconds();
400+
int nanosOfDuration = value.getNano();
401401

402402
int days = (int) (secondsOfDuration / 86400);
403403
int secondsOfADay = (int) (secondsOfDuration % 86400);
@@ -415,13 +415,7 @@ private static void binaryEncodeTime(Duration value, ByteBuf buffer) {
415415
return;
416416
}
417417

418-
int microSecond;
419-
if (isNegative == 1 && nanosOfDuration > 0) {
420-
second = second - 1;
421-
microSecond = (1000_000_000 - nanosOfDuration) / 1000;
422-
} else {
423-
microSecond = nanosOfDuration / 1000;
424-
}
418+
int microSecond = nanosOfDuration / 1000;
425419

426420
buffer.writeByte(12);
427421
buffer.writeByte(isNegative);

‎vertx-mysql-client/src/test/java/io/vertx/tests/mysqlclient/data/DateTimeBinaryCodecTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public void testEncodeFractionalSecondsPart(TestContext ctx) {
8282
testEncodeTime(ctx, Duration.ofHours(11).plusMinutes(12).plusNanos(123456000), Duration.ofHours(11).plusMinutes(12).plusNanos(123456000));
8383
}
8484

85+
@Test
86+
public void testEncodeNegativeFractionalSecondsPart(TestContext ctx) {
87+
testEncodeTime(ctx, Duration.ofSeconds(-59).minusMillis(500), Duration.ofSeconds(-59).minusMillis(500));
88+
}
89+
8590
@Test
8691
public void testEncodeTimeFromLocalTimeWithFractionalSecondsPart(TestContext ctx) {
8792
testEncodeTime(ctx, LocalTime.of(11, 12, 0, 123456000), Duration.ofHours(11).plusMinutes(12).plusNanos(123456000), LocalTime.of(11, 12, 0, 123456000));

0 commit comments

Comments
 (0)