Skip to content

Simplify TrinoResultSet.parseDate #18109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.trino.jdbc.ColumnInfo.Nullable;
import io.trino.jdbc.TypeConversions.NoConversionRegisteredException;
import org.joda.time.DateTimeZone;
import org.joda.time.IllegalInstantException;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -355,16 +354,10 @@ private Date getDate(int columnIndex, DateTimeZone localTimeZone)

private static Date parseDate(String value, DateTimeZone localTimeZone)
{
try {
long millis = DATE_FORMATTER.withZone(localTimeZone).parseMillis(String.valueOf(value));
if (millis >= START_OF_MODERN_ERA_SECONDS * MILLISECONDS_PER_SECOND) {
return new Date(millis);
}
}
catch (IllegalInstantException ignored) {
// https://joda-time.sourceforge.net/faq.html#illegalinstant
LocalDate localDate = DATE_FORMATTER.parseLocalDate(String.valueOf(value));
return new Date(localDate.toDateTimeAtStartOfDay(localTimeZone).getMillis());
LocalDate localDate = DATE_FORMATTER.parseLocalDate(String.valueOf(value));
long millis = localDate.toDateTimeAtStartOfDay(localTimeZone).getMillis();
if (millis >= START_OF_MODERN_ERA_SECONDS * MILLISECONDS_PER_SECOND) {
return new Date(millis);
}

// The chronology used by default by Joda is not historically accurate for dates
Expand All @@ -374,8 +367,8 @@ private static Date parseDate(String value, DateTimeZone localTimeZone)
// expensive GregorianCalendar; note that Joda also has a chronology that works for
// older dates, but it uses a slightly different algorithm and yields results that
// are not compatible with java.sql.Date.
LocalDate localDate = DATE_FORMATTER.parseLocalDate(String.valueOf(value));
Calendar calendar = new GregorianCalendar(localDate.getYear(), localDate.getMonthOfYear() - 1, localDate.getDayOfMonth());
LocalDate preGregorianDate = DATE_FORMATTER.parseLocalDate(String.valueOf(value));
Calendar calendar = new GregorianCalendar(preGregorianDate.getYear(), preGregorianDate.getMonthOfYear() - 1, preGregorianDate.getDayOfMonth());
calendar.setTimeZone(TimeZone.getTimeZone(ZoneId.of(localTimeZone.getID())));

return new Date(calendar.getTimeInMillis());
Expand Down