From 3a343a23e62c5612c5b664270a1c19bdeb2ce39f Mon Sep 17 00:00:00 2001 From: Baoyi Chen Date: Thu, 4 Aug 2022 11:35:07 +0800 Subject: [PATCH 1/2] Optimize convertToLocalDateTime method Signed-off-by: Baoyi Chen --- .../converter/UtcTimestampConverter.java | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java b/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java index 21c10571bf..88d1a2b5c6 100644 --- a/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java +++ b/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java @@ -24,10 +24,10 @@ import quickfix.SystemTime; import java.text.DateFormat; +import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -135,26 +135,37 @@ public static Date convert(String value) throws FieldConvertError { */ public static LocalDateTime convertToLocalDateTime(String value) throws FieldConvertError { verifyFormat(value); - int length = value.length(); try { - switch (length) { - case LENGTH_INCL_SECONDS: - return LocalDateTime.parse(value, FORMATTER_SECONDS); - case LENGTH_INCL_MILLIS: - return LocalDateTime.parse(value, FORMATTER_MILLIS); - case LENGTH_INCL_MICROS: - return LocalDateTime.parse(value, FORMATTER_MICROS); - case LENGTH_INCL_NANOS: - case LENGTH_INCL_PICOS: - return LocalDateTime.parse(value.substring(0, LENGTH_INCL_NANOS), FORMATTER_NANOS); - default: - throwFieldConvertError(value, TYPE); + int length = value.length(); + int ns = 0; + if (length >= LENGTH_INCL_NANOS) { + ns = parseInt(value, 18, 9); + } else if (length == LENGTH_INCL_MICROS) { + ns = parseInt(value, 18, 6) * 1000; + } else if (length == LENGTH_INCL_MILLIS) { + ns = parseInt(value, 18, 3) * 1000000; } - } catch (DateTimeParseException e) { + + int yy = parseInt(value, 0, 4); + int mm = parseInt(value, 4, 2); + int dd = parseInt(value, 6, 2); + int h = parseInt(value, 9, 2); + int m = parseInt(value, 12, 2); + int s = parseInt(value, 15, 2); + return LocalDateTime.of(yy, mm, dd, h, m, s, ns); + } catch (DateTimeException e) { throwFieldConvertError(value, TYPE); } return null; - } + } + + private static int parseInt(String value, int off, int len) { + int num = 0; + for (int index = 0; index < len; index++) { + num = (num * 10) + value.charAt(off + index) - '0'; + } + return num; + } private static Long getMillisForDay(String value) { // Performance optimization: the calendar for the start of the day is cached. From 301e2d918772b9927700361cca6e0c0de4e434ad Mon Sep 17 00:00:00 2001 From: Baoyi Chen Date: Thu, 25 Aug 2022 17:06:48 +0800 Subject: [PATCH 2/2] Optimize try catch block Signed-off-by: Baoyi Chen --- .../converter/UtcTimestampConverter.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java b/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java index 88d1a2b5c6..a90f5c94be 100644 --- a/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java +++ b/quickfixj-core/src/main/java/quickfix/field/converter/UtcTimestampConverter.java @@ -135,23 +135,23 @@ public static Date convert(String value) throws FieldConvertError { */ public static LocalDateTime convertToLocalDateTime(String value) throws FieldConvertError { verifyFormat(value); - try { - int length = value.length(); - int ns = 0; - if (length >= LENGTH_INCL_NANOS) { - ns = parseInt(value, 18, 9); - } else if (length == LENGTH_INCL_MICROS) { - ns = parseInt(value, 18, 6) * 1000; - } else if (length == LENGTH_INCL_MILLIS) { - ns = parseInt(value, 18, 3) * 1000000; - } + int length = value.length(); + int ns = 0; + if (length >= LENGTH_INCL_NANOS) { + ns = parseInt(value, 18, 9); + } else if (length == LENGTH_INCL_MICROS) { + ns = parseInt(value, 18, 6) * 1000; + } else if (length == LENGTH_INCL_MILLIS) { + ns = parseInt(value, 18, 3) * 1000000; + } - int yy = parseInt(value, 0, 4); - int mm = parseInt(value, 4, 2); - int dd = parseInt(value, 6, 2); - int h = parseInt(value, 9, 2); - int m = parseInt(value, 12, 2); - int s = parseInt(value, 15, 2); + int yy = parseInt(value, 0, 4); + int mm = parseInt(value, 4, 2); + int dd = parseInt(value, 6, 2); + int h = parseInt(value, 9, 2); + int m = parseInt(value, 12, 2); + int s = parseInt(value, 15, 2); + try { return LocalDateTime.of(yy, mm, dd, h, m, s, ns); } catch (DateTimeException e) { throwFieldConvertError(value, TYPE);