Skip to content
Open
Show file tree
Hide file tree
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 @@ -141,8 +141,8 @@ public String dateToString(Object value, ClickHouseColumn column) {
} else if (value instanceof LocalDate) {
return formatter.format(((LocalDate)value));
} else if (value instanceof java.sql.Date) {
java.sql.Date date = (java.sql.Date) value;
return formatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), UTC_ZONE_ID));
// java.sql.Date is a local date in the JVM default time zone, not an instant in UTC
return formatter.format(((java.sql.Date) value).toLocalDate());
} else if (value instanceof java.sql.Time) {
return formatter.format(EPOCH_START_OF_THE_DAY);
} else if (value instanceof Date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.TimeZone;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
Expand Down Expand Up @@ -100,6 +101,34 @@ public void testDateToString() {
assertEquals(converter.convertToString(sqlTime, column), "1970-01-01");
}

@DataProvider(name = "defaultTimeZones")
public static Object[][] defaultTimeZones() {
return new Object[][] {
{"UTC"},
{"Europe/Berlin"}, // UTC+1: local midnight is the previous day in UTC
{"Asia/Shanghai"}, // UTC+8
{"Pacific/Kiritimati"}, // UTC+14
{"America/New_York"}, // UTC-5
{"Pacific/Pago_Pago"}, // UTC-11
};
}

@Test(dataProvider = "defaultTimeZones")
public void testSqlDateToStringDoesNotDependOnDefaultTimeZone(String timeZone) {
TimeZone defaultTimeZone = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
DataTypeConverter converter = new DataTypeConverter();
ClickHouseColumn column = ClickHouseColumn.of("field", "Date");

assertEquals(converter.convertToString(java.sql.Date.valueOf("2022-01-04"), column), "2022-01-04");
assertEquals(converter.convertToString(java.sql.Date.valueOf("1970-01-01"), column), "1970-01-01");
assertEquals(converter.convertToString(java.sql.Date.valueOf("2024-02-29"), column), "2024-02-29");
} finally {
TimeZone.setDefault(defaultTimeZone);
}
}


@Test
public void testTimeToString() {
Expand Down
Loading