Skip to content

Commit

Permalink
Calendar conversions are now matching the rest of the Temporal conver…
Browse files Browse the repository at this point in the history
…sions, where Double and BigDecimal represent fractional seconds, Long represents milliseconds, and BigInteger represents nanoseconds.
  • Loading branch information
jdereg committed Mar 2, 2024
1 parent c43b015 commit bb4e780
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

/**
Expand All @@ -35,6 +37,14 @@ final class BigDecimalConversions {

private BigDecimalConversions() { }

static Calendar toCalendar(Object from, Converter converter) {
BigDecimal seconds = (BigDecimal) from;
BigDecimal millis = seconds.multiply(BigDecimal.valueOf(1000));
Calendar calendar = GregorianCalendar.getInstance(converter.getOptions().getTimeZone());
calendar.setTimeInMillis(millis.longValue());
return calendar;
}

static Instant toInstant(Object from, Converter converter) {
BigDecimal seconds = (BigDecimal) from;
BigDecimal nanos = seconds.remainder(BigDecimal.ONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

/**
Expand All @@ -32,6 +34,7 @@
*/
final class BigIntegerConversions {
static final BigInteger BILLION = BigInteger.valueOf(1_000_000_000);
static final BigInteger MILLION = BigInteger.valueOf(1_000_000);

private BigIntegerConversions() { }

Expand Down Expand Up @@ -78,6 +81,14 @@ static Timestamp toTimestamp(Object from, Converter converter) {
return Timestamp.from(toInstant(from, converter));
}

static Calendar toCalendar(Object from, Converter converter) {
BigInteger epochNanos = (BigInteger) from;
BigInteger epochMillis = epochNanos.divide(MILLION);
Calendar calendar = GregorianCalendar.getInstance(converter.getOptions().getTimeZone());
calendar.setTimeInMillis(epochMillis.longValue());
return calendar;
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ static Long toLong(Object from, Converter converter) {
}

static double toDouble(Object from, Converter converter) {
return (double)toLong(from, converter);
Calendar calendar = (Calendar) from;
long epochMillis = calendar.getTime().getTime();
return epochMillis / 1000.0;
}

static Date toDate(Object from, Converter converter) {
Expand Down Expand Up @@ -81,11 +83,13 @@ static LocalTime toLocalTime(Object from, Converter converter) {
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
return BigDecimal.valueOf(((Calendar) from).getTime().getTime());
Calendar cal = (Calendar) from;
long epochMillis = cal.getTime().getTime();
return new BigDecimal(epochMillis).divide(BigDecimalConversions.GRAND);
}

static BigInteger toBigInteger(Object from, Converter converter) {
return BigInteger.valueOf(((Calendar) from).getTime().getTime());
return BigInteger.valueOf(((Calendar) from).getTime().getTime() * 1_000_000L);
}

static Calendar clone(Object from, Converter converter) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Short.class, Calendar.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Integer.class, Calendar.class), UNSUPPORTED);
CONVERSION_DB.put(pair(Long.class, Calendar.class), NumberConversions::toCalendar);
CONVERSION_DB.put(pair(Double.class, Calendar.class), NumberConversions::toCalendar);
CONVERSION_DB.put(pair(BigInteger.class, Calendar.class), NumberConversions::toCalendar);
CONVERSION_DB.put(pair(BigDecimal.class, Calendar.class), NumberConversions::toCalendar);
CONVERSION_DB.put(pair(Double.class, Calendar.class), DoubleConversions::toCalendar);
CONVERSION_DB.put(pair(BigInteger.class, Calendar.class), BigIntegerConversions::toCalendar);
CONVERSION_DB.put(pair(BigDecimal.class, Calendar.class), BigDecimalConversions::toCalendar);
CONVERSION_DB.put(pair(AtomicInteger.class, Calendar.class), UNSUPPORTED);
CONVERSION_DB.put(pair(AtomicLong.class, Calendar.class), NumberConversions::toCalendar);
CONVERSION_DB.put(pair(Date.class, Calendar.class), DateConversions::toCalendar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* @author John DeRegnaucourt ([email protected])
Expand Down Expand Up @@ -47,6 +49,15 @@ static Date toSqlDate(Object from, Converter converter) {
return new java.sql.Date((long)(d * 1000));
}

static Calendar toCalendar(Object from, Converter converter) {
double seconds = (double) from;
long epochMillis = (long)(seconds * 1000);
Calendar calendar = GregorianCalendar.getInstance(converter.getOptions().getTimeZone());
calendar.clear();
calendar.setTimeInMillis(epochMillis);
return calendar;
}

static LocalTime toLocalTime(Object from, Converter converter) {
double seconds = (double) from;
double nanos = seconds * 1_000_000_000.0;
Expand Down
Loading

0 comments on commit bb4e780

Please sign in to comment.