Skip to content

Commit

Permalink
LocalDate tests added, started LocalTime
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 26, 2024
1 parent f9976ef commit 9723bef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,4 @@ static Calendar create(long epochMilli, ConverterOptions options) {
cal.setTimeInMillis(epochMilli);
return cal;
}

static Calendar create(ZonedDateTime time, ConverterOptions options) {
return GregorianCalendar.from(time);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ static long toLong(Object fromInstance, Converter converter, ConverterOptions op
return toInstant(fromInstance, options).toEpochMilli();
}

/**
* Warning: Can lose precision going from a full long down to a floating point number
* @param fromInstance instance to convert
* @param converter converter instance
* @param options converter options
* @return the floating point number cast from a lont.
*/
static float toFloat(Object fromInstance, Converter converter, ConverterOptions options) {
return toLong(fromInstance, converter, options);
}
Expand Down
44 changes: 42 additions & 2 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ void testLocalDateToLocalDateTimeAndBack(long epochMilli, ZoneId zoneId, LocalDa

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateBigIntegerAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {
void testLocalDateToBigIntegerAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

BigInteger intermediate = this.converter.convert(expected, BigInteger.class, createConvertOptions(zoneId, targetZone));

Expand All @@ -963,7 +963,7 @@ void testLocalDateBigIntegerAndBack(long epochMilli, ZoneId zoneId, LocalDate ex

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateBigDecimalAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {
void testLocalDateToBigDecimalAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

BigDecimal intermediate = this.converter.convert(expected, BigDecimal.class, createConvertOptions(zoneId, targetZone));

Expand All @@ -974,6 +974,46 @@ void testLocalDateBigDecimalAndBack(long epochMilli, ZoneId zoneId, LocalDate ex
assertThat(actual).isEqualTo(expected);
}

@Test
void testLocalDateToFloat() {

float intermediate = this.converter.convert(LD_MILLINNIUM_NY, float.class, createConvertOptions(NEW_YORK, TOKYO));

assertThat((long)intermediate).isNotEqualTo(946616400000L);
}

@Test
void testLocalDateToLocalTime_withZoneChange_willBeZoneOffset() {

LocalTime intermediate = this.converter.convert(LD_MILLINNIUM_NY, LocalTime.class, createConvertOptions(NEW_YORK, TOKYO));

assertThat(intermediate).hasHour(14)
.hasMinute(0)
.hasSecond(0)
.hasNano(0);
}

@Test
void testLocalDateToLocalTimeWithoutZoneChange_willBeMidnight() {

LocalTime intermediate = this.converter.convert(LD_MILLINNIUM_NY, LocalTime.class, createConvertOptions(NEW_YORK, NEW_YORK));

assertThat(intermediate).hasHour(0)
.hasMinute(0)
.hasSecond(0)
.hasNano(0);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateToLocalTime(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

float intermediate = this.converter.convert(expected, float.class, createConvertOptions(zoneId, targetZone));

assertThat((long)intermediate).isNotEqualTo(epochMilli);
}


@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expected)
Expand Down

0 comments on commit 9723bef

Please sign in to comment.