15
15
*/
16
16
package com .google .cloud .bigquery .storage .v1 ;
17
17
18
+ import static com .google .cloud .bigquery .storage .util .TimeConversionUtils .toJavaTimeLocalDateTime ;
19
+ import static com .google .cloud .bigquery .storage .util .TimeConversionUtils .toJavaTimeLocalTime ;
20
+ import static com .google .cloud .bigquery .storage .util .TimeConversionUtils .toThreetenLocalDateTime ;
21
+ import static com .google .cloud .bigquery .storage .util .TimeConversionUtils .toThreetenLocalTime ;
18
22
import static com .google .common .base .Preconditions .checkArgument ;
19
23
20
- import org .threeten .bp .DateTimeException ;
21
- import org .threeten .bp .LocalDateTime ;
22
- import org .threeten .bp .LocalTime ;
23
- import org .threeten .bp .temporal .ChronoUnit ;
24
+ import com .google .api .core .ObsoleteApi ;
25
+ import java .time .DateTimeException ;
26
+ import java .time .temporal .ChronoUnit ;
24
27
25
28
/**
26
29
* Ported from ZetaSQL CivilTimeEncoder Original code can be found at:
@@ -89,7 +92,7 @@ public final class CivilTimeEncoder {
89
92
* @see #decodePacked32TimeSeconds(int)
90
93
*/
91
94
@ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
92
- private static int encodePacked32TimeSeconds (LocalTime time ) {
95
+ private static int encodePacked32TimeSeconds (java . time . LocalTime time ) {
93
96
checkValidTimeSeconds (time );
94
97
int bitFieldTimeSeconds = 0x0 ;
95
98
bitFieldTimeSeconds |= time .getHour () << HOUR_SHIFT ;
@@ -112,19 +115,29 @@ private static int encodePacked32TimeSeconds(LocalTime time) {
112
115
* @see #encodePacked32TimeSeconds(LocalTime)
113
116
*/
114
117
@ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
115
- private static LocalTime decodePacked32TimeSeconds (int bitFieldTimeSeconds ) {
118
+ private static java . time . LocalTime decodePacked32TimeSeconds (int bitFieldTimeSeconds ) {
116
119
checkValidBitField (bitFieldTimeSeconds , TIME_SECONDS_MASK );
117
120
int hourOfDay = getFieldFromBitField (bitFieldTimeSeconds , HOUR_MASK , HOUR_SHIFT );
118
121
int minuteOfHour = getFieldFromBitField (bitFieldTimeSeconds , MINUTE_MASK , MINUTE_SHIFT );
119
122
int secondOfMinute = getFieldFromBitField (bitFieldTimeSeconds , SECOND_MASK , SECOND_SHIFT );
120
123
// LocalTime validates the input parameters.
121
124
try {
122
- return LocalTime .of (hourOfDay , minuteOfHour , secondOfMinute );
125
+ return java . time . LocalTime .of (hourOfDay , minuteOfHour , secondOfMinute );
123
126
} catch (DateTimeException e ) {
124
127
throw new IllegalArgumentException (e .getMessage (), e );
125
128
}
126
129
}
127
130
131
+ /**
132
+ * This method is obsolete. Use {@link #encodePacked64TimeMicrosLocalTime(java.time.LocalTime)}
133
+ * instead.
134
+ */
135
+ @ ObsoleteApi ("Use encodePacked64TimeMicrosLocalTime(java.time.LocalTime) instead" )
136
+ @ SuppressWarnings ("GoodTime" )
137
+ public static long encodePacked64TimeMicros (org .threeten .bp .LocalTime time ) {
138
+ return encodePacked64TimeMicrosLocalTime (toJavaTimeLocalTime (time ));
139
+ }
140
+
128
141
/**
129
142
* Encodes {@code time} as a 8-byte integer with microseconds precision.
130
143
*
@@ -140,13 +153,21 @@ private static LocalTime decodePacked32TimeSeconds(int bitFieldTimeSeconds) {
140
153
* @see #encodePacked64TimeMicros(LocalTime)
141
154
*/
142
155
@ SuppressWarnings ("GoodTime" )
143
- public static long encodePacked64TimeMicros ( LocalTime time ) {
156
+ public static long encodePacked64TimeMicrosLocalTime ( java . time . LocalTime time ) {
144
157
checkValidTimeMicros (time );
145
158
return (((long ) encodePacked32TimeSeconds (time )) << MICRO_LENGTH ) | (time .getNano () / 1_000L );
146
159
}
147
160
161
+ /** This method is obsolete. Use {@link #decodePacked64TimeMicrosLocalTime(long)} instead. */
162
+ @ ObsoleteApi ("Use decodePacked64TimeMicrosLocalTime(long) instead" )
163
+ @ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
164
+ public static org .threeten .bp .LocalTime decodePacked64TimeMicros (long bitFieldTimeMicros ) {
165
+ return toThreetenLocalTime (decodePacked64TimeMicrosLocalTime (bitFieldTimeMicros ));
166
+ }
167
+
148
168
/**
149
- * Decodes {@code bitFieldTimeMicros} as a {@link LocalTime} with microseconds precision.
169
+ * Decodes {@code bitFieldTimeMicros} as a {@link java.time.LocalTime} with microseconds
170
+ * precision.
150
171
*
151
172
* <p>Encoding is as the following:
152
173
*
@@ -159,13 +180,13 @@ public static long encodePacked64TimeMicros(LocalTime time) {
159
180
* @see #encodePacked64TimeMicros(LocalTime)
160
181
*/
161
182
@ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
162
- public static LocalTime decodePacked64TimeMicros (long bitFieldTimeMicros ) {
183
+ public static java . time . LocalTime decodePacked64TimeMicrosLocalTime (long bitFieldTimeMicros ) {
163
184
checkValidBitField (bitFieldTimeMicros , TIME_MICROS_MASK );
164
185
int bitFieldTimeSeconds = (int ) (bitFieldTimeMicros >> MICRO_LENGTH );
165
- LocalTime timeSeconds = decodePacked32TimeSeconds (bitFieldTimeSeconds );
186
+ java . time . LocalTime timeSeconds = decodePacked32TimeSeconds (bitFieldTimeSeconds );
166
187
int microOfSecond = getFieldFromBitField (bitFieldTimeMicros , MICRO_MASK , MICRO_SHIFT );
167
188
checkValidMicroOfSecond (microOfSecond );
168
- LocalTime time = timeSeconds .withNano (microOfSecond * 1000 );
189
+ java . time . LocalTime time = timeSeconds .withNano (microOfSecond * 1000 );
169
190
checkValidTimeMicros (time );
170
191
return time ;
171
192
}
@@ -184,7 +205,7 @@ public static LocalTime decodePacked64TimeMicros(long bitFieldTimeMicros) {
184
205
* @see #decodePacked64DatetimeSeconds(long)
185
206
*/
186
207
@ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
187
- private static long encodePacked64DatetimeSeconds (LocalDateTime dateTime ) {
208
+ private static long encodePacked64DatetimeSeconds (java . time . LocalDateTime dateTime ) {
188
209
checkValidDateTimeSeconds (dateTime );
189
210
long bitFieldDatetimeSeconds = 0x0L ;
190
211
bitFieldDatetimeSeconds |= (long ) dateTime .getYear () << YEAR_SHIFT ;
@@ -208,16 +229,17 @@ private static long encodePacked64DatetimeSeconds(LocalDateTime dateTime) {
208
229
* @see #encodePacked64DatetimeSeconds(LocalDateTime)
209
230
*/
210
231
@ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
211
- private static LocalDateTime decodePacked64DatetimeSeconds (long bitFieldDatetimeSeconds ) {
232
+ private static java .time .LocalDateTime decodePacked64DatetimeSeconds (
233
+ long bitFieldDatetimeSeconds ) {
212
234
checkValidBitField (bitFieldDatetimeSeconds , DATETIME_SECONDS_MASK );
213
235
int bitFieldTimeSeconds = (int ) (bitFieldDatetimeSeconds & TIME_SECONDS_MASK );
214
- LocalTime timeSeconds = decodePacked32TimeSeconds (bitFieldTimeSeconds );
236
+ java . time . LocalTime timeSeconds = decodePacked32TimeSeconds (bitFieldTimeSeconds );
215
237
int year = getFieldFromBitField (bitFieldDatetimeSeconds , YEAR_MASK , YEAR_SHIFT );
216
238
int monthOfYear = getFieldFromBitField (bitFieldDatetimeSeconds , MONTH_MASK , MONTH_SHIFT );
217
239
int dayOfMonth = getFieldFromBitField (bitFieldDatetimeSeconds , DAY_MASK , DAY_SHIFT );
218
240
try {
219
- LocalDateTime dateTime =
220
- LocalDateTime .of (
241
+ java . time . LocalDateTime dateTime =
242
+ java . time . LocalDateTime .of (
221
243
year ,
222
244
monthOfYear ,
223
245
dayOfMonth ,
@@ -231,6 +253,16 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime
231
253
}
232
254
}
233
255
256
+ /**
257
+ * This method is obsolete. Use {@link
258
+ * #encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime)} instead.
259
+ */
260
+ @ ObsoleteApi ("Use encodePacked64DatetimeMicrosLocalDateTime(java.time.LocalDateTime) instead" )
261
+ @ SuppressWarnings ({"GoodTime-ApiWithNumericTimeUnit" , "JavaLocalDateTimeGetNano" })
262
+ public static long encodePacked64DatetimeMicros (org .threeten .bp .LocalDateTime dateTime ) {
263
+ return encodePacked64DatetimeMicrosLocalDateTime (toJavaTimeLocalDateTime (dateTime ));
264
+ }
265
+
234
266
/**
235
267
* Encodes {@code dateTime} as a 8-byte integer with microseconds precision.
236
268
*
@@ -245,14 +277,26 @@ private static LocalDateTime decodePacked64DatetimeSeconds(long bitFieldDatetime
245
277
* @see #decodePacked64DatetimeMicros(long)
246
278
*/
247
279
@ SuppressWarnings ({"GoodTime-ApiWithNumericTimeUnit" , "JavaLocalDateTimeGetNano" })
248
- public static long encodePacked64DatetimeMicros ( LocalDateTime dateTime ) {
280
+ public static long encodePacked64DatetimeMicrosLocalDateTime ( java . time . LocalDateTime dateTime ) {
249
281
checkValidDateTimeMicros (dateTime );
250
282
return (encodePacked64DatetimeSeconds (dateTime ) << MICRO_LENGTH )
251
283
| (dateTime .getNano () / 1_000L );
252
284
}
253
285
254
286
/**
255
- * Decodes {@code bitFieldDatetimeMicros} as a {@link LocalDateTime} with microseconds precision.
287
+ * This method is obsolete. Use {@link #decodePacked64DatetimeMicrosLocalDateTime(long)} instead.
288
+ */
289
+ @ ObsoleteApi ("Use decodePacked64DatetimeMicrosLocalDateTime(long) instead" )
290
+ @ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
291
+ public static org .threeten .bp .LocalDateTime decodePacked64DatetimeMicros (
292
+ long bitFieldDatetimeMicros ) {
293
+ return toThreetenLocalDateTime (
294
+ decodePacked64DatetimeMicrosLocalDateTime (bitFieldDatetimeMicros ));
295
+ }
296
+
297
+ /**
298
+ * Decodes {@code bitFieldDatetimeMicros} as a {@link java.time.LocalDateTime} with microseconds
299
+ * precision.
256
300
*
257
301
* <p>Encoding is as the following:
258
302
*
@@ -265,13 +309,15 @@ public static long encodePacked64DatetimeMicros(LocalDateTime dateTime) {
265
309
* @see #encodePacked64DatetimeMicros(LocalDateTime)
266
310
*/
267
311
@ SuppressWarnings ("GoodTime-ApiWithNumericTimeUnit" )
268
- public static LocalDateTime decodePacked64DatetimeMicros (long bitFieldDatetimeMicros ) {
312
+ public static java .time .LocalDateTime decodePacked64DatetimeMicrosLocalDateTime (
313
+ long bitFieldDatetimeMicros ) {
269
314
checkValidBitField (bitFieldDatetimeMicros , DATETIME_MICROS_MASK );
270
315
long bitFieldDatetimeSeconds = bitFieldDatetimeMicros >> MICRO_LENGTH ;
271
- LocalDateTime dateTimeSeconds = decodePacked64DatetimeSeconds (bitFieldDatetimeSeconds );
316
+ java .time .LocalDateTime dateTimeSeconds =
317
+ decodePacked64DatetimeSeconds (bitFieldDatetimeSeconds );
272
318
int microOfSecond = getFieldFromBitField (bitFieldDatetimeMicros , MICRO_MASK , MICRO_SHIFT );
273
319
checkValidMicroOfSecond (microOfSecond );
274
- LocalDateTime dateTime = dateTimeSeconds .withNano (microOfSecond * 1_000 );
320
+ java . time . LocalDateTime dateTime = dateTimeSeconds .withNano (microOfSecond * 1_000 );
275
321
checkValidDateTimeMicros (dateTime );
276
322
return dateTime ;
277
323
}
@@ -280,25 +326,25 @@ private static int getFieldFromBitField(long bitField, long mask, int shift) {
280
326
return (int ) ((bitField & mask ) >> shift );
281
327
}
282
328
283
- private static void checkValidTimeSeconds (LocalTime time ) {
329
+ private static void checkValidTimeSeconds (java . time . LocalTime time ) {
284
330
checkArgument (time .getHour () >= 0 && time .getHour () <= 23 );
285
331
checkArgument (time .getMinute () >= 0 && time .getMinute () <= 59 );
286
332
checkArgument (time .getSecond () >= 0 && time .getSecond () <= 59 );
287
333
}
288
334
289
- private static void checkValidDateTimeSeconds (LocalDateTime dateTime ) {
335
+ private static void checkValidDateTimeSeconds (java . time . LocalDateTime dateTime ) {
290
336
checkArgument (dateTime .getYear () >= 1 && dateTime .getYear () <= 9999 );
291
337
checkArgument (dateTime .getMonthValue () >= 1 && dateTime .getMonthValue () <= 12 );
292
338
checkArgument (dateTime .getDayOfMonth () >= 1 && dateTime .getDayOfMonth () <= 31 );
293
339
checkValidTimeSeconds (dateTime .toLocalTime ());
294
340
}
295
341
296
- private static void checkValidTimeMicros (LocalTime time ) {
342
+ private static void checkValidTimeMicros (java . time . LocalTime time ) {
297
343
checkValidTimeSeconds (time );
298
344
checkArgument (time .equals (time .truncatedTo (ChronoUnit .MICROS )));
299
345
}
300
346
301
- private static void checkValidDateTimeMicros (LocalDateTime dateTime ) {
347
+ private static void checkValidDateTimeMicros (java . time . LocalDateTime dateTime ) {
302
348
checkValidDateTimeSeconds (dateTime );
303
349
checkArgument (dateTime .equals (dateTime .truncatedTo (ChronoUnit .MICROS )));
304
350
}
0 commit comments