Skip to content

Commit

Permalink
Date and sql Date conversions are no longer ambiguous on timezone. Th…
Browse files Browse the repository at this point in the history
…e reader side (Map to Date, Map to sql date) always ensure timezone is included. If it is not specified (older JSON format) then it will use the system's timezone, which is what it did in the past.
  • Loading branch information
jdereg committed Jan 25, 2025
1 parent 0b8bf6c commit 41891dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ static Map<String, Object> toMap(Object from, Converter converter) {
Map<String, Object> map = new LinkedHashMap<>();

if (date instanceof java.sql.Date) {
// SQL Date - interpret as a LocalDate
LocalDate localDate = ((java.sql.Date) date).toLocalDate();
// Convert millis to Instant then LocalDate in UTC
LocalDate localDate = Instant.ofEpochMilli(date.getTime())
.atZone(ZoneOffset.UTC)
.toLocalDate();

// Place that LocalDate at midnight in UTC, then format
ZonedDateTime zdt = localDate.atStartOfDay(ZoneOffset.UTC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,14 @@ static java.sql.Date toSqlDate(Object from, Converter converter) {
}

if (time instanceof String && StringUtilities.hasContent((String)time)) {
ZonedDateTime zdt = DateUtilities.parseDate((String) time, converter.getOptions().getZoneId(), true);
String timeStr = (String)time;
ZoneId zoneId;
if (timeStr.endsWith("Z")) {
zoneId = ZoneId.of("Z");
} else {
zoneId = converter.getOptions().getZoneId();
}
ZonedDateTime zdt = DateUtilities.parseDate((String) time, zoneId, true);
return new java.sql.Date(zdt.toInstant().toEpochMilli());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ private static void loadSqlDateTests() {
{ mapOf(EPOCH_MILLIS, 0L), new java.sql.Date(0L)},
{ mapOf(EPOCH_MILLIS, 1L), new java.sql.Date(1L)},
{ mapOf(EPOCH_MILLIS, 1710714535152L), new java.sql.Date(1710714535152L)},
{ mapOf(SQL_DATE, "1970-01-01T00:00:00Z"), new java.sql.Date(0L)},
{ mapOf(SQL_DATE, "1970-01-01T00:00:00Z"), new java.sql.Date(0L), true},
{ mapOf(SQL_DATE, "X1970-01-01T00:00:00Z"), new IllegalArgumentException("Issue parsing date-time, other characters present: X")},
{ mapOf(SQL_DATE, "1970-01-01X00:00:00Z"), new IllegalArgumentException("Issue parsing date-time, other characters present: X")},
{ mapOf(SQL_DATE, "1970-01-01T00:00bad zone"), new IllegalArgumentException("Issue parsing date-time, other characters present: zone")},
Expand Down Expand Up @@ -3867,10 +3867,10 @@ void testJsonIo(String shortNameSource, String shortNameTarget, Object source, O
e.printStackTrace();
throw new RuntimeException(e);
}
System.out.println("source = " + source);
System.out.println("target = " + target);
System.out.println("restored = " + restored);
System.out.println("*****");
// System.out.println("source = " + source);
// System.out.println("target = " + target);
// System.out.println("restored = " + restored);
// System.out.println("*****");
assert DeepEquals.deepEquals(restored, target);
}
}
Expand Down

0 comments on commit 41891dc

Please sign in to comment.