Skip to content

Commit

Permalink
Backport fix for #69
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 30, 2019
1 parent 08a02e1 commit db6ed04
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.fasterxml.jackson.datatype.jsr310;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.function.BiFunction;

/**
Expand Down Expand Up @@ -136,6 +137,11 @@ else if (seconds.scale() < -63) {
// Now we know that seconds has reasonable scale, we can safely chop it apart.
secondsOnly = seconds.longValue();
nanosOnly = nanoseconds.subtract(new BigDecimal(secondsOnly).scaleByPowerOfTen(9)).intValue();

if (secondsOnly < 0 && secondsOnly > Instant.MIN.getEpochSecond()) {
// Issue #69 and Issue #120: avoid sending a negative adjustment to the Instant constructor, we want this as the actual nanos
nanosOnly = Math.abs(nanosOnly);
}
}

return convert.apply(secondsOnly, nanosOnly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public void testToDecimal01()
decimal = DecimalUtils.toDecimal(19827342231L, 999888000);
assertEquals("The returned decimal is not correct.",
"19827342231.999888000", decimal);

decimal = DecimalUtils.toDecimal(-22704862, 599000000);
assertEquals("The returned decimal is not correct.",
"-22704862.599000000", decimal);
}

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -133,6 +137,13 @@ public void testExtractSecondsAndNanos06()
checkExtractSecondsAndNanos(19827342231L, 999999999, value);
}

@Test
public void testExtractSecondsAndNanosFromNegativeBigDecimal()
{
BigDecimal value = new BigDecimal("-22704862.599000000");
checkExtractSecondsAndNanos(-22704862L, 599000000, value);
}

@Test(timeout = 100)
public void testExtractSecondsAndNanos07()
{
Expand Down
Loading

0 comments on commit db6ed04

Please sign in to comment.