Debian Codegraph @

src/main/org/h2/util/LegacyDateTimeUtils.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.MILLIS_PER_DAY;
9 import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND;
10
11 import java.sql.Date;
12 import java.sql.Time;
13 import java.sql.Timestamp;
14 import java.util.Calendar;
15 import java.util.GregorianCalendar;
16 import java.util.TimeZone;
17
18 import org.h2.engine.CastDataProvider;
19 import org.h2.value.TypeInfo;
20 import org.h2.value.Value;
21 import org.h2.value.ValueDate;
22 import org.h2.value.ValueNull;
23 import org.h2.value.ValueTime;
24 import org.h2.value.ValueTimestamp;
25 import org.h2.value.ValueTimestampTimeZone;
26
27 /**
28 * Date and time utilities for {@link Date}, {@link Time}, and {@link Timestamp}
29 * classes.
30 */
31 public final class LegacyDateTimeUtils {
32
33 /**
34 * Gregorian change date for a {@link java.util.GregorianCalendar} that
35 * represents a proleptic Gregorian calendar.
36 */
37 public static final Date PROLEPTIC_GREGORIAN_CHANGE = new Date(Long.MIN_VALUE);
38
39 /**
40 * UTC time zone.
41 */
42 public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
43
44 private LegacyDateTimeUtils() {
45 }
46
47 /**
48 * Get or create a date value for the given date.
49 *
50 * @param provider
51 * the cast information provider
52 * @param timeZone
53 * time zone, or {@code null} for default
54 * @param date
55 * the date
56 * @return the value
57 */
58 public static ValueDate fromDate(CastDataProvider provider, TimeZone timeZone, Date date) {
59 long ms = date.getTime();
60 return ValueDate.fromDateValue(dateValueFromLocalMillis(
61 ms + (timeZone == null ? getTimeZoneOffsetMillis(provider, ms) : timeZone.getOffset(ms))));
62 }
63
64 /**
65 * Get or create a time value for the given time.
66 *
67 * @param provider
68 * the cast information provider
69 * @param timeZone
70 * time zone, or {@code null} for default
71 * @param time
72 * the time
73 * @return the value
74 */
75 public static ValueTime fromTime(CastDataProvider provider, TimeZone timeZone, Time time) {
76 long ms = time.getTime();
77 return ValueTime.fromNanos(nanosFromLocalMillis(
78 ms + (timeZone == null ? getTimeZoneOffsetMillis(provider, ms) : timeZone.getOffset(ms))));
79 }
80
81 /**
82 * Get or create a timestamp value for the given timestamp.
83 *
84 * @param provider
85 * the cast information provider
86 * @param timeZone
87 * time zone, or {@code null} for default
88 * @param timestamp
89 * the timestamp
90 * @return the value
91 */
92 public static ValueTimestamp fromTimestamp(CastDataProvider provider, TimeZone timeZone, Timestamp timestamp) {
93 long ms = timestamp.getTime();
94 return timestampFromLocalMillis(
95 ms + (timeZone == null ? getTimeZoneOffsetMillis(provider, ms) : timeZone.getOffset(ms)),
96 timestamp.getNanos() % 1_000_000);
97 }
98
99 /**
100 * Get or create a timestamp value for the given date/time in millis.
101 *
102 * @param provider
103 * the cast information provider
104 * @param ms
105 * the milliseconds
106 * @param nanos
107 * the nanoseconds
108 * @return the value
109 */
110 public static ValueTimestamp fromTimestamp(CastDataProvider provider, long ms, int nanos) {
111 return timestampFromLocalMillis(ms + getTimeZoneOffsetMillis(provider, ms), nanos);
112 }
113
114 private static ValueTimestamp timestampFromLocalMillis(long ms, int nanos) {
115 long dateValue = dateValueFromLocalMillis(ms);
116 long timeNanos = nanos + nanosFromLocalMillis(ms);
117 return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos);
118 }
119
120 /**
121 * Convert a local datetime in millis to an encoded date.
122 *
123 * @param ms
124 * the milliseconds
125 * @return the date value
126 */
127 public static long dateValueFromLocalMillis(long ms) {
128 long absoluteDay = ms / MILLIS_PER_DAY;
129 // Round toward negative infinity
130 if (ms < 0 && (absoluteDay * MILLIS_PER_DAY != ms)) {
131 absoluteDay--;
132 }
133 return DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay);
134 }
135
136 /**
137 * Convert a time in milliseconds in local time to the nanoseconds since
138 * midnight.
139 *
140 * @param ms
141 * the milliseconds
142 * @return the nanoseconds
143 */
144 public static long nanosFromLocalMillis(long ms) {
145 ms %= MILLIS_PER_DAY;
146 if (ms < 0) {
147 ms += MILLIS_PER_DAY;
148 }
149 return ms * 1_000_000;
150 }
151
152 /**
153 * Get the date value converted to the specified time zone.
154 *
155 * @param provider the cast information provider
156 * @param timeZone the target time zone
157 * @param value the value to convert
158 * @return the date
159 */
160 public static Date toDate(CastDataProvider provider, TimeZone timeZone, Value value) {
161 return value != ValueNull.INSTANCE
162 ? new Date(getMillis(provider, timeZone, value.convertToDate(provider).getDateValue(), 0)) : null;
163 }
164
165 /**
166 * Get the time value converted to the specified time zone.
167 *
168 * @param provider the cast information provider
169 * @param timeZone the target time zone
170 * @param value the value to convert
171 * @return the time
172 */
173 public static Time toTime(CastDataProvider provider, TimeZone timeZone, Value value) {
174 switch (value.getValueType()) {
175 case Value.NULL:
176 return null;
177 default:
178 value = value.convertTo(TypeInfo.TYPE_TIME, provider);
179 //$FALL-THROUGH$
180 case Value.TIME:
181 return new Time(
182 getMillis(provider, timeZone, DateTimeUtils.EPOCH_DATE_VALUE, ((ValueTime) value).getNanos()));
183 }
184 }
185
186 /**
187 * Get the timestamp value converted to the specified time zone.
188 *
189 * @param provider the cast information provider
190 * @param timeZone the target time zone
191 * @param value the value to convert
192 * @return the timestamp
193 */
194 public static Timestamp toTimestamp(CastDataProvider provider, TimeZone timeZone, Value value) {
195 switch (value.getValueType()) {
196 case Value.NULL:
197 return null;
198 default:
199 value = value.convertTo(TypeInfo.TYPE_TIMESTAMP, provider);
200 //$FALL-THROUGH$
201 case Value.TIMESTAMP: {
202 ValueTimestamp v = (ValueTimestamp) value;
203 long timeNanos = v.getTimeNanos();
204 Timestamp ts = new Timestamp(getMillis(provider, timeZone, v.getDateValue(), timeNanos));
205 ts.setNanos((int) (timeNanos % NANOS_PER_SECOND));
206 return ts;
207 }
208 case Value.TIMESTAMP_TZ: {
209 ValueTimestampTimeZone v = (ValueTimestampTimeZone) value;
210 long timeNanos = v.getTimeNanos();
211 Timestamp ts = new Timestamp(DateTimeUtils.absoluteDayFromDateValue(v.getDateValue()) * MILLIS_PER_DAY
212 + timeNanos / 1_000_000 - v.getTimeZoneOffsetSeconds() * 1_000);
213 ts.setNanos((int) (timeNanos % NANOS_PER_SECOND));
214 return ts;
215 }
216 }
217 }
218
219 /**
220 * Calculate the milliseconds since 1970-01-01 (UTC) for the given date and
221 * time (in the specified timezone).
222 *
223 * @param provider the cast information provider
224 * @param tz the timezone of the parameters, or null for the default
225 * timezone
226 * @param dateValue date value
227 * @param timeNanos nanoseconds since midnight
228 * @return the number of milliseconds (UTC)
229 */
230 public static long getMillis(CastDataProvider provider, TimeZone tz, long dateValue, long timeNanos) {
231 return (tz == null ? provider != null ? provider.currentTimeZone() : DateTimeUtils.getTimeZone()
232 : TimeZoneProvider.ofId(tz.getID())).getEpochSecondsFromLocal(dateValue, timeNanos) * 1_000
233 + timeNanos / 1_000_000 % 1_000;
234 }
235
236 /**
237 * Returns local time zone offset for a specified timestamp.
238 *
239 * @param provider the cast information provider
240 * @param ms milliseconds since Epoch in UTC
241 * @return local time zone offset
242 */
243 public static int getTimeZoneOffsetMillis(CastDataProvider provider, long ms) {
244 long seconds = ms / 1_000;
245 // Round toward negative infinity
246 if (ms < 0 && (seconds * 1_000 != ms)) {
247 seconds--;
248 }
249 return (provider != null ? provider.currentTimeZone() : DateTimeUtils.getTimeZone())
250 .getTimeZoneOffsetUTC(seconds) * 1_000;
251 }
252
253 /**
254 * Convert a legacy Java object to a value.
255 *
256 * @param session
257 * the session
258 * @param x
259 * the value
260 * @return the value, or {@code null} if not supported
261 */
262 public static Value legacyObjectToValue(CastDataProvider session, Object x) {
263 if (x instanceof Date) {
264 return fromDate(session, null, (Date) x);
265 } else if (x instanceof Time) {
266 return fromTime(session, null, (Time) x);
267 } else if (x instanceof Timestamp) {
268 return fromTimestamp(session, null, (Timestamp) x);
269 } else if (x instanceof java.util.Date) {
270 return fromTimestamp(session, ((java.util.Date) x).getTime(), 0);
271 } else if (x instanceof Calendar) {
272 Calendar gc = (Calendar) x;
273 long ms = gc.getTimeInMillis();
274 return timestampFromLocalMillis(ms + gc.getTimeZone().getOffset(ms), 0);
275 } else {
276 return null;
277 }
278 }
279
280 /**
281 * Converts the specified value to an object of the specified legacy type.
282 *
283 * @param <T> the type
284 * @param type the class
285 * @param value the value
286 * @param provider the cast information provider
287 * @return an instance of the specified class, or {@code null} if not supported
288 */
289 @SuppressWarnings("unchecked")
290 public static <T> T valueToLegacyType(Class<T> type, Value value, CastDataProvider provider) {
291 if (type == Date.class) {
292 return (T) toDate(provider, null, value);
293 } else if (type == Time.class) {
294 return (T) toTime(provider, null, value);
295 } else if (type == Timestamp.class) {
296 return (T) toTimestamp(provider, null, value);
297 } else if (type == java.util.Date.class) {
298 return (T) new java.util.Date(toTimestamp(provider, null, value).getTime());
299 } else if (type == Calendar.class) {
300 GregorianCalendar calendar = new GregorianCalendar();
301 calendar.setGregorianChange(PROLEPTIC_GREGORIAN_CHANGE);
302 calendar.setTime(toTimestamp(provider, calendar.getTimeZone(), value));
303 return (T) calendar;
304 } else {
305 return null;
306 }
307 }
308
309 /**
310 * Get the type information for the given legacy Java class.
311 *
312 * @param clazz
313 * the Java class
314 * @return the value type, or {@code null} if not supported
315 */
316 public static TypeInfo legacyClassToType(Class<?> clazz) {
317 if (Date.class.isAssignableFrom(clazz)) {
318 return TypeInfo.TYPE_DATE;
319 } else if (Time.class.isAssignableFrom(clazz)) {
320 return TypeInfo.TYPE_TIME;
321 } else if (java.util.Date.class.isAssignableFrom(clazz) || Calendar.class.isAssignableFrom(clazz)) {
322 return TypeInfo.TYPE_TIMESTAMP;
323 } else{
324 return null;
325 }
326 }
327
328 }