|
29 | 29 | import com.google.cloud.bigquery.FieldValueList; |
30 | 30 | import com.google.cloud.bigquery.Range; |
31 | 31 | import com.google.cloud.bigquery.exception.BigQueryJdbcCoercionException; |
| 32 | +import com.google.cloud.bigquery.jdbc.rules.TimeZoneRule; |
32 | 33 | import com.google.common.collect.ImmutableList; |
33 | 34 | import java.math.BigDecimal; |
34 | 35 | import java.sql.Date; |
|
38 | 39 | import java.time.LocalDate; |
39 | 40 | import java.time.LocalTime; |
40 | 41 | import java.time.temporal.ChronoUnit; |
41 | | -import java.util.concurrent.TimeUnit; |
42 | 42 | import org.junit.jupiter.api.Test; |
| 43 | +import org.junit.jupiter.api.extension.RegisterExtension; |
43 | 44 |
|
44 | 45 | public class FieldValueTypeBigQueryCoercionUtilityTest { |
| 46 | + @RegisterExtension public final TimeZoneRule timeZoneRule = new TimeZoneRule("UTC"); |
| 47 | + |
45 | 48 | private static final FieldValue STRING_VALUE = FieldValue.of(PRIMITIVE, "sample-string"); |
46 | 49 | private static final FieldValue INTEGER_VALUE = FieldValue.of(PRIMITIVE, "345"); |
47 | 50 | private static final FieldValue FLOAT_VALUE = FieldValue.of(PRIMITIVE, "345.21"); |
@@ -315,11 +318,27 @@ public void fieldValueToTimestampWhenInnerValueIsNull() { |
315 | 318 | public void fieldValueToTime() { |
316 | 319 | LocalTime expectedTime = LocalTime.of(23, 59, 59); |
317 | 320 | assertThat(INSTANCE.coerceTo(Time.class, TIME_VALUE)).isEqualTo(Time.valueOf(expectedTime)); |
318 | | - LocalTime expectedTimeWithNanos = LocalTime.parse("23:59:59.99999"); |
319 | | - long millisOfDay = TimeUnit.NANOSECONDS.toMillis(expectedTimeWithNanos.toNanoOfDay()); |
320 | | - long localMillis = millisOfDay - java.util.TimeZone.getDefault().getOffset(millisOfDay); |
| 321 | + // expectedTimeWithNanos has 999 milliseconds, giving 86399999 ms of day. |
| 322 | + // Since the test runs under UTC timezone by TimeZoneRule, expected localMillis is 86399999L. |
321 | 323 | assertThat(INSTANCE.coerceTo(Time.class, TIME_WITH_NANOSECOND_VALUE)) |
322 | | - .isEqualTo(new Time(localMillis)); |
| 324 | + .isEqualTo(new Time(86399999L)); |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + public void fieldValueToTimeInNonUTCTimeZone() { |
| 329 | + java.util.TimeZone originalTimeZone = java.util.TimeZone.getDefault(); |
| 330 | + try { |
| 331 | + java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/Los_Angeles")); |
| 332 | + com.google.cloud.bigquery.jdbc.TimeZoneCache.reset(); |
| 333 | + // 23:59:59.99999 yields 86399999 milliseconds. |
| 334 | + // Under America/Los_Angeles on 1970-01-01 (PST, -8 hours offset), |
| 335 | + // the subtracted offset results in 86399999 - (-28800000) = 115199999L. |
| 336 | + assertThat(INSTANCE.coerceTo(Time.class, TIME_WITH_NANOSECOND_VALUE)) |
| 337 | + .isEqualTo(new Time(115199999L)); |
| 338 | + } finally { |
| 339 | + java.util.TimeZone.setDefault(originalTimeZone); |
| 340 | + com.google.cloud.bigquery.jdbc.TimeZoneCache.reset(); |
| 341 | + } |
323 | 342 | } |
324 | 343 |
|
325 | 344 | @Test |
|
0 commit comments