Skip to content

Commit e20c59e

Browse files
committed
fix(bqjdbc): fix Log to java.sql.Time coercion
1 parent 6858b61 commit e20c59e

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeCoercionUtility.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,12 @@ private static class LongToTime implements BigQueryCoercion<Long, Time> {
198198

199199
@Override
200200
public Time coerce(Long value) {
201-
202-
int HH = (int) TimeUnit.MICROSECONDS.toHours(value);
203-
int MM = (int) (TimeUnit.MICROSECONDS.toMinutes(value) % 60);
204-
int SS = (int) (TimeUnit.MICROSECONDS.toSeconds(value) % 60);
205-
206-
// Note: BQ Time has a precision of up to six fractional digits (microsecond precision)
207-
// but java.sql.Time do not. So data after seconds is not returned.
208-
// Using Calendar for timezone-safe date rollover arithmetic instead of the deprecated Time
209-
// constructor.
210-
java.util.Calendar cal = java.util.Calendar.getInstance();
211-
cal.clear();
212-
cal.set(1970, 0, 1, 0, 0, 0);
213-
cal.add(java.util.Calendar.HOUR, HH);
214-
cal.add(java.util.Calendar.MINUTE, MM);
215-
cal.add(java.util.Calendar.SECOND, SS);
216-
return new Time(cal.getTimeInMillis());
201+
// Convert UTC epoch microseconds to Instant
202+
Instant instant = Instant.ofEpochMilli(value / 1000);
203+
// Convert to LocalTime using the system default timezone to ensure correct wall-clock time
204+
LocalTime localTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalTime();
205+
// Return java.sql.Time with date component set to 1970-01-01 as mandated by JDBC spec
206+
return Time.valueOf(localTime);
217207
}
218208
}
219209

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/ArrowFormatTypeBigQueryCoercionUtilityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void nullToTime() {
165165
@Test
166166
public void longToTime() {
167167
assertThat(INSTANCE.coerceTo(Time.class, 1408452095220000L))
168-
.isEqualTo(new Time(1408452095000L));
168+
.isEqualTo(Time.valueOf(java.time.LocalTime.of(12, 41, 35)));
169169
}
170170

171171
@Test

0 commit comments

Comments
 (0)