Debian Codegraph @

src/main/org/h2/value/ValueTimestampTimeZone.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.value;
7
8 import org.h2.api.ErrorCode;
9 import org.h2.engine.CastDataProvider;
10 import org.h2.message.DbException;
11 import org.h2.util.DateTimeUtils;
12
13 /**
14 * Implementation of the TIMESTAMP WITH TIME ZONE data type.
15 */
16 public final class ValueTimestampTimeZone extends Value {
17
18 /**
19 * The default precision and display size of the textual representation of a timestamp.
20 * Example: 2001-01-01 23:59:59.123456+10:00
21 */
22 public static final int DEFAULT_PRECISION = 32;
23
24 /**
25 * The maximum precision and display size of the textual representation of a timestamp.
26 * Example: 2001-01-01 23:59:59.123456789+10:00
27 */
28 public static final int MAXIMUM_PRECISION = 35;
29
30 /**
31 * A bit field with bits for the year, month, and day (see DateTimeUtils for
32 * encoding)
33 */
34 private final long dateValue;
35 /**
36 * The nanoseconds since midnight.
37 */
38 private final long timeNanos;
39 /**
40 * Time zone offset from UTC in seconds, range of -18 hours to +18 hours. This
41 * range is compatible with OffsetDateTime from JSR-310.
42 */
43 private final int timeZoneOffsetSeconds;
44
45 private ValueTimestampTimeZone(long dateValue, long timeNanos, int timeZoneOffsetSeconds) {
46 if (dateValue < DateTimeUtils.MIN_DATE_VALUE || dateValue > DateTimeUtils.MAX_DATE_VALUE) {
47 throw new IllegalArgumentException("dateValue out of range " + dateValue);
48 }
49 if (timeNanos < 0 || timeNanos >= DateTimeUtils.NANOS_PER_DAY) {
50 throw new IllegalArgumentException(
51 "timeNanos out of range " + timeNanos);
52 }
53 /*
54 * Some current and historic time zones have offsets larger than 12 hours.
55 * JSR-310 determines 18 hours as maximum possible offset in both directions, so
56 * we use this limit too for compatibility.
57 */
58 if (timeZoneOffsetSeconds < (-18 * 60 * 60)
59 || timeZoneOffsetSeconds > (18 * 60 * 60)) {
60 throw new IllegalArgumentException(
61 "timeZoneOffsetSeconds out of range " + timeZoneOffsetSeconds);
62 }
63 this.dateValue = dateValue;
64 this.timeNanos = timeNanos;
65 this.timeZoneOffsetSeconds = timeZoneOffsetSeconds;
66 }
67
68 /**
69 * Get or create a date value for the given date.
70 *
71 * @param dateValue the date value, a bit field with bits for the year,
72 * month, and day
73 * @param timeNanos the nanoseconds since midnight
74 * @param timeZoneOffsetSeconds the timezone offset in seconds
75 * @return the value
76 */
77 public static ValueTimestampTimeZone fromDateValueAndNanos(long dateValue, long timeNanos,
78 int timeZoneOffsetSeconds) {
79 return (ValueTimestampTimeZone) Value.cache(new ValueTimestampTimeZone(
80 dateValue, timeNanos, timeZoneOffsetSeconds));
81 }
82
83 /**
84 * Parse a string to a ValueTimestamp. This method supports the format
85 * +/-year-month-day hour:minute:seconds.fractional and an optional timezone
86 * part.
87 *
88 * @param s the string to parse
89 * @param provider
90 * the cast information provider, may be {@code null} for
91 * literals with time zone
92 * @return the date
93 */
94 public static ValueTimestampTimeZone parse(String s, CastDataProvider provider) {
95 try {
96 return (ValueTimestampTimeZone) DateTimeUtils.parseTimestamp(s, provider, true);
97 } catch (Exception e) {
98 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "TIMESTAMP WITH TIME ZONE", s);
99 }
100 }
101
102 /**
103 * A bit field with bits for the year, month, and day (see DateTimeUtils for
104 * encoding).
105 *
106 * @return the data value
107 */
108 public long getDateValue() {
109 return dateValue;
110 }
111
112 /**
113 * The nanoseconds since midnight.
114 *
115 * @return the nanoseconds
116 */
117 public long getTimeNanos() {
118 return timeNanos;
119 }
120
121 /**
122 * The time zone offset in seconds.
123 *
124 * @return the offset
125 */
126 public int getTimeZoneOffsetSeconds() {
127 return timeZoneOffsetSeconds;
128 }
129
130 @Override
131 public TypeInfo getType() {
132 return TypeInfo.TYPE_TIMESTAMP_TZ;
133 }
134
135 @Override
136 public int getValueType() {
137 return TIMESTAMP_TZ;
138 }
139
140 @Override
141 public int getMemory() {
142 // Java 11 with -XX:-UseCompressedOops
143 return 40;
144 }
145
146 @Override
147 public String getString() {
148 return toString(new StringBuilder(MAXIMUM_PRECISION), false).toString();
149 }
150
151 /**
152 * Returns value as string in ISO format.
153 *
154 * @return value as string in ISO format
155 */
156 public String getISOString() {
157 return toString(new StringBuilder(MAXIMUM_PRECISION), true).toString();
158 }
159
160 @Override
161 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
162 return toString(builder.append("TIMESTAMP WITH TIME ZONE '"), false).append('\'');
163 }
164
165 private StringBuilder toString(StringBuilder builder, boolean iso) {
166 DateTimeUtils.appendDate(builder, dateValue).append(iso ? 'T' : ' ');
167 DateTimeUtils.appendTime(builder, timeNanos);
168 return DateTimeUtils.appendTimeZone(builder, timeZoneOffsetSeconds);
169 }
170
171 @Override
172 public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) {
173 ValueTimestampTimeZone t = (ValueTimestampTimeZone) o;
174 // Maximum time zone offset is +/-18 hours so difference in days between local
175 // and UTC cannot be more than one day
176 long dateValueA = dateValue;
177 long timeA = timeNanos - timeZoneOffsetSeconds * DateTimeUtils.NANOS_PER_SECOND;
178 if (timeA < 0) {
179 timeA += DateTimeUtils.NANOS_PER_DAY;
180 dateValueA = DateTimeUtils.decrementDateValue(dateValueA);
181 } else if (timeA >= DateTimeUtils.NANOS_PER_DAY) {
182 timeA -= DateTimeUtils.NANOS_PER_DAY;
183 dateValueA = DateTimeUtils.incrementDateValue(dateValueA);
184 }
185 long dateValueB = t.dateValue;
186 long timeB = t.timeNanos - t.timeZoneOffsetSeconds * DateTimeUtils.NANOS_PER_SECOND;
187 if (timeB < 0) {
188 timeB += DateTimeUtils.NANOS_PER_DAY;
189 dateValueB = DateTimeUtils.decrementDateValue(dateValueB);
190 } else if (timeB >= DateTimeUtils.NANOS_PER_DAY) {
191 timeB -= DateTimeUtils.NANOS_PER_DAY;
192 dateValueB = DateTimeUtils.incrementDateValue(dateValueB);
193 }
194 int cmp = Long.compare(dateValueA, dateValueB);
195 if (cmp != 0) {
196 return cmp;
197 }
198 return Long.compare(timeA, timeB);
199 }
200
201 @Override
202 public boolean equals(Object other) {
203 if (this == other) {
204 return true;
205 } else if (!(other instanceof ValueTimestampTimeZone)) {
206 return false;
207 }
208 ValueTimestampTimeZone x = (ValueTimestampTimeZone) other;
209 return dateValue == x.dateValue && timeNanos == x.timeNanos
210 && timeZoneOffsetSeconds == x.timeZoneOffsetSeconds;
211 }
212
213 @Override
214 public int hashCode() {
215 return (int) (dateValue ^ (dateValue >>> 32) ^ timeNanos
216 ^ (timeNanos >>> 32) ^ timeZoneOffsetSeconds);
217 }
218
219 }