| 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 data type. |
| 15 |
*/ |
| 16 |
public final class ValueTimestamp 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 |
| 21 |
*/ |
| 22 |
public static final int DEFAULT_PRECISION = 26; |
| 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 |
| 27 |
*/ |
| 28 |
public static final int MAXIMUM_PRECISION = 29; |
| 29 |
|
| 30 |
/** |
| 31 |
* The default scale for timestamps. |
| 32 |
*/ |
| 33 |
public static final int DEFAULT_SCALE = 6; |
| 34 |
|
| 35 |
/** |
| 36 |
* The maximum scale for timestamps. |
| 37 |
*/ |
| 38 |
public static final int MAXIMUM_SCALE = 9; |
| 39 |
|
| 40 |
/** |
| 41 |
* A bit field with bits for the year, month, and day (see DateTimeUtils for |
| 42 |
* encoding) |
| 43 |
*/ |
| 44 |
private final long dateValue; |
| 45 |
/** |
| 46 |
* The nanoseconds since midnight. |
| 47 |
*/ |
| 48 |
private final long timeNanos; |
| 49 |
|
| 50 |
private ValueTimestamp(long dateValue, long timeNanos) { |
| 51 |
if (dateValue < DateTimeUtils.MIN_DATE_VALUE || dateValue > DateTimeUtils.MAX_DATE_VALUE) { |
| 52 |
throw new IllegalArgumentException("dateValue out of range " + dateValue); |
| 53 |
} |
| 54 |
if (timeNanos < 0 || timeNanos >= DateTimeUtils.NANOS_PER_DAY) { |
| 55 |
throw new IllegalArgumentException("timeNanos out of range " + timeNanos); |
| 56 |
} |
| 57 |
this.dateValue = dateValue; |
| 58 |
this.timeNanos = timeNanos; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get or create a date value for the given date. |
| 63 |
* |
| 64 |
* @param dateValue the date value, a bit field with bits for the year, |
| 65 |
* month, and day |
| 66 |
* @param timeNanos the nanoseconds since midnight |
| 67 |
* @return the value |
| 68 |
*/ |
| 69 |
public static ValueTimestamp fromDateValueAndNanos(long dateValue, long timeNanos) { |
| 70 |
return (ValueTimestamp) Value.cache(new ValueTimestamp(dateValue, timeNanos)); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Parse a string to a ValueTimestamp, using the given {@link CastDataProvider}. |
| 75 |
* This method supports the format +/-year-month-day[ -]hour[:.]minute[:.]seconds.fractional |
| 76 |
* and an optional timezone part. |
| 77 |
* |
| 78 |
* @param s the string to parse |
| 79 |
* @param provider |
| 80 |
* the cast information provider, may be {@code null} for |
| 81 |
* literals without time zone |
| 82 |
* @return the date |
| 83 |
*/ |
| 84 |
public static ValueTimestamp parse(String s, CastDataProvider provider) { |
| 85 |
try { |
| 86 |
return (ValueTimestamp) DateTimeUtils.parseTimestamp(s, provider, false); |
| 87 |
} catch (Exception e) { |
| 88 |
throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "TIMESTAMP", s); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* A bit field with bits for the year, month, and day (see DateTimeUtils for |
| 94 |
* encoding). |
| 95 |
* |
| 96 |
* @return the data value |
| 97 |
*/ |
| 98 |
public long getDateValue() { |
| 99 |
return dateValue; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* The nanoseconds since midnight. |
| 104 |
* |
| 105 |
* @return the nanoseconds |
| 106 |
*/ |
| 107 |
public long getTimeNanos() { |
| 108 |
return timeNanos; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public TypeInfo getType() { |
| 113 |
return TypeInfo.TYPE_TIMESTAMP; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public int getValueType() { |
| 118 |
return TIMESTAMP; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public int getMemory() { |
| 123 |
return 32; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public String getString() { |
| 128 |
return toString(new StringBuilder(MAXIMUM_PRECISION), false).toString(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns value as string in ISO format. |
| 133 |
* |
| 134 |
* @return value as string in ISO format |
| 135 |
*/ |
| 136 |
public String getISOString() { |
| 137 |
return toString(new StringBuilder(MAXIMUM_PRECISION), true).toString(); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 142 |
return toString(builder.append("TIMESTAMP '"), false).append('\''); |
| 143 |
} |
| 144 |
|
| 145 |
private StringBuilder toString(StringBuilder builder, boolean iso) { |
| 146 |
DateTimeUtils.appendDate(builder, dateValue).append(iso ? 'T' : ' '); |
| 147 |
return DateTimeUtils.appendTime(builder, timeNanos); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) { |
| 152 |
ValueTimestamp t = (ValueTimestamp) o; |
| 153 |
int c = Long.compare(dateValue, t.dateValue); |
| 154 |
if (c != 0) { |
| 155 |
return c; |
| 156 |
} |
| 157 |
return Long.compare(timeNanos, t.timeNanos); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public boolean equals(Object other) { |
| 162 |
if (this == other) { |
| 163 |
return true; |
| 164 |
} else if (!(other instanceof ValueTimestamp)) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
ValueTimestamp x = (ValueTimestamp) other; |
| 168 |
return dateValue == x.dateValue && timeNanos == x.timeNanos; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public int hashCode() { |
| 173 |
return (int) (dateValue ^ (dateValue >>> 32) ^ timeNanos ^ (timeNanos >>> 32)); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public Value add(Value v) { |
| 178 |
ValueTimestamp t = (ValueTimestamp) v; |
| 179 |
long absoluteDay = DateTimeUtils.absoluteDayFromDateValue(dateValue) |
| 180 |
+ DateTimeUtils.absoluteDayFromDateValue(t.dateValue); |
| 181 |
long nanos = timeNanos + t.timeNanos; |
| 182 |
if (nanos >= DateTimeUtils.NANOS_PER_DAY) { |
| 183 |
nanos -= DateTimeUtils.NANOS_PER_DAY; |
| 184 |
absoluteDay++; |
| 185 |
} |
| 186 |
return ValueTimestamp.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay), nanos); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public Value subtract(Value v) { |
| 191 |
ValueTimestamp t = (ValueTimestamp) v; |
| 192 |
long absoluteDay = DateTimeUtils.absoluteDayFromDateValue(dateValue) |
| 193 |
- DateTimeUtils.absoluteDayFromDateValue(t.dateValue); |
| 194 |
long nanos = timeNanos - t.timeNanos; |
| 195 |
if (nanos < 0) { |
| 196 |
nanos += DateTimeUtils.NANOS_PER_DAY; |
| 197 |
absoluteDay--; |
| 198 |
} |
| 199 |
return ValueTimestamp.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay), nanos); |
| 200 |
} |
| 201 |
|
| 202 |
} |