Skip to content

Commit 94821de

Browse files
author
Paultagoras
committed
Tweaking timezones
1 parent c0d9634 commit 94821de

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
269269
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
270270
checkClosed();
271271
if (cal == null) {
272-
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));//This says whatever date is in UTC
272+
cal = new GregorianCalendar();//This says whatever date is in UTC
273273
}
274274

275275
LocalDate d = x.toLocalDate();
@@ -283,7 +283,7 @@ public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLExceptio
283283
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
284284
checkClosed();
285285
if (cal == null) {
286-
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
286+
cal = new GregorianCalendar();
287287
}
288288

289289
LocalTime t = x.toLocalTime();
@@ -297,7 +297,7 @@ public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLExceptio
297297
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
298298
checkClosed();
299299
if (cal == null) {
300-
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
300+
cal = new GregorianCalendar();
301301
}
302302

303303
LocalDateTime ldt = x.toLocalDateTime();

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class ResultSetImpl implements ResultSet, JdbcV2Wrapper {
3838
private boolean closed;
3939
private final StatementImpl parentStatement;
4040
private boolean wasNull;
41-
private final Calendar defaultCalendar;
4241

4342
public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, ClickHouseBinaryFormatReader reader) {
4443
this.parentStatement = parentStatement;
@@ -47,7 +46,6 @@ public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, Clic
4746
this.metaData = new com.clickhouse.jdbc.metadata.ResultSetMetaData(this);
4847
this.closed = false;
4948
this.wasNull = false;
50-
this.defaultCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
5149
}
5250

5351
protected ResultSetImpl(ResultSetImpl resultSet) {
@@ -57,7 +55,6 @@ protected ResultSetImpl(ResultSetImpl resultSet) {
5755
this.metaData = resultSet.metaData;
5856
this.closed = false;
5957
this.wasNull = false;
60-
this.defaultCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
6158
}
6259

6360
private void checkClosed() throws SQLException {
@@ -1048,17 +1045,18 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
10481045
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
10491046
checkClosed();
10501047
try {
1051-
LocalDate d = reader.getLocalDate(columnLabel);
1052-
if (d == null) {
1048+
ZonedDateTime zdt = reader.getZonedDateTime(columnLabel);
1049+
if (zdt == null) {
10531050
wasNull = true;
10541051
return null;
10551052
}
10561053
wasNull = false;
10571054

1058-
Calendar c = (Calendar) (cal != null ? cal : defaultCalendar).clone();
1059-
c.clear();
1060-
c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0);
1061-
return new Date(c.getTimeInMillis());
1055+
if (cal == null) {
1056+
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
1057+
}
1058+
1059+
return new Date(zdt.withZoneSameLocal(cal.getTimeZone().toZoneId()).toInstant().toEpochMilli());//This assumes the response is in cal.getTimeZone()
10621060
} catch (Exception e) {
10631061
throw ExceptionUtils.toSqlState(String.format("Method: getDate(\"%s\") encountered an exception.", columnLabel), String.format("SQL: [%s]", parentStatement.getLastSql()), e);
10641062
}
@@ -1080,10 +1078,11 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
10801078
}
10811079
wasNull = false;
10821080

1083-
Calendar c = (Calendar)( cal != null ? cal : defaultCalendar).clone();
1084-
c.clear();
1085-
c.set(1970, Calendar.JANUARY, 1, zdt.getHour(), zdt.getMinute(), zdt.getSecond());
1086-
return new Time(c.getTimeInMillis());
1081+
if (cal == null) {
1082+
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
1083+
}
1084+
1085+
return new Time(zdt.withZoneSameLocal(cal.getTimeZone().toZoneId()).toInstant().toEpochMilli());//This assumes the response is in cal.getTimeZone()
10871086
} catch (Exception e) {
10881087
throw ExceptionUtils.toSqlState(String.format("Method: getTime(\"%s\") encountered an exception.", columnLabel), String.format("SQL: [%s]", parentStatement.getLastSql()), e);
10891088
}
@@ -1098,23 +1097,23 @@ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException
10981097
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
10991098
checkClosed();
11001099
try {
1101-
LocalDateTime localDateTime = reader.getLocalDateTime(columnLabel);
1102-
if (localDateTime == null) {
1100+
ZonedDateTime zdt = reader.getZonedDateTime(columnLabel);
1101+
if (zdt == null) {
11031102
wasNull = true;
11041103
return null;
11051104
}
11061105
wasNull = false;
1107-
1108-
Calendar c = (Calendar) (cal != null ? cal : defaultCalendar).clone();
1109-
c.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(), localDateTime.getHour(), localDateTime.getMinute(),
1110-
localDateTime.getSecond());
1111-
Timestamp timestamp = new Timestamp(c.getTimeInMillis());
1112-
timestamp.setNanos(localDateTime.getNano());
1113-
return timestamp;
1106+
1107+
if (cal == null) {
1108+
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
1109+
}
1110+
1111+
Timestamp ts = new Timestamp(zdt.withZoneSameLocal(cal.getTimeZone().toZoneId()).toInstant().toEpochMilli());
1112+
ts.setNanos(zdt.getNano());
1113+
return ts;//This assumes the response is in cal.getTimeZone()
11141114
} catch (Exception e) {
11151115
throw ExceptionUtils.toSqlState(String.format("Method: getTimestamp(\"%s\") encountered an exception.", columnLabel), String.format("SQL: [%s]", parentStatement.getLastSql()), e);
11161116
}
1117-
11181117
}
11191118

