From 05058a501c03cab3108dda7676bb8f91df385ce0 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Mon, 5 Nov 2018 10:05:07 +0100 Subject: [PATCH] [#8013] Regression in OffsetDateTime parser when PostgreSQL timestamps have fractional seconds of 1, 2, 4, 5 digit precision --- .../java/org/jooq/impl/DefaultBinding.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java b/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java index bdf9d7d619..2fe21168fa 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java @@ -2485,17 +2485,8 @@ public class DefaultBinding implements Binding { if (parseCharIf(string, position, ':')) { second = parseInt(string, position, 2); - if (parseCharIf(string, position, '.')) { - nano = 1000000 * parseInt(string, position, 3); - - if (Character.isDigit(string.charAt(position[0]))) { - nano = nano + 1000 * parseInt(string, position, 3); - - if (Character.isDigit(string.charAt(position[0]))) { - nano = nano + parseInt(string, position, 3); - } - } - } + if (parseCharIf(string, position, '.')) + nano = parseInt(string, position, 9, true); } return LocalTime.of(hour, minute, second, nano); @@ -2543,8 +2534,10 @@ public class DefaultBinding implements Binding { private static final boolean parseCharIf(String string, int[] position, char expected) { boolean result = string.length() > position[0] && string.charAt(position[0]) == expected; + if (result) position[0] = position[0] + 1; + return result; } @@ -2553,19 +2546,29 @@ public class DefaultBinding implements Binding { throw new IllegalArgumentException("Expected '" + expected + "' at position " + position[0] + " in " + string); } - private static final int parseInt(String string, int[] position, int length) { - int result = 0; + private static final int parseInt(String string, int[] position, int maxLength) { + return parseInt(string, position, maxLength, false); + } - for (int i = position[0] + length - 1, dec = 1; i >= position[0]; i--, dec = dec * 10) { - int digit = string.charAt(i) - '0'; + private static final int parseInt(String string, int[] position, int maxLength, boolean rightPad) { + int result = 0; + int pos = position[0]; + int length; + + for (length = 0; length < maxLength && length < string.length(); length++) { + int digit = string.charAt(pos + length) - '0'; if (digit >= 0 && digit < 10) - result = result + dec * digit; + result = result * 10 + digit; else - throw new NumberFormatException("Not a number: " + string); + break; } - position[0] = position[0] + length; + if (rightPad && length < maxLength && result > 0) + for (int i = length; i < maxLength; i++) + result = result * 10; + + position[0] = pos + length; return result; } }