Skip to content

Commit

Permalink
Double, BigDecimal, and BigInteger completed in terms of conversions …
Browse files Browse the repository at this point in the history
…of temporal types.
  • Loading branch information
jdereg committed Feb 26, 2024
1 parent 05e240e commit bdb6227
Show file tree
Hide file tree
Showing 10 changed files with 2,256 additions and 2,060 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -80,6 +81,10 @@ static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId());
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}

static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Short.class, java.sql.Date.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Integer.class, java.sql.Date.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Long.class, java.sql.Date.class), NumberConversions::toSqlDate);
CONVERSION_DB.put(pair(Double.class, java.sql.Date.class), DoubleConversions::toDate);
CONVERSION_DB.put(pair(Double.class, java.sql.Date.class), DoubleConversions::toSqlDate);
CONVERSION_DB.put(pair(BigInteger.class, java.sql.Date.class), BigIntegerConversions::toSqlDate);
CONVERSION_DB.put(pair(BigDecimal.class, java.sql.Date.class), BigDecimalConversions::toSqlDate);
CONVERSION_DB.put(pair(AtomicInteger.class, java.sql.Date.class), UNSUPPORTED);
Expand Down Expand Up @@ -520,7 +520,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Integer.class, LocalDate.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Long.class, LocalDate.class), NumberConversions::toLocalDate);
CONVERSION_DB.put(pair(Double.class, LocalDate.class), DoubleConversions::toLocalDate);
CONVERSION_DB.put(pair(BigInteger.class, LocalDate.class), NumberConversions::toLocalDate);
CONVERSION_DB.put(pair(BigInteger.class, LocalDate.class), BigIntegerConversions::toLocalDate);
CONVERSION_DB.put(pair(BigDecimal.class, LocalDate.class), BigDecimalConversions::toLocalDate);
CONVERSION_DB.put(pair(AtomicInteger.class, LocalDate.class), UNSUPPORTED);
CONVERSION_DB.put(pair(AtomicLong.class, LocalDate.class), NumberConversions::toLocalDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ static LocalTime toLocalTime(Object from, Converter converter) {

static BigInteger toBigInteger(Object from, Converter converter) {
Instant instant = toInstant(from, converter);
BigInteger seconds = BigInteger.valueOf(instant.getEpochSecond());
BigInteger nanos = BigInteger.valueOf(instant.getNano());

// Convert the seconds to nanoseconds and add the nanosecond fraction
return seconds.multiply(BigIntegerConversions.BILLION).add(nanos);
return InstantConversions.toBigInteger(instant, converter);
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ static Date toDate(Object from, Converter converter) {
return new Date((long)(d * 1000));
}

static Date toSqlDate(Object from, Converter converter) {
double d = (Double) from;
return new java.sql.Date((long)(d * 1000));
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,13 @@ static Date toDate(Object from, Converter converter) {
}

static BigInteger toBigInteger(Object from, Converter converter) {
// TODO: Upgrade precision
return BigInteger.valueOf(toLong(from, converter));
Instant instant = toInstant(from, converter);
return InstantConversions.toBigInteger(instant, converter);
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
Instant instant = toInstant(from, converter);
BigDecimal epochSeconds = BigDecimal.valueOf(instant.getEpochSecond());
BigDecimal nanos = new BigDecimal(BigInteger.valueOf(instant.getNano()), 9);

// Add the nanos to the whole seconds
return epochSeconds.add(nanos);
return InstantConversions.toBigDecimal(instant, converter);
}

static String toString(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ static Date toDate(Object from, Converter converter) {
}

static BigInteger toBigInteger(Object from, Converter converter) {
return BigInteger.valueOf(toLong(from, converter));
Instant instant = toInstant(from, converter);
return InstantConversions.toBigInteger(instant, converter);
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,14 @@ static Date toDate(Object from, Converter converter) {
}

static BigInteger toBigInteger(Object from, Converter converter) {
// TODO: nanosecond resolution needed
return BigInteger.valueOf(toLong(from, converter));
Instant instant = toInstant(from, converter);
return InstantConversions.toBigInteger(instant, converter);
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
OffsetDateTime offsetDateTime = (OffsetDateTime) from;
Instant instant = offsetDateTime.toInstant();

long epochSecond = instant.getEpochSecond();
long nano = instant.getNano();

// Convert to BigDecimal and add
BigDecimal seconds = BigDecimal.valueOf(epochSecond);
BigDecimal nanoSeconds = BigDecimal.valueOf(nano).scaleByPowerOfTen(-9);

return seconds.add(nanoSeconds);
return InstantConversions.toBigDecimal(instant, converter);
}

static OffsetTime toOffsetTime(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ static Date toDate(Object from, Converter converter) {
}

static BigInteger toBigInteger(Object from, Converter converter) {
return BigInteger.valueOf(toLong(from, converter));
Instant instant = toInstant(from, converter);
return InstantConversions.toBigInteger(instant, converter);
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
Expand Down
Loading

0 comments on commit bdb6227

Please sign in to comment.