提交 5d667744 authored 作者: noelgrandin's avatar noelgrandin

improve method names and add comments to ValueTimestamp and related classes

上级 fb28c029
......@@ -261,7 +261,7 @@ public class ValueDataType implements DataType {
case Value.TIMESTAMP: {
ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue();
long nanos = ts.getNanos();
long nanos = ts.getTimeNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
buff.put((byte) type).
......
......@@ -519,7 +519,7 @@ public class Data {
ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue();
writeVarLong(dateValue);
long nanos = ts.getNanos();
long nanos = ts.getTimeNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
writeVarLong(millis);
......@@ -1006,7 +1006,7 @@ public class Data {
if (SysProperties.STORE_LOCAL_TIME) {
ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue();
long nanos = ts.getNanos();
long nanos = ts.getTimeNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
return 1 + getVarLongLen(dateValue) + getVarLongLen(millis) +
......
......@@ -149,7 +149,7 @@ public class DateTimeUtils {
cal.clear();
cal.setLenient(true);
long dateValue = ts.getDateValue();
long nanos = ts.getNanos();
long nanos = ts.getTimeNanos();
long millis = nanos / 1000000;
nanos -= millis * 1000000;
long s = millis / 1000;
......@@ -651,13 +651,13 @@ public class DateTimeUtils {
* timezone.
*
* @param dateValue the date value
* @param nanos the nanoseconds since midnight
* @param timeNanos the nanoseconds since midnight
* @return the timestamp
*/
public static Timestamp convertDateValueToTimestamp(long dateValue,
long nanos) {
long millis = nanos / 1000000;
nanos -= millis * 1000000;
long timeNanos) {
long millis = timeNanos / 1000000;
timeNanos -= millis * 1000000;
long s = millis / 1000;
millis -= s * 1000;
long m = s / 60;
......@@ -670,7 +670,7 @@ public class DateTimeUtils {
dayFromDateValue(dateValue),
(int) h, (int) m, (int) s, 0);
Timestamp ts = new Timestamp(ms);
ts.setNanos((int) (nanos + millis * 1000000));
ts.setNanos((int) (timeNanos + millis * 1000000));
return ts;
}
......
......@@ -379,7 +379,7 @@ public class Transfer {
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
ValueTimestamp ts = (ValueTimestamp) v;
writeLong(ts.getDateValue());
writeLong(ts.getNanos());
writeLong(ts.getTimeNanos());
} else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
Timestamp ts = v.getTimestamp();
writeLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
......
......@@ -732,7 +732,7 @@ public abstract class Value {
return ValueTime.fromNanos(0);
case TIMESTAMP:
return ValueTime.fromNanos(
((ValueTimestamp) this).getNanos());
((ValueTimestamp) this).getTimeNanos());
}
break;
}
......
......@@ -40,23 +40,29 @@ public class ValueTimestamp extends Value {
*/
static final int DEFAULT_SCALE = 10;
/**
* A bit field with bits for the year, month, and day (see DateTimeUtils for encoding)
*/
private final long dateValue;
private final long nanos;
/**
* The nanoseconds since midnight.
*/
private final long timeNanos;
private ValueTimestamp(long dateValue, long nanos) {
private ValueTimestamp(long dateValue, long timeNanos) {
this.dateValue = dateValue;
this.nanos = nanos;
this.timeNanos = timeNanos;
}
/**
* Get or create a date value for the given date.
*
* @param dateValue the date value
* @param nanos the nanoseconds
* @param dateValue the date value, a bit field with bits for the year, month, and day
* @param timeNanos the nanoseconds since midnight
* @return the value
*/
public static ValueTimestamp fromDateValueAndNanos(long dateValue, long nanos) {
return (ValueTimestamp) Value.cache(new ValueTimestamp(dateValue, nanos));
public static ValueTimestamp fromDateValueAndNanos(long dateValue, long timeNanos) {
return (ValueTimestamp) Value.cache(new ValueTimestamp(dateValue, timeNanos));
}
/**
......@@ -165,17 +171,23 @@ public class ValueTimestamp extends Value {
return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos);
}
/**
* A bit field with bits for the year, month, and day (see DateTimeUtils for encoding)
*/
public long getDateValue() {
return dateValue;
}
public long getNanos() {
return nanos;
/**
* The nanoseconds since midnight.
*/
public long getTimeNanos() {
return timeNanos;
}
@Override
public Timestamp getTimestamp() {
return DateTimeUtils.convertDateValueToTimestamp(dateValue, nanos);
return DateTimeUtils.convertDateValueToTimestamp(dateValue, timeNanos);
}
@Override
......@@ -188,7 +200,7 @@ public class ValueTimestamp extends Value {
StringBuilder buff = new StringBuilder(DISPLAY_SIZE);
ValueDate.appendDate(buff, dateValue);
buff.append(' ');
ValueTime.appendTime(buff, nanos, true);
ValueTime.appendTime(buff, timeNanos, true);
return buff.toString();
}
......@@ -220,7 +232,7 @@ public class ValueTimestamp extends Value {
if (targetScale < 0) {
throw DbException.getInvalidValueException("scale", targetScale);
}
long n = nanos;
long n = timeNanos;
BigDecimal bd = BigDecimal.valueOf(n);
bd = bd.movePointLeft(9);
bd = ValueDecimal.setScale(bd, targetScale);
......@@ -239,7 +251,7 @@ public class ValueTimestamp extends Value {
if (c != 0) {
return c;
}
return MathUtils.compareLong(nanos, t.nanos);
return MathUtils.compareLong(timeNanos, t.timeNanos);
}
@Override
......@@ -250,12 +262,12 @@ public class ValueTimestamp extends Value {
return false;
}
ValueTimestamp x = (ValueTimestamp) other;
return dateValue == x.dateValue && nanos == x.nanos;
return dateValue == x.dateValue && timeNanos == x.timeNanos;
}
@Override
public int hashCode() {
return (int) (dateValue ^ (dateValue >>> 32) ^ nanos ^ (nanos >>> 32));
return (int) (dateValue ^ (dateValue >>> 32) ^ timeNanos ^ (timeNanos >>> 32));
}
@Override
......@@ -274,7 +286,7 @@ public class ValueTimestamp extends Value {
ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue);
long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue);
return DateTimeUtils.normalizeTimestamp(d1 + d2, nanos + t.nanos);
return DateTimeUtils.normalizeTimestamp(d1 + d2, timeNanos + t.timeNanos);
}
@Override
......@@ -282,7 +294,7 @@ public class ValueTimestamp extends Value {
ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue);
long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue);
return DateTimeUtils.normalizeTimestamp(d1 - d2, nanos - t.nanos);
return DateTimeUtils.normalizeTimestamp(d1 - d2, timeNanos - t.timeNanos);
}
}
......@@ -234,7 +234,7 @@ public class TestDate extends TestBase {
assertEquals("TIMESTAMP '2001-01-01 01:01:01.111'", t1.toString());
assertEquals(Value.TIMESTAMP, t1.getType());
long dateValue = t1.getDateValue();
long nanos = t1.getNanos();
long nanos = t1.getTimeNanos();
assertEquals((int) ((dateValue >>> 32) ^ dateValue ^
(nanos >>> 32) ^ nanos),
t1.hashCode());
......@@ -311,7 +311,7 @@ public class TestDate extends TestBase {
assertEquals(0, DateTimeUtils.absoluteDayFromDateValue(
ValueTimestamp.parse("1970-01-01").getDateValue()));
assertEquals(0, ValueTimestamp.parse(
"1970-01-01").getNanos());
"1970-01-01").getTimeNanos());
assertEquals(0, ValueTimestamp.parse(
"1970-01-01 00:00:00.000 UTC").getTimestamp().getTime());
assertEquals(0, ValueTimestamp.parse(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论