File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueTime.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import java.sql.Time;
12 import org.h2.constant.ErrorCode;
13 import org.h2.message.DbException;
14 import org.h2.util.DateTimeUtils;
15 import org.h2.util.MathUtils;
16 import org.h2.util.StringUtils;
17
18 /**
19 * Implementation of the TIME data type.
20 */
21 public class ValueTime extends Value {
22
23 /**
24 * The precision in digits.
25 */
26 public static final int PRECISION = 6;
27
28 /**
29 * The display size of the textual representation of a time.
30 * Example: 10:00:00
31 */
32 static final int DISPLAY_SIZE = 8;
33
34 private final long nanos;
35
36 private ValueTime(long nanos) {
37 this.nanos = nanos;
38 }
39
40 /**
41 * Get or create a time value.
42 *
43 * @param nanos the nanoseconds
44 * @return the value
45 */
46 public static ValueTime fromNanos(long nanos) {
47 return (ValueTime) Value.cache(new ValueTime(nanos));
48 }
49
50 /**
51 * Get or create a time value for the given time.
52 *
53 * @param time the time
54 * @return the value
55 */
56 public static ValueTime get(Time time) {
57 return fromNanos(DateTimeUtils.nanosFromDate(time.getTime()));
58 }
59
60 /**
61 * Parse a string to a ValueTime.
62 *
63 * @param s the string to parse
64 * @return the time
65 */
66
67 public static ValueTime parse(String s) {
68 try {
69 return fromNanos(DateTimeUtils.parseTimeNanos(s, 0, s.length(), false));
70 } catch (Exception e) {
71 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2,
72 e, "TIME", s);
73 }
74 }
75
76 public long getNanos() {
77 return nanos;
78 }
79
80 public Time getTime() {
81 return DateTimeUtils.convertNanoToTime(nanos);
82 }
83
84 public int getType() {
85 return Value.TIME;
86 }
87
88 public String getString() {
89 StringBuilder buff = new StringBuilder(DISPLAY_SIZE);
90 appendTime(buff, nanos, false);
91 return buff.toString();
92 }
93
94 public String getSQL() {
95 return "TIME '" + getString() + "'";
96 }
97
98 public long getPrecision() {
99 return PRECISION;
100 }
101
102 public int getDisplaySize() {
103 return DISPLAY_SIZE;
104 }
105
106 protected int compareSecure(Value o, CompareMode mode) {
107 return MathUtils.compareLong(nanos, ((ValueTime) o).nanos);
108 }
109
110 public boolean equals(Object other) {
111 if (this == other) {
112 return true;
113 }
114 return other instanceof ValueTime && nanos == (((ValueTime) other).nanos);
115 }
116
117 public int hashCode() {
118 return (int) (nanos ^ (nanos >>> 32));
119 }
120
121 public Object getObject() {
122 return getTime();
123 }
124
125 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
126 prep.setTime(parameterIndex, getTime());
127 }
128
129 public Value add(Value v) {
130 ValueTime t = (ValueTime) v.convertTo(Value.TIME);
131 return ValueTime.fromNanos(nanos + t.getNanos());
132 }
133
134 public Value subtract(Value v) {
135 ValueTime t = (ValueTime) v.convertTo(Value.TIME);
136 return ValueTime.fromNanos(nanos - t.getNanos());
137 }
138
139 public Value multiply(Value v) {
140 return ValueTime.fromNanos((long) (nanos * v.getDouble()));
141 }
142
143 public Value divide(Value v) {
144 return ValueTime.fromNanos((long) (nanos / v.getDouble()));
145 }
146
147 public int getSignum() {
148 return Long.signum(nanos);
149 }
150
151 public Value negate() {
152 return ValueTime.fromNanos(-nanos);
153 }
154
155 /**
156 * Append a time to the string builder.
157 *
158 * @param buff the target string builder
159 * @param nanos the time in nanoseconds
160 * @param alwaysAddMillis whether to always add at least ".0"
161 */
162 static void appendTime(StringBuilder buff, long nanos, boolean alwaysAddMillis) {
163 if (nanos < 0) {
164 buff.append('-');
165 nanos = -nanos;
166 }
167 long ms = nanos / 1000000;
168 nanos -= ms * 1000000;
169 long s = ms / 1000;
170 ms -= s * 1000;
171 long m = s / 60;
172 s -= m * 60;
173 long h = m / 60;
174 m -= h * 60;
175 StringUtils.appendZeroPadded(buff, 2, h);
176 buff.append(':');
177 StringUtils.appendZeroPadded(buff, 2, m);
178 buff.append(':');
179 StringUtils.appendZeroPadded(buff, 2, s);
180 if (alwaysAddMillis || ms > 0 || nanos > 0) {
181 buff.append('.');
182 int start = buff.length();
183 StringUtils.appendZeroPadded(buff, 3, ms);
184 if (nanos > 0) {
185 StringUtils.appendZeroPadded(buff, 6, nanos);
186 }
187 for (int i = buff.length() - 1; i > start; i--) {
188 if (buff.charAt(i) != '0') {
189 break;
190 }
191 buff.deleteCharAt(i);
192 }
193 }
194 }
195
196 }
197