Debian Codegraph @

src/main/org/h2/util/JSR310Utils.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND;
9 import static org.h2.util.DateTimeUtils.SECONDS_PER_DAY;
10 import static org.h2.util.DateTimeUtils.SHIFT_MONTH;
11 import static org.h2.util.DateTimeUtils.SHIFT_YEAR;
12 import static org.h2.util.DateTimeUtils.absoluteDayFromDateValue;
13 import static org.h2.util.DateTimeUtils.dateValue;
14 import static org.h2.util.DateTimeUtils.dateValueFromAbsoluteDay;
15 import static org.h2.util.DateTimeUtils.dayFromDateValue;
16 import static org.h2.util.DateTimeUtils.monthFromDateValue;
17 import static org.h2.util.DateTimeUtils.yearFromDateValue;
18
19 import java.math.BigInteger;
20 import java.time.Duration;
21 import java.time.Instant;
22 import java.time.LocalDate;
23 import java.time.LocalDateTime;
24 import java.time.LocalTime;
25 import java.time.OffsetDateTime;
26 import java.time.OffsetTime;
27 import java.time.Period;
28 import java.time.ZoneOffset;
29 import java.time.ZonedDateTime;
30
31 import org.h2.api.ErrorCode;
32 import org.h2.api.IntervalQualifier;
33 import org.h2.engine.CastDataProvider;
34 import org.h2.message.DbException;
35 import org.h2.value.DataType;
36 import org.h2.value.TypeInfo;
37 import org.h2.value.Value;
38 import org.h2.value.ValueDate;
39 import org.h2.value.ValueInterval;
40 import org.h2.value.ValueTime;
41 import org.h2.value.ValueTimeTimeZone;
42 import org.h2.value.ValueTimestamp;
43 import org.h2.value.ValueTimestampTimeZone;
44
45 /**
46 * This utility class provides access to JSR 310 classes.
47 */
48 public class JSR310Utils {
49
50 private static final long MIN_DATE_VALUE = (-999_999_999L << SHIFT_YEAR)
51 + (1 << SHIFT_MONTH) + 1;
52
53 private static final long MAX_DATE_VALUE = (999_999_999L << SHIFT_YEAR)
54 + (12 << SHIFT_MONTH) + 31;
55
56 private static final long MIN_INSTANT_SECOND = -31_557_014_167_219_200L;
57
58 private static final long MAX_INSTANT_SECOND = 31_556_889_864_403_199L;
59
60 private JSR310Utils() {
61 // utility class
62 }
63
64 /**
65 * Converts a value to a LocalDate.
66 *
67 * This method should only be called from Java 8 or later version.
68 *
69 * @param value
70 * the value to convert
71 * @param provider
72 * the cast information provider
73 * @return the LocalDate
74 */
75 public static LocalDate valueToLocalDate(Value value, CastDataProvider provider) {
76 long dateValue = value.convertToDate(provider).getDateValue();
77 if (dateValue > MAX_DATE_VALUE) {
78 return LocalDate.MAX;
79 } else if (dateValue < MIN_DATE_VALUE) {
80 return LocalDate.MIN;
81 }
82 return LocalDate.of(yearFromDateValue(dateValue), monthFromDateValue(dateValue),
83 dayFromDateValue(dateValue));
84 }
85
86 /**
87 * Converts a value to a LocalTime.
88 *
89 * This method should only be called from Java 8 or later version.
90 *
91 * @param value
92 * the value to convert
93 * @param provider
94 * the cast information provider
95 * @return the LocalTime
96 */
97 public static LocalTime valueToLocalTime(Value value, CastDataProvider provider) {
98 return LocalTime.ofNanoOfDay(((ValueTime) value.convertTo(TypeInfo.TYPE_TIME, provider)).getNanos());
99 }
100
101 /**
102 * Converts a value to a LocalDateTime.
103 *
104 * This method should only be called from Java 8 or later version.
105 *
106 * @param value
107 * the value to convert
108 * @param provider
109 * the cast information provider
110 * @return the LocalDateTime
111 */
112 public static LocalDateTime valueToLocalDateTime(Value value, CastDataProvider provider) {
113 ValueTimestamp valueTimestamp = (ValueTimestamp) value.convertTo(TypeInfo.TYPE_TIMESTAMP, provider);
114 return localDateTimeFromDateNanos(valueTimestamp.getDateValue(), valueTimestamp.getTimeNanos());
115 }
116
117 /**
118 * Converts a value to a Instant.
119 *
120 * This method should only be called from Java 8 or later version.
121 *
122 * @param value
123 * the value to convert
124 * @param provider
125 * the cast information provider
126 * @return the Instant
127 */
128 public static Instant valueToInstant(Value value, CastDataProvider provider) {
129 ValueTimestampTimeZone valueTimestampTimeZone = (ValueTimestampTimeZone) value
130 .convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider);
131 long timeNanos = valueTimestampTimeZone.getTimeNanos();
132 long epochSecond = absoluteDayFromDateValue(valueTimestampTimeZone.getDateValue())
133 * SECONDS_PER_DAY //
134 + timeNanos / NANOS_PER_SECOND //
135 - valueTimestampTimeZone.getTimeZoneOffsetSeconds();
136 if (epochSecond > MAX_INSTANT_SECOND) {
137 return Instant.MAX;
138 } else if (epochSecond < MIN_INSTANT_SECOND) {
139 return Instant.MIN;
140 }
141 return Instant.ofEpochSecond(epochSecond, timeNanos % NANOS_PER_SECOND);
142 }
143
144 /**
145 * Converts a value to a OffsetDateTime.
146 *
147 * This method should only be called from Java 8 or later version.
148 *
149 * @param value
150 * the value to convert
151 * @param provider
152 * the cast information provider
153 * @return the OffsetDateTime
154 */
155 public static OffsetDateTime valueToOffsetDateTime(Value value, CastDataProvider provider) {
156 ValueTimestampTimeZone v = (ValueTimestampTimeZone) value.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider);
157 return OffsetDateTime.of(localDateTimeFromDateNanos(v.getDateValue(), v.getTimeNanos()),
158 ZoneOffset.ofTotalSeconds(v.getTimeZoneOffsetSeconds()));
159 }
160
161 /**
162 * Converts a value to a ZonedDateTime.
163 *
164 * This method should only be called from Java 8 or later version.
165 *
166 * @param value
167 * the value to convert
168 * @param provider
169 * the cast information provider
170 * @return the ZonedDateTime
171 */
172 public static ZonedDateTime valueToZonedDateTime(Value value, CastDataProvider provider) {
173 ValueTimestampTimeZone v = (ValueTimestampTimeZone) value.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider);
174 return ZonedDateTime.of(localDateTimeFromDateNanos(v.getDateValue(), v.getTimeNanos()),
175 ZoneOffset.ofTotalSeconds(v.getTimeZoneOffsetSeconds()));
176 }
177
178 /**
179 * Converts a value to a OffsetTime.
180 *
181 * This method should only be called from Java 8 or later version.
182 *
183 * @param value
184 * the value to convert
185 * @param provider
186 * the cast information provider
187 * @return the OffsetTime
188 */
189 public static OffsetTime valueToOffsetTime(Value value, CastDataProvider provider) {
190 ValueTimeTimeZone valueTimeTimeZone = (ValueTimeTimeZone) value.convertTo(TypeInfo.TYPE_TIME_TZ, provider);
191 return OffsetTime.of(LocalTime.ofNanoOfDay(valueTimeTimeZone.getNanos()),
192 ZoneOffset.ofTotalSeconds(valueTimeTimeZone.getTimeZoneOffsetSeconds()));
193 }
194
195 /**
196 * Converts a value to a Period.
197 *
198 * This method should only be called from Java 8 or later version.
199 *
200 * @param value
201 * the value to convert
202 * @return the Period
203 */
204 public static Period valueToPeriod(Value value) {
205 if (!(value instanceof ValueInterval)) {
206 value = value.convertTo(TypeInfo.TYPE_INTERVAL_YEAR_TO_MONTH);
207 }
208 if (!DataType.isYearMonthIntervalType(value.getValueType())) {
209 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, (Throwable) null, value.getString());
210 }
211 ValueInterval v = (ValueInterval) value;
212 IntervalQualifier qualifier = v.getQualifier();
213 boolean negative = v.isNegative();
214 long leading = v.getLeading();
215 long remaining = v.getRemaining();
216 int y = Value.convertToInt(IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining), null);
217 int m = Value.convertToInt(IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining), null);
218 return Period.of(y, m, 0);
219 }
220
221 /**
222 * Converts a value to a Duration.
223 *
224 * This method should only be called from Java 8 or later version.
225 *
226 * @param value
227 * the value to convert
228 * @return the Duration
229 */
230 public static Duration valueToDuration(Value value) {
231 if (!(value instanceof ValueInterval)) {
232 value = value.convertTo(TypeInfo.TYPE_INTERVAL_DAY_TO_SECOND);
233 }
234 if (DataType.isYearMonthIntervalType(value.getValueType())) {
235 throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, (Throwable) null, value.getString());
236 }
237 BigInteger[] dr = IntervalUtils.intervalToAbsolute((ValueInterval) value)
238 .divideAndRemainder(BigInteger.valueOf(1_000_000_000));
239 return Duration.ofSeconds(dr[0].longValue(), dr[1].longValue());
240 }
241
242 /**
243 * Converts a LocalDate to a Value.
244 *
245 * @param localDate
246 * the LocalDate to convert, not {@code null}
247 * @return the value
248 */
249 public static ValueDate localDateToValue(LocalDate localDate) {
250 return ValueDate.fromDateValue(
251 dateValue(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()));
252 }
253
254 /**
255 * Converts a LocalTime to a Value.
256 *
257 * @param localTime
258 * the LocalTime to convert, not {@code null}
259 * @return the value
260 */
261 public static ValueTime localTimeToValue(LocalTime localTime) {
262 return ValueTime.fromNanos(localTime.toNanoOfDay());
263 }
264
265 /**
266 * Converts a LocalDateTime to a Value.
267 *
268 * @param localDateTime
269 * the LocalDateTime to convert, not {@code null}
270 * @return the value
271 */
272 public static ValueTimestamp localDateTimeToValue(LocalDateTime localDateTime) {
273 LocalDate localDate = localDateTime.toLocalDate();
274 return ValueTimestamp.fromDateValueAndNanos(
275 dateValue(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()),
276 localDateTime.toLocalTime().toNanoOfDay());
277 }
278
279 /**
280 * Converts a Instant to a Value.
281 *
282 * @param instant
283 * the Instant to convert, not {@code null}
284 * @return the value
285 */
286 public static ValueTimestampTimeZone instantToValue(Instant instant) {
287 long epochSecond = instant.getEpochSecond();
288 int nano = instant.getNano();
289 long absoluteDay = epochSecond / 86_400;
290 // Round toward negative infinity
291 if (epochSecond < 0 && (absoluteDay * 86_400 != epochSecond)) {
292 absoluteDay--;
293 }
294 long timeNanos = (epochSecond - absoluteDay * 86_400) * 1_000_000_000 + nano;
295 return ValueTimestampTimeZone.fromDateValueAndNanos(dateValueFromAbsoluteDay(absoluteDay),
296 timeNanos, 0);
297 }
298
299 /**
300 * Converts a OffsetDateTime to a Value.
301 *
302 * @param offsetDateTime
303 * the OffsetDateTime to convert, not {@code null}
304 * @return the value
305 */
306 public static ValueTimestampTimeZone offsetDateTimeToValue(OffsetDateTime offsetDateTime) {
307 LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
308 LocalDate localDate = localDateTime.toLocalDate();
309 return ValueTimestampTimeZone.fromDateValueAndNanos(
310 dateValue(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()),
311 localDateTime.toLocalTime().toNanoOfDay(), //
312 offsetDateTime.getOffset().getTotalSeconds());
313 }
314
315 /**
316 * Converts a ZonedDateTime to a Value.
317 *
318 * @param zonedDateTime
319 * the ZonedDateTime to convert, not {@code null}
320 * @return the value
321 */
322 public static ValueTimestampTimeZone zonedDateTimeToValue(ZonedDateTime zonedDateTime) {
323 LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
324 LocalDate localDate = localDateTime.toLocalDate();
325 return ValueTimestampTimeZone.fromDateValueAndNanos(
326 dateValue(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()),
327 localDateTime.toLocalTime().toNanoOfDay(), //
328 zonedDateTime.getOffset().getTotalSeconds());
329 }
330
331 /**
332 * Converts a OffsetTime to a Value.
333 *
334 * @param offsetTime
335 * the OffsetTime to convert, not {@code null}
336 * @return the value
337 */
338 public static ValueTimeTimeZone offsetTimeToValue(OffsetTime offsetTime) {
339 return ValueTimeTimeZone.fromNanos(offsetTime.toLocalTime().toNanoOfDay(),
340 offsetTime.getOffset().getTotalSeconds());
341 }
342
343 private static LocalDateTime localDateTimeFromDateNanos(long dateValue, long timeNanos) {
344 if (dateValue > MAX_DATE_VALUE) {
345 return LocalDateTime.MAX;
346 } else if (dateValue < MIN_DATE_VALUE) {
347 return LocalDateTime.MIN;
348 }
349 return LocalDateTime.of(LocalDate.of(yearFromDateValue(dateValue),
350 monthFromDateValue(dateValue), dayFromDateValue(dateValue)),
351 LocalTime.ofNanoOfDay(timeNanos));
352 }
353
354 /**
355 * Converts a Period to a Value.
356 *
357 * @param period
358 * the Period to convert, not {@code null}
359 * @return the value
360 */
361 public static ValueInterval periodToValue(Period period) {
362 int days = period.getDays();
363 if (days != 0) {
364 throw DbException.getInvalidValueException("Period.days", days);
365 }
366 int years = period.getYears();
367 int months = period.getMonths();
368 IntervalQualifier qualifier;
369 boolean negative = false;
370 long leading = 0L, remaining = 0L;
371 if (years == 0) {
372 if (months == 0L) {
373 // Use generic qualifier
374 qualifier = IntervalQualifier.YEAR_TO_MONTH;
375 } else {
376 qualifier = IntervalQualifier.MONTH;
377 leading = months;
378 if (leading < 0) {
379 leading = -leading;
380 negative = true;
381 }
382 }
383 } else {
384 if (months == 0L) {
385 qualifier = IntervalQualifier.YEAR;
386 leading = years;
387 if (leading < 0) {
388 leading = -leading;
389 negative = true;
390 }
391 } else {
392 qualifier = IntervalQualifier.YEAR_TO_MONTH;
393 leading = years * 12 + months;
394 if (leading < 0) {
395 leading = -leading;
396 negative = true;
397 }
398 remaining = leading % 12;
399 leading /= 12;
400 }
401 }
402 return ValueInterval.from(qualifier, negative, leading, remaining);
403 }
404
405 /**
406 * Converts a Duration to a Value.
407 *
408 * @param duration
409 * the Duration to convert, not {@code null}
410 * @return the value
411 */
412 public static ValueInterval durationToValue(Duration duration) {
413 long seconds = duration.getSeconds();
414 int nano = duration.getNano();
415 boolean negative = seconds < 0;
416 seconds = Math.abs(seconds);
417 if (negative && nano != 0) {
418 nano = 1_000_000_000 - nano;
419 seconds--;
420 }
421 return ValueInterval.from(IntervalQualifier.SECOND, negative, seconds, nano);
422 }
423
424 }