From a221a8f714d3cfef44bc0afa28849537a075b486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Hidra?= Date: Sat, 4 Apr 2026 13:57:08 -0400 Subject: [PATCH] Fix deserializing LocalTime negative and above 24:00 (issue #88) --- javakdb/src/main/java/com/kx/c.java | 3 ++- javakdb/src/test/java/com/kx/CTest.java | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/javakdb/src/main/java/com/kx/c.java b/javakdb/src/main/java/com/kx/c.java index 79374c1..2146bb2 100644 --- a/javakdb/src/main/java/com/kx/c.java +++ b/javakdb/src/main/java/com/kx/c.java @@ -998,7 +998,8 @@ void w(LocalDate d){ */ LocalTime rt(){ int timeAsInt=ri(); - return (timeAsInt==ni?LOCAL_TIME_NULL:LocalTime.ofNanoOfDay(timeAsInt*1_000_000L)); + // 86,400,00 is the max ms for LocalTime (24:00:00), after that we circle back to 00:00:00 + return (timeAsInt==ni?LOCAL_TIME_NULL:LocalTime.ofNanoOfDay((Math.abs(timeAsInt) % 86_400_000) * 1_000_000L)); } /** * Write LocalTime to serialization buffer in big endian format diff --git a/javakdb/src/test/java/com/kx/CTest.java b/javakdb/src/test/java/com/kx/CTest.java index 7660d4f..44a62d3 100644 --- a/javakdb/src/test/java/com/kx/CTest.java +++ b/javakdb/src/test/java/com/kx/CTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertTrue; +import java.io.UnsupportedEncodingException; import java.util.UUID; import java.util.Arrays; import java.time.LocalTime; @@ -437,6 +438,26 @@ public void testSerializeDeserializeTime() } } + @Test + public void testDeserializeTimeBeyond24Hours() throws c.KException, UnsupportedEncodingException { + // Serialized payload: 43:12:34.567 => 155,554,567ms + byte[] incomingMsg = new byte[]{1, 2, 0, 0, 13, 0, 0, 0, -19, 7, -109, 69, 9}; + com.kx.c c = new com.kx.c(); + LocalTime actual = (LocalTime) c.deserialize(incomingMsg); + LocalTime expected = LocalTime.of(19, 12, 34, 567_000_000); + Assert.assertEquals(expected, actual); + } + + @Test + public void testDeserializeTimeNegative() throws c.KException, UnsupportedEncodingException { + // Serialized payload: -23:12:34.567 -> -83,554,567ms + byte[] payload = new byte[]{1, 2, 0, 0, 13, 0, 0, 0, -19, -7, 14, 5, -5}; + com.kx.c c = new com.kx.c(); + LocalTime actual = (LocalTime) c.deserialize(payload); + LocalTime expected = LocalTime.of(23, 12, 34, 567_000_000); + Assert.assertEquals(expected, actual); + } + @Test public void testSerializeDeserializeInstant() {