Skip to content

Commit

Permalink
Added in more conversions, more tests. Thinking about removing the co…
Browse files Browse the repository at this point in the history
…nversions between the temporal types that have method conversions already on them. For example, I can get a ZonedDateTime from an OffsetDateTime. Removing these conversions will cut down significantly on "cross product" conversions (and tests) that are already available via standard JDK apis.
  • Loading branch information
jdereg committed Feb 19, 2024
1 parent 32adb24 commit a42b6c5
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.UUID;

Expand Down Expand Up @@ -39,6 +40,10 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toOffsetDateTime();
}

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 @@ -5,6 +5,9 @@
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.UUID;

/**
Expand Down Expand Up @@ -61,24 +64,22 @@ static UUID toUUID(Object from, Converter converter) {
return UUID.fromString(uuidString);
}

/**
* Epoch nanos to Timestamp
*/
static Timestamp toTimestamp(Object from, Converter converter) {
BigInteger nanoseconds = (BigInteger) from;
Duration duration = toDuration(nanoseconds, converter);
Instant epoch = Instant.EPOCH;
return Timestamp.from(toInstant(from, converter));
}

// Add the duration to the epoch
Instant timestampInstant = epoch.plus(duration);
static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return toInstant(from, converter).atZone(converter.getOptions().getZoneId());
}

static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}

// Convert Instant to Timestamp
return Timestamp.from(timestampInstant);
static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toOffsetDateTime();
}

