Debian Codegraph @

src/main/org/h2/value/ValueTimeTimeZone.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 TIME WITH TIME ZONE data type.
15 */
16 public final class ValueTimeTimeZone extends Value {
17
18 /**
19 * The default precision and display size of the textual representation of a
20 * time. Example: 10:00:00+10:00
21 */
22 public static final int DEFAULT_PRECISION = 14;
23
24 /**
25 * The maximum precision and display size of the textual representation of a
26 * time. Example: 10:00:00.123456789+10:00
27 */
28 public static final int MAXIMUM_PRECISION = 24;
29
30 /**
31 * Nanoseconds since midnight
32 */
33 private final long nanos;
34
35 /**
36 * Time zone offset from UTC in seconds, range of -18 hours to +18 hours.
37 * This range is compatible with OffsetTime from JSR-310.
38 */
39 private final int timeZoneOffsetSeconds;
40
41 /**
42 * @param nanos
43 * nanoseconds since midnight
44 */
45 private ValueTimeTimeZone(long nanos, int timeZoneOffsetSeconds) {
46 this.nanos = nanos;
47 this.timeZoneOffsetSeconds = timeZoneOffsetSeconds;
48 }
49
50 /**
51 * Get or create a time value.
52 *
53 * @param nanos
54 * the nanoseconds since midnight
55 * @param timeZoneOffsetSeconds
56 * the timezone offset in seconds
57 * @return the value
58 */
59 public static ValueTimeTimeZone fromNanos(long nanos, int timeZoneOffsetSeconds) {
60 if (nanos < 0L || nanos >= DateTimeUtils.NANOS_PER_DAY) {
61 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, "TIME WITH TIME ZONE",
62 DateTimeUtils.appendTime(new StringBuilder(), nanos).toString());
63 }
64 /*
65 * Some current and historic time zones have offsets larger than 12
66 * hours. JSR-310 determines 18 hours as maximum possible offset in both
67 * directions, so we use this limit too for compatibility.
68 */
69 if (timeZoneOffsetSeconds < (-18 * 60 * 60) || timeZoneOffsetSeconds > (18 * 60 * 60)) {
70 throw new IllegalArgumentException("timeZoneOffsetSeconds " + timeZoneOffsetSeconds);
71 }
72 return (ValueTimeTimeZone) Value.cache(new ValueTimeTimeZone(nanos, timeZoneOffsetSeconds));
73 }
74
75 /**
76 * Parse a string to a ValueTime.
77 *
78 * @param s
79 * the string to parse
80 * @param provider
81 * the cast information provider, may be {@code null} for
82 * literals with time zone
83 * @return the time
84 */
85 public static ValueTimeTimeZone parse(String s, CastDataProvider provider) {
86 try {
87 return (ValueTimeTimeZone) DateTimeUtils.parseTime(s, provider, true);
88 } catch (Exception e) {
89 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "TIME WITH TIME ZONE", s);
90 }
91 }
92
93 /**
94 * @return nanoseconds since midnight
95 */
96 public long getNanos() {
97 return nanos;
98 }
99
100 /**
101 * The time zone offset in seconds.
102 *
103 * @return the offset
104 */
105 public int getTimeZoneOffsetSeconds() {
106 return timeZoneOffsetSeconds;
107 }
108
109 @Override
110 public TypeInfo getType() {
111 return TypeInfo.TYPE_TIME_TZ;
112 }
113
114 @Override
115 public int getValueType() {
116 return TIME_TZ;
117 }
118
119 @Override
120 public int getMemory() {
121 return 32;
122 }
123
124 @Override
125 public String getString() {
126 return toString(new StringBuilder(MAXIMUM_PRECISION)).toString();
127 }
128
129 @Override
130 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
131 return toString(builder.append("TIME WITH TIME ZONE '")).append('\'');
132 }
133
134 private StringBuilder toString(StringBuilder builder) {
135 return DateTimeUtils.appendTimeZone(DateTimeUtils.appendTime(builder, nanos), timeZoneOffsetSeconds);
136 }
137
138 @Override
139 public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) {
140 ValueTimeTimeZone t = (ValueTimeTimeZone) o;
141 return Long.compare(nanos - timeZoneOffsetSeconds * DateTimeUtils.NANOS_PER_SECOND,
142 t.nanos - t.timeZoneOffsetSeconds * DateTimeUtils.NANOS_PER_SECOND);
143 }
144
145 @Override
146 public boolean equals(Object other) {
147 if (this == other) {
148 return true;
149 } else if (!(other instanceof ValueTimeTimeZone)) {
150 return false;
151 }
152 ValueTimeTimeZone t = (ValueTimeTimeZone) other;
153 return nanos == t.nanos && timeZoneOffsetSeconds == t.timeZoneOffsetSeconds;
154 }
155
156 @Override
157 public int hashCode() {
158 return (int) (nanos ^ (nanos >>> 32) ^ timeZoneOffsetSeconds);
159 }
160
161 }