Skip to content

Commit

Permalink
- Added support for more number values to OffsetTime
Browse files Browse the repository at this point in the history
- Ensured no tests use Timestamp before year 0001 as it is not supported by java.sql.Timestamp
- Ensured no tests use Calendar before year 0002, as it is not supported by Java Calendar.
  • Loading branch information
jdereg committed Feb 2, 2025
1 parent fe9c893 commit c6999fe
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 223 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand Down Expand Up @@ -28,9 +27,4 @@ static AtomicInteger toAtomicInteger(Object from, Converter converter) {
AtomicInteger atomicInt = (AtomicInteger) from;
return new AtomicInteger(atomicInt.intValue());
}

static LocalTime toLocalTime(Object from, Converter converter) {
AtomicInteger atomicInteger= (AtomicInteger) from;
return LongConversions.toLocalTime((long)atomicInteger.get(), converter);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cedarsoftware.util.convert;

import java.time.LocalTime;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand Down Expand Up @@ -28,9 +27,4 @@ static AtomicLong toAtomicLong(Object from, Converter converter) {
AtomicLong atomicLong = (AtomicLong) from;
return new AtomicLong(atomicLong.get());
}

static LocalTime toLocalTime(Object from, Converter converter) {
AtomicLong atomicLong = (AtomicLong) from;
return LongConversions.toLocalTime(atomicLong.get(), converter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -75,6 +76,20 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}

static OffsetTime toOffsetTime(Object from, Converter converter) {
BigDecimal seconds = (BigDecimal) from;
try {
long wholeSecs = seconds.longValue(); // gets the integer part
BigDecimal frac = seconds.subtract(BigDecimal.valueOf(wholeSecs)); // gets just the fractional part
long nanos = frac.multiply(BILLION).longValue(); // converts fraction to nanos

Instant instant = Instant.ofEpochSecond(wholeSecs, nanos);
return OffsetTime.ofInstant(instant, converter.getOptions().getZoneId());
} catch (Exception e) {
throw new IllegalArgumentException("Input value [" + seconds.toPlainString() + "] for conversion to LocalTime must be >= 0 && <= 86399.999999999", e);
}
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toOffsetDateTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -109,6 +110,21 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}

static OffsetTime toOffsetTime(Object from, Converter converter) {
BigInteger bigI = (BigInteger) from;
try {
// Divide by billion to get seconds
BigInteger[] secondsAndNanos = bigI.divideAndRemainder(BigInteger.valueOf(1_000_000_000L));
long seconds = secondsAndNanos[0].longValue();
long nanos = secondsAndNanos[1].longValue();

Instant instant = Instant.ofEpochSecond(seconds, nanos);
return OffsetTime.ofInstant(instant, converter.getOptions().getZoneId());
} catch (Exception e) {
throw new IllegalArgumentException("Input value [" + bigI + "] for conversion to LocalTime must be >= 0 && <= 86399999999999", e);
}
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toOffsetDateTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.cedarsoftware.util.DateUtilities;

/**
* @author John DeRegnaucourt ([email protected])
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Map.class, Integer.class), MapConversions::toInt);
CONVERSION_DB.put(pair(String.class, Integer.class), StringConversions::toInt);
CONVERSION_DB.put(pair(LocalTime.class, Integer.class), LocalTimeConversions::toInteger);
CONVERSION_DB.put(pair(OffsetTime.class, Integer.class), OffsetTimeConversions::toInteger);
CONVERSION_DB.put(pair(Year.class, Integer.class), YearConversions::toInt);

// toLong
Expand All @@ -332,6 +333,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(LocalDate.class, Long.class), LocalDateConversions::toLong);
CONVERSION_DB.put(pair(LocalTime.class, Long.class), LocalTimeConversions::toLong);
CONVERSION_DB.put(pair(LocalDateTime.class, Long.class), LocalDateTimeConversions::toLong);
CONVERSION_DB.put(pair(OffsetTime.class, Long.class), OffsetTimeConversions::toLong);
CONVERSION_DB.put(pair(OffsetDateTime.class, Long.class), OffsetDateTimeConversions::toLong);
CONVERSION_DB.put(pair(ZonedDateTime.class, Long.class), ZonedDateTimeConversions::toLong);
CONVERSION_DB.put(pair(Calendar.class, Long.class), CalendarConversions::toLong);
Expand Down Expand Up @@ -376,6 +378,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(LocalDate.class, Double.class), LocalDateConversions::toDouble);
CONVERSION_DB.put(pair(LocalDateTime.class, Double.class), LocalDateTimeConversions::toDouble);
CONVERSION_DB.put(pair(ZonedDateTime.class, Double.class), ZonedDateTimeConversions::toDouble);
CONVERSION_DB.put(pair(OffsetTime.class, Double.class), OffsetTimeConversions::toDouble);
CONVERSION_DB.put(pair(OffsetDateTime.class, Double.class), OffsetDateTimeConversions::toDouble);
CONVERSION_DB.put(pair(Date.class, Double.class), DateConversions::toDouble);
CONVERSION_DB.put(pair(java.sql.Date.class, Double.class), DateConversions::toDouble);
Expand Down Expand Up @@ -452,6 +455,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(LocalDate.class, BigInteger.class), LocalDateConversions::toBigInteger);
CONVERSION_DB.put(pair(LocalDateTime.class, BigInteger.class), LocalDateTimeConversions::toBigInteger);
CONVERSION_DB.put(pair(ZonedDateTime.class, BigInteger.class), ZonedDateTimeConversions::toBigInteger);
CONVERSION_DB.put(pair(OffsetTime.class, BigInteger.class), OffsetTimeConversions::toBigInteger);
CONVERSION_DB.put(pair(OffsetDateTime.class, BigInteger.class), OffsetDateTimeConversions::toBigInteger);
CONVERSION_DB.put(pair(UUID.class, BigInteger.class), UUIDConversions::toBigInteger);
CONVERSION_DB.put(pair(Calendar.class, BigInteger.class), CalendarConversions::toBigInteger);
Expand Down Expand Up @@ -483,6 +487,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(LocalDate.class, BigDecimal.class), LocalDateConversions::toBigDecimal);
CONVERSION_DB.put(pair(LocalDateTime.class, BigDecimal.class), LocalDateTimeConversions::toBigDecimal);
CONVERSION_DB.put(pair(ZonedDateTime.class, BigDecimal.class), ZonedDateTimeConversions::toBigDecimal);
CONVERSION_DB.put(pair(OffsetTime.class, BigDecimal.class), OffsetTimeConversions::toBigDecimal);
CONVERSION_DB.put(pair(OffsetDateTime.class, BigDecimal.class), OffsetDateTimeConversions::toBigDecimal);
CONVERSION_DB.put(pair(UUID.class, BigDecimal.class), UUIDConversions::toBigDecimal);
CONVERSION_DB.put(pair(Calendar.class, BigDecimal.class), CalendarConversions::toBigDecimal);
Expand Down Expand Up @@ -525,6 +530,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(AtomicBoolean.class, AtomicInteger.class), AtomicBooleanConversions::toAtomicInteger);
CONVERSION_DB.put(pair(AtomicLong.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(LocalTime.class, AtomicInteger.class), LocalTimeConversions::toAtomicInteger);
CONVERSION_DB.put(pair(OffsetTime.class, AtomicInteger.class), OffsetTimeConversions::toAtomicInteger);
CONVERSION_DB.put(pair(Map.class, AtomicInteger.class), MapConversions::toAtomicInteger);
CONVERSION_DB.put(pair(String.class, AtomicInteger.class), StringConversions::toAtomicInteger);
CONVERSION_DB.put(pair(Year.class, AtomicInteger.class), YearConversions::toAtomicInteger);
Expand Down Expand Up @@ -553,6 +559,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(LocalTime.class, AtomicLong.class), LocalTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(LocalDateTime.class, AtomicLong.class), LocalDateTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(ZonedDateTime.class, AtomicLong.class), ZonedDateTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(OffsetTime.class, AtomicLong.class), OffsetTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(OffsetDateTime.class, AtomicLong.class), OffsetDateTimeConversions::toAtomicLong);
CONVERSION_DB.put(pair(Calendar.class, AtomicLong.class), CalendarConversions::toAtomicLong);
CONVERSION_DB.put(pair(Map.class, AtomicLong.class), MapConversions::toAtomicLong);
Expand Down Expand Up @@ -677,13 +684,13 @@ private static void buildFactoryConversions() {

// LocalTime conversions supported
CONVERSION_DB.put(pair(Void.class, LocalTime.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Integer.class, LocalTime.class), IntegerConversions::toLocalTime);
CONVERSION_DB.put(pair(Long.class, LocalTime.class), LongConversions::toLocalTime);
CONVERSION_DB.put(pair(Integer.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(Long.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(Double.class, LocalTime.class), DoubleConversions::toLocalTime);
CONVERSION_DB.put(pair(BigInteger.class, LocalTime.class), BigIntegerConversions::toLocalTime);
CONVERSION_DB.put(pair(BigDecimal.class, LocalTime.class), BigDecimalConversions::toLocalTime);
CONVERSION_DB.put(pair(AtomicInteger.class, LocalTime.class), AtomicIntegerConversions::toLocalTime);
CONVERSION_DB.put(pair(AtomicLong.class, LocalTime.class), AtomicLongConversions::toLocalTime);
CONVERSION_DB.put(pair(AtomicInteger.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(AtomicLong.class, LocalTime.class), NumberConversions::toLocalTime);
CONVERSION_DB.put(pair(java.sql.Date.class, LocalTime.class), DateConversions::toLocalTime);
CONVERSION_DB.put(pair(Timestamp.class, LocalTime.class), DateConversions::toLocalTime);
CONVERSION_DB.put(pair(Date.class, LocalTime.class), DateConversions::toLocalTime);
Expand Down Expand Up @@ -736,6 +743,13 @@ private static void buildFactoryConversions() {

// toOffsetTime
CONVERSION_DB.put(pair(Void.class, OffsetTime.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Integer.class, OffsetTime.class), NumberConversions::toOffsetTime);
CONVERSION_DB.put(pair(Long.class, OffsetTime.class), NumberConversions::toOffsetTime);
CONVERSION_DB.put(pair(Double.class, OffsetTime.class), DoubleConversions::toOffsetTime);
CONVERSION_DB.put(pair(BigInteger.class, OffsetTime.class), BigIntegerConversions::toOffsetTime);
CONVERSION_DB.put(pair(BigDecimal.class, OffsetTime.class), BigDecimalConversions::toOffsetTime);
CONVERSION_DB.put(pair(AtomicInteger.class, OffsetTime.class), NumberConversions::toOffsetTime);
CONVERSION_DB.put(pair(AtomicLong.class, OffsetTime.class), NumberConversions::toOffsetTime);
CONVERSION_DB.put(pair(OffsetTime.class, OffsetTime.class), Converter::identity);
CONVERSION_DB.put(pair(OffsetDateTime.class, OffsetTime.class), OffsetDateTimeConversions::toOffsetTime);
CONVERSION_DB.put(pair(Map.class, OffsetTime.class), MapConversions::toOffsetTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -82,6 +83,21 @@ static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId());
}

static OffsetTime toOffsetTime(Object from, Converter converter) {
double seconds = (double) from;
long wholeSecs = (long) seconds; // gets whole number of seconds
double frac = seconds - wholeSecs; // gets just the fractional part
long nanos = (long) (frac * 1_000_000_000.0); // converts fraction to nanos

try {
Instant instant = Instant.ofEpochSecond(wholeSecs, nanos);
return OffsetTime.ofInstant(instant, converter.getOptions().getZoneId());
}
catch (Exception e) {
throw new IllegalArgumentException("Input value [" + seconds + "] for conversion to LocalTime must be >= 0 && <= 86399.999999999", e);
}
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId()).toOffsetDateTime();
}
Expand Down

This file was deleted.

34 changes: 0 additions & 34 deletions src/main/java/com/cedarsoftware/util/convert/LongConversions.java

This file was deleted.

Loading

0 comments on commit c6999fe

Please sign in to comment.