Skip to content

Commit

Permalink
Add exception handling for BigDecimal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nirusu committed Jan 11, 2025
1 parent 6e09489 commit 9e7366b
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,26 @@ private static ZonedDateTime dateTimeValue(String numberValue) {
return null;
}

// Try to parse string time value as BigDecimal to keep the floating point precision
BigDecimal epochSecondsDecimal = new BigDecimal(numberValue);

// Truncate decimal part of the value to get the seconds
long seconds = epochSecondsDecimal.longValue();
if (seconds < 0) {
try {
// Try to parse string time value as BigDecimal to keep the floating point precision
BigDecimal epochSecondsDecimal = new BigDecimal(numberValue);

// Truncate decimal part of the value to get the seconds
long seconds = epochSecondsDecimal.longValue();
if (seconds < 0) {
return null;
}

// Get decimal part as nanoseconds
long nanoseconds = epochSecondsDecimal.subtract(new BigDecimal(seconds))
.movePointRight(9)
.longValue();

Instant instant = Instant.ofEpochSecond(seconds, nanoseconds);
return ZonedDateTime.ofInstant(instant, UTC);
} catch (NumberFormatException | ArithmeticException e) {
return null;
}

// Get decimal part as nanoseconds
long nanoseconds = epochSecondsDecimal.subtract(new BigDecimal(seconds))
.movePointRight(9)
.longValue();

Instant instant = Instant.ofEpochSecond(seconds, nanoseconds);
return ZonedDateTime.ofInstant(instant, UTC);
}

private static ZonedDateTime parseDateTime(String value) {
Expand Down

0 comments on commit 9e7366b

Please sign in to comment.