/**
* Epoch nanos to Instant
*/
static Instant toInstant(Object from, Converter converter) {
BigInteger nanoseconds = (BigInteger) from;
BigInteger[] secondsAndNanos = nanoseconds.divideAndRemainder(BILLION);
Expand All @@ -87,9 +88,6 @@ static Instant toInstant(Object from, Converter converter) {
return Instant.ofEpochSecond(seconds, nanos);
}

/**
* Epoch nanos to Duration
*/
static Duration toDuration(Object from, Converter converter) {
BigInteger nanoseconds = (BigInteger) from;
BigInteger[] secondsAndNanos = nanoseconds.divideAndRemainder(BILLION);
Expand Down
33 changes: 19 additions & 14 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -36,26 +37,27 @@
* limitations under the License.
*/
final class OffsetDateTimeConversions {
private OffsetDateTimeConversions() {}
private OffsetDateTimeConversions() {
}

static Instant toInstant(Object from, Converter converter) {
return ((OffsetDateTime)from).toInstant();
return ((OffsetDateTime) from).toInstant();
}

static long toLong(Object from, Converter converter) {
return toInstant(from, converter).toEpochMilli();
}

static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return ((OffsetDateTime)from).toLocalDateTime();
return toZonedDateTime(from, converter).toLocalDateTime();
}

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

static LocalTime toLocalTime(Object from, Converter converter) {
return ((OffsetDateTime)from).toLocalTime();
return toZonedDateTime(from, converter).toLocalTime();
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
Expand All @@ -76,6 +78,11 @@ static java.sql.Date toSqlDate(Object from, Converter converter) {
return new java.sql.Date(toLong(from, converter));
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
return ((OffsetDateTime) from).toInstant().atZone(converter.getOptions().getZoneId());
// return ((OffsetDateTime) from).atZoneSameInstant(converter.getOptions().getZoneId());
}

static Date toDate(Object from, Converter converter) {
return new Date(toLong(from, converter));
}
Expand Down Expand Up @@ -109,4 +116,8 @@ static Map<String, Object> toMap(Object from, Converter converter) {
target.put(MapConversions.OFFSET, converter.convert(zoneOffset, String.class));
return target;
}

static double toDouble(Object from, Converter converter) {
throw new UnsupportedOperationException("This needs to be implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
Expand Down Expand Up @@ -64,6 +65,11 @@ static LocalTime toLocalTime(Object from, Converter converter) {
return toDifferentZone(from, converter).toLocalTime(); // shorter code over speed
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
ZonedDateTime zdt = (ZonedDateTime) from;
return zdt.toOffsetDateTime();
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
return new AtomicLong(toLong(from, converter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,16 @@ public ZoneId getZoneId() {
// Timestamp
/////////////////////////////////////////////////////////////
TEST_DB.put(pair(BigDecimal.class, Timestamp.class), new Object[][] {
{ new BigDecimal("-62167219200000.000000"), Timestamp.from(ZonedDateTime.parse("0000-01-01T00:00:00Z").toInstant()), true },
{ new BigDecimal("-62167219199999.999999"), Timestamp.from(ZonedDateTime.parse("0000-01-01T00:00:00.000000001Z").toInstant()), true },
{ new BigDecimal("-1000.000001"), Timestamp.from(ZonedDateTime.parse("1969-12-31T23:59:58.999999999Z").toInstant()), true },
{ new BigDecimal("-1000.000000"), Timestamp.from(ZonedDateTime.parse("1969-12-31T23:59:59Z").toInstant()), true },
{ new BigDecimal("-0.000010"), Timestamp.from(ZonedDateTime.parse("1969-12-31T23:59:59.999999990Z").toInstant()), true },
{ new BigDecimal("-0.000001"), Timestamp.from(ZonedDateTime.parse("1969-12-31T23:59:59.999999999Z").toInstant()), true },
{ new BigDecimal("0.000000"), Timestamp.from(ZonedDateTime.parse("1970-01-01T00:00:00.000000000Z").toInstant()), true },
{ new BigDecimal("0.000001"), Timestamp.from(ZonedDateTime.parse("1970-01-01T00:00:00.000000001Z").toInstant()), true },
{ new BigDecimal("999.999999"), Timestamp.from(ZonedDateTime.parse("1970-01-01T00:00:00.999999999Z").toInstant()), true },
{ new BigDecimal("1000.000000"), Timestamp.from(ZonedDateTime.parse("1970-01-01T00:00:01.000000000Z").toInstant()), true },
{ new BigDecimal("1708237915987.654321"), Timestamp.from(ZonedDateTime.parse("2024-02-18T06:31:55.987654321+00:00").toInstant()), true },
{ new BigDecimal("1708237915123.456789"), Timestamp.from(ZonedDateTime.parse("2024-02-18T06:31:55.123456789+00:00").toInstant()), true },
});
Expand Down
27 changes: 1 addition & 26 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1172,17 +1172,6 @@ void testLongToInstant(long epochMilli, ZoneId zoneId, LocalDateTime expected)
assertThat(actual).isEqualTo(Instant.ofEpochMilli(epochMilli));
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testBigIntegerToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expected)
{
BigInteger bi = BigInteger.valueOf(epochMilli);

Converter converter = new Converter(createCustomZones(zoneId));
LocalDateTime localDateTime = converter.convert(bi, LocalDateTime.class);
assertThat(localDateTime).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testBigDecimalToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expected)
Expand Down Expand Up @@ -1706,21 +1695,7 @@ void testLocalDateTimeToZonedDateTime(long epochMilli, ZoneId sourceZoneId, Loca
LocalDateTime actual = converter.convert(intermediate, LocalDateTime.class);
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateTimeConversion_params")
void testLocalDateTimeToBigInteger(long epochMilli, ZoneId sourceZoneId, LocalDateTime initial, ZoneId targetZoneId, LocalDateTime expected)
{
Converter converter = new Converter(createCustomZones(sourceZoneId));
BigInteger milli = converter.convert(initial, BigInteger.class);
assertThat(milli.longValue()).isEqualTo(epochMilli);

converter = new Converter(createCustomZones(targetZoneId));
LocalDateTime actual = converter.convert(milli, LocalDateTime.class);
assertThat(actual).isEqualTo(expected);

}


@ParameterizedTest
@MethodSource("localDateTimeConversion_params")
void testLocalDateTimeToBigDecimal(long epochMilli, ZoneId sourceZoneId, LocalDateTime initial, ZoneId targetZoneId, LocalDateTime expected)
Expand Down

0 comments on commit a42b6c5

Please sign in to comment.