11201119
@Override

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ public void testDateTypes() throws SQLException {
301301
assertTrue(rs.next());
302302
assertEquals(rs.getDate("date", new GregorianCalendar()).toString(), date.toString());
303303
assertEquals(rs.getDate("date32", new GregorianCalendar()).toString(), date32.toString());
304-
assertEquals(rs.getTimestamp("dateTime", new GregorianCalendar()).toString(), dateTime.toString());
305-
assertEquals(rs.getTimestamp("dateTime32", new GregorianCalendar()).toString(), dateTime32.toString());
306-
assertEquals(rs.getTimestamp("dateTime643", new GregorianCalendar()).toString(), dateTime643.toString());
307-
assertEquals(rs.getTimestamp("dateTime646", new GregorianCalendar()).toString(), dateTime646.toString());
308-
assertEquals(rs.getTimestamp("dateTime649", new GregorianCalendar()).toString(), dateTime649.toString());
304+
assertEquals(rs.getTimestamp("dateTime").toString(), dateTime.toString());
305+
assertEquals(rs.getTimestamp("dateTime32").toString(), dateTime32.toString());
306+
assertEquals(rs.getTimestamp("dateTime643").toString(), dateTime643.toString());
307+
assertEquals(rs.getTimestamp("dateTime646").toString(), dateTime646.toString());
308+
assertEquals(rs.getTimestamp("dateTime649").toString(), dateTime649.toString());
309309

310310
assertFalse(rs.next());
311311
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void testSetTime() throws Exception {
186186
stmt.setTime(1, java.sql.Time.valueOf("12:34:56"));
187187
try (ResultSet rs = stmt.executeQuery()) {
188188
assertTrue(rs.next());
189-
assertEquals(rs.getTime(1, new GregorianCalendar()).toString(), "12:34:56");
189+
assertEquals(rs.getTime(1).toString(), "12:34:56");
190190
assertFalse(rs.next());
191191
}
192192
}
@@ -201,7 +201,7 @@ public void testSetTimestamp() throws Exception {
201201
stmt.setTimestamp(2, java.sql.Timestamp.valueOf("2021-01-01 01:34:56.456"), java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")));
202202
try (ResultSet rs = stmt.executeQuery()) {
203203
assertTrue(rs.next());
204-
assertEquals(rs.getTimestamp(1, new GregorianCalendar()).toString(), "2021-01-01 01:34:56.456");
204+
assertEquals(rs.getTimestamp(1).toString(), "2021-01-01 01:34:56.456");
205205
assertEquals(rs.getTimestamp(2, new GregorianCalendar()).toString(), "2021-01-01 01:34:56.456");
206206
assertFalse(rs.next());
207207
}

0 commit comments

Comments
 (0)