Debian Codegraph @

src/main/org/h2/value/ValueTime.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 import static org.h2.util.DateTimeUtils.NANOS_PER_HOUR;
14
15 /**
16 * Implementation of the TIME data type.
17 */
18 public final class ValueTime extends Value {
19
20 /**
21 * The default precision and display size of the textual representation of a time.
22 * Example: 10:00:00
23 */
24 public static final int DEFAULT_PRECISION = 8;
25
26 /**
27 * The maximum precision and display size of the textual representation of a time.
28 * Example: 10:00:00.123456789
29 */
30 public static final int MAXIMUM_PRECISION = 18;
31
32 /**
33 * The default scale for time.
34 */
35 public static final int DEFAULT_SCALE = 0;
36
37 /**
38 * The maximum scale for time.
39 */
40 public static final int MAXIMUM_SCALE = 9;
41
42 private static final ValueTime[] STATIC_CACHE;
43
44 /**
45 * Nanoseconds since midnight
46 */
47 private final long nanos;
48
49 static {
50 ValueTime[] cache = new ValueTime[24];
51 for (int hour = 0; hour < 24; hour++) {
52 cache[hour] = new ValueTime(hour * NANOS_PER_HOUR);
53 }
54 STATIC_CACHE = cache;
55 }
56
57 /**
58 * @param nanos nanoseconds since midnight
59 */
60 private ValueTime(long nanos) {
61 this.nanos = nanos;
62 }
63
64 /**
65 * Get or create a time value.
66 *
67 * @param nanos the nanoseconds since midnight
68 * @return the value
69 */
70 public static ValueTime fromNanos(long nanos) {
71 if (nanos < 0L || nanos >= DateTimeUtils.NANOS_PER_DAY) {
72 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, "TIME",
73 DateTimeUtils.appendTime(new StringBuilder(), nanos).toString());
74 }
75 if (nanos % NANOS_PER_HOUR == 0L) {
76 return STATIC_CACHE[(int) (nanos / NANOS_PER_HOUR)];
77 }
78 return (ValueTime) Value.cache(new ValueTime(nanos));
79 }
80
81 /**
82 * Parse a string to a ValueTime.
83 *
84 * @param s the string to parse
85 * @param provider
86 * the cast information provider, may be {@code null} for
87 * literals without time zone
88 * @return the time
89 */
90 public static ValueTime parse(String s, CastDataProvider provider) {
91 try {
92 return (ValueTime) DateTimeUtils.parseTime(s, provider, false);
93 } catch (Exception e) {
94 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2,
95 e, "TIME", s);
96 }
97 }
98
99 /**
100 * @return nanoseconds since midnight
101 */
102 public long getNanos() {
103 return nanos;
104 }
105
106 @Override
107 public TypeInfo getType() {
108 return TypeInfo.TYPE_TIME;
109 }
110
111 @Override
112 public int getValueType() {
113 return TIME;
114 }
115
116 @Override
117 public String getString() {
118 return DateTimeUtils.appendTime(new StringBuilder(MAXIMUM_PRECISION), nanos).toString();
119 }
120
121 @Override
122 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
123 return DateTimeUtils.appendTime(builder.append("TIME '"), nanos).append('\'');
124 }
125
126 @Override
127 public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) {
128 return Long.compare(nanos, ((ValueTime) o).nanos);
129 }
130
131 @Override
132 public boolean equals(Object other) {
133 return this == other || other instanceof ValueTime && nanos == ((ValueTime) other).nanos;
134 }
135
136 @Override
137 public int hashCode() {
138 return (int) (nanos ^ (nanos >>> 32));
139 }
140
141 @Override
142 public Value add(Value v) {
143 ValueTime t = (ValueTime) v;
144 return ValueTime.fromNanos(nanos + t.getNanos());
145 }
146
147 @Override
148 public Value subtract(Value v) {
149 ValueTime t = (ValueTime) v;
150 return ValueTime.fromNanos(nanos - t.getNanos());
151 }
152
153 @Override
154 public Value multiply(Value v) {
155 return ValueTime.fromNanos((long) (nanos * v.getDouble()));
156 }
157
158 @Override
159 public Value divide(Value v, TypeInfo quotientType) {
160 return ValueTime.fromNanos((long) (nanos / v.getDouble()));
161 }
162
163 }