Skip to content

Commit e1cd563

Browse files
committed
fix sql.Time?
1 parent e78db38 commit e1cd563

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

client-v2/src/main/java/com/clickhouse/client/api/internal/DataTypeConverter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public String stringToString(Object bytesOrString, ClickHouseColumn column) {
9494
public static final ZonedDateTime EPOCH_START_OF_THE_DAY =
9595
ZonedDateTime.ofInstant(Instant.EPOCH, UTC_ZONE_ID);
9696

97+
public static final LocalDate EPOCH_DATE = LocalDate.of(1970, 1, 1);
98+
9799
public String dateToString(Object value, ClickHouseColumn column) {
98100
DateTimeFormatter formatter = DataTypeUtils.DATE_FORMATTER;
99101

@@ -133,8 +135,9 @@ public String timeToString(Object value, ClickHouseColumn column) {
133135
} else if (value instanceof java.sql.Date) {
134136
return formatter.format(EPOCH_START_OF_THE_DAY);
135137
} else if (value instanceof java.sql.Time) {
136-
java.sql.Time date = (java.sql.Time) value;
137-
return formatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), UTC_ZONE_ID));
138+
java.sql.Time time = (java.sql.Time) value;
139+
LocalTime lt = time.toLocalTime();
140+
return formatter.format(lt);
138141
} else if (value instanceof Date) {
139142
return formatter.format(((Date)value).toInstant().atZone(UTC_ZONE_ID));
140143
}
@@ -162,8 +165,9 @@ public String dateTimeToString(Object value, ClickHouseColumn column) {
162165
return formatter.format(EPOCH_START_OF_THE_DAY);
163166

164167
} else if (value instanceof java.sql.Time) {
165-
java.sql.Time date = (java.sql.Time) value;
166-
return formatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), UTC_ZONE_ID));
168+
java.sql.Time time = (java.sql.Time) value;
169+
LocalTime lt = time.toLocalTime();
170+
return formatter.format(lt.atDate(EPOCH_DATE));
167171
} else if (value instanceof Date) {
168172
return formatter.format(((Date)value).toInstant().atZone(UTC_ZONE_ID));
169173
}

client-v2/src/test/java/com/clickhouse/client/api/internal/DataTypeConverterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public void testTimeToString() {
7878
assertEquals(converter.convertToString(sqlDate, column), "00:00:00");
7979
Date date = Date.from(ZonedDateTime.of(2022, 1, 4, 12, 34, 56, 0, ZoneId.of("Asia/Shanghai")).toInstant());
8080
assertEquals(converter.convertToString(date, column), "04:34:56");
81-
java.sql.Time sqlTime = java.sql.Time.valueOf("12:34:56");
82-
assertEquals(converter.convertToString(sqlTime, column), "20:34:56");
81+
java.sql.Time sqlTime = java.sql.Time.valueOf("12:34:56");// 12:34:56 in ms
82+
assertEquals(converter.convertToString(sqlTime, column), "12:34:56");
8383
}
8484

8585

@@ -95,7 +95,7 @@ public void testDateTimeToString() {
9595
Date sqlDate = java.sql.Date.valueOf("2022-01-04");
9696
assertEquals(converter.convertToString(sqlDate, column), "1970-01-01 00:00:00");
9797
java.sql.Time sqlTime = java.sql.Time.valueOf("12:34:56");
98-
assertEquals(converter.convertToString(sqlTime, column), "1970-01-01 20:34:56");
98+
assertEquals(converter.convertToString(sqlTime, column), "1970-01-01 12:34:56");
9999
}
100100

101101
@Test

0 commit comments

Comments
 (0)