Skip to content

Commit

Permalink
Merge pull request #89 from kpartlow/master
Browse files Browse the repository at this point in the history
Updates to StringCovnersions -- optimize so we don't create an extra …
  • Loading branch information
jdereg authored Jan 29, 2024
2 parents e123c9a + e96ba1d commit f9f3fce
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand Down
42 changes: 16 additions & 26 deletions src/main/java/com/cedarsoftware/util/convert/StringConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -286,45 +287,30 @@ static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOption
if (instant == null) {
return null;
}
Date date = Date.from(instant);
// Bring the zonedDateTime to a user-specifiable timezone
return new java.sql.Date(date.getTime());
return new java.sql.Date(instant.toEpochMilli());
}

static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
// Bring the zonedDateTime to a user-specifiable timezone
return Timestamp.from(instant);
}

static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
Date date = Date.from(instant);
return CalendarConversions.create(date.getTime(), options);
ZonedDateTime time = toZonedDateTime(from, options);
return time == null ? null : GregorianCalendar.from(time);
}

static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
// Bring the LocalDate to a user-specifiable timezone
return instant.atZone(options.getZoneId()).toLocalDate();
ZonedDateTime time = toZonedDateTime(from, options);
return time == null ? null : time.toLocalDate();
}

static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
// Bring the LocalDateTime to a user-specifiable timezone
return instant.atZone(options.getZoneId()).toLocalDateTime();
ZonedDateTime time = toZonedDateTime(from, options);
return time == null ? null : time.toLocalDateTime();
}

static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -335,12 +321,17 @@ static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions
return LocalTime.parse(str);
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
Instant instant = getInstant((String) from, options);
if (instant == null) {
return null;
}
return ZonedDateTime.ofInstant(instant, options.getZoneId());
return instant.atZone(options.getZoneId());
}


static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options);
}

static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -362,8 +353,7 @@ private static Instant getInstant(String from, ConverterOptions options) {
return null;
}
ZonedDateTime dateTime = DateUtilities.parseDate(str, options.getSourceZoneIdForLocalDates(), true);
Instant instant = Instant.from(dateTime);
return instant;
return dateTime.toInstant();
}

static char[] toCharArray(Object from, Converter converter, ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;

/**
Expand Down Expand Up @@ -72,7 +73,8 @@ static Timestamp toTimestamp(Object from, Converter converter, ConverterOptions
}

static Calendar toCalendar(Object from, Converter converter, ConverterOptions options) {
return CalendarConversions.create(toLong(from), options);
return GregorianCalendar.from((ZonedDateTime) from);
//return CalendarConversions.create(toLong(from), options);
}

static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) {
Expand Down
42 changes: 36 additions & 6 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -692,6 +693,18 @@ void testToBoolean_falseCases(Object input) {
}


private static Stream<Arguments> epochMilliWithZoneId() {
return Stream.of(
Arguments.of("946702799959", TOKYO),
Arguments.of("946702799959", PARIS),
Arguments.of("946702799959", GMT),
Arguments.of("946702799959", NEW_YORK),
Arguments.of("946702799959", CHICAGO),
Arguments.of("946702799959", LOS_ANGELES)
);
}


private static Stream<Arguments> dateStringNoZoneOffset() {
return Stream.of(
Arguments.of("2000-01-01T13:59:59", TOKYO),
Expand Down Expand Up @@ -737,11 +750,25 @@ private static Stream<Arguments> dateStringInIsoZoneDateTime() {
);
}

@ParameterizedTest
@MethodSource("epochMilliWithZoneId")
void testEpochMilliWithZoneId(String epochMilli, ZoneId zoneId) {
LocalDateTime localDateTime = this.converter.convert(epochMilli, LocalDateTime.class, createCustomZones(zoneId, NEW_YORK));

assertThat(localDateTime)
.hasYear(1999)
.hasMonthValue(12)
.hasDayOfMonth(31)
.hasHour(23)
.hasMinute(59)
.hasSecond(59);
}



@ParameterizedTest
@MethodSource("dateStringNoZoneOffset")
void testStringDateWithNoTimeZoneInformation(String date, ZoneId zoneId) {
// source is TOKYO, bu should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(zoneId, NEW_YORK));

assertThat(localDateTime)
Expand All @@ -757,7 +784,7 @@ void testStringDateWithNoTimeZoneInformation(String date, ZoneId zoneId) {
@ParameterizedTest
@MethodSource("dateStringInIsoOffsetDateTime")
void testStringDateWithTimeZoneToLocalDateTime(String date) {
// source is TOKYO, bu should be ignored when zone is provided on string.
// source is TOKYO, should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(TOKYO, NEW_YORK));

assertThat(localDateTime)
Expand All @@ -769,11 +796,12 @@ void testStringDateWithTimeZoneToLocalDateTime(String date) {
.hasSecond(59);
}


/*
@ParameterizedTest
@MethodSource("dateStringInIsoOffsetDateTimeWithMillis")
void testStringDateWithTimeZoneToLocalDateTimeIncludeMillis(String date) {
// source is TOKYO, bu should be ignored when zone is provided on string.
// source is TOKYO, should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(TOKYO, NEW_YORK));
assertThat(localDateTime)
Expand All @@ -786,10 +814,10 @@ void testStringDateWithTimeZoneToLocalDateTimeIncludeMillis(String date) {
.hasNano(959);
}
@ParameterizedTest
@ParameterizedTest
@MethodSource("dateStringInIsoZoneDateTime")
void testStringDateWithTimeZoneToLocalDateTimeWithZone(String date) {
// source is TOKYO, bu should be ignored when zone is provided on string.
// source is TOKYO, should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(TOKYO, NEW_YORK));
assertThat(localDateTime)
Expand All @@ -801,9 +829,11 @@ void testStringDateWithTimeZoneToLocalDateTimeWithZone(String date) {
.hasSecond(59)
.hasNano(959);
}
*/




private static Stream<Arguments> epochMillis_withLocalDateTimeInformation() {
return Stream.of(
Arguments.of(1687622249729L, TOKYO, LDT_2023_TOKYO),
Expand Down

0 comments on commit f9f3fce

Please sign in to comment.