Skip to content

Commit

Permalink
Updates to StringCovnersions -- optimize so we don't create an extra …
Browse files Browse the repository at this point in the history
…date
  • Loading branch information
kpartlow committed Jan 29, 2024
1 parent e123c9a commit ddb52cd
Show file tree
Hide file tree
Showing 4 changed files with 28 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
14 changes: 8 additions & 6 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,6 @@ private static Stream<Arguments> dateStringInIsoZoneDateTime() {
@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 +756,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 +768,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 +786,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 +801,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 ddb52cd

Please sign in to comment.