提交 20103f32 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not call TimeZone.getOffset(long) twice

上级 e1dbbe4c
...@@ -122,16 +122,17 @@ public class DateTimeUtils { ...@@ -122,16 +122,17 @@ public class DateTimeUtils {
} }
/** /**
* Returns local time zone. * Returns local time zone offset for a specified timestamp.
* *
* @return local time zone * @param ms milliseconds since Epoch in UTC
* @return local time zone offset
*/ */
static TimeZone getTimeZone() { public static int getTimeZoneOffset(long ms) {
TimeZone tz = timeZone; TimeZone tz = timeZone;
if (tz == null) { if (tz == null) {
timeZone = tz = TimeZone.getDefault(); timeZone = tz = TimeZone.getDefault();
} }
return tz; return tz.getOffset(ms);
} }
/** /**
...@@ -568,6 +569,7 @@ public class DateTimeUtils { ...@@ -568,6 +569,7 @@ public class DateTimeUtils {
} }
} else { } else {
long millis = convertDateTimeValueToMillis(tz, dateValue, nanos / 1_000_000); long millis = convertDateTimeValueToMillis(tz, dateValue, nanos / 1_000_000);
millis += getTimeZoneOffset(millis);
dateValue = dateValueFromDate(millis); dateValue = dateValueFromDate(millis);
nanos = nanos % 1_000_000 + nanosFromDate(millis); nanos = nanos % 1_000_000 + nanosFromDate(millis);
} }
...@@ -1119,14 +1121,12 @@ public class DateTimeUtils { ...@@ -1119,14 +1121,12 @@ public class DateTimeUtils {
} }
/** /**
* Convert a UTC datetime in millis to an encoded date in the default * Convert a local datetime in millis to an encoded date.
* timezone.
* *
* @param ms the milliseconds * @param ms the milliseconds
* @return the date value * @return the date value
*/ */
public static long dateValueFromDate(long ms) { public static long dateValueFromDate(long ms) {
ms += getTimeZone().getOffset(ms);
long absoluteDay = ms / MILLIS_PER_DAY; long absoluteDay = ms / MILLIS_PER_DAY;
// Round toward negative infinity // Round toward negative infinity
if (ms < 0 && (absoluteDay * MILLIS_PER_DAY != ms)) { if (ms < 0 && (absoluteDay * MILLIS_PER_DAY != ms)) {
...@@ -1152,14 +1152,12 @@ public class DateTimeUtils { ...@@ -1152,14 +1152,12 @@ public class DateTimeUtils {
} }
/** /**
* Convert a time in milliseconds in UTC to the nanoseconds since midnight * Convert a time in milliseconds in local time to the nanoseconds since midnight.
* (in the default timezone).
* *
* @param ms the milliseconds * @param ms the milliseconds
* @return the nanoseconds * @return the nanoseconds
*/ */
public static long nanosFromDate(long ms) { public static long nanosFromDate(long ms) {
ms += getTimeZone().getOffset(ms);
long absoluteDay = ms / MILLIS_PER_DAY; long absoluteDay = ms / MILLIS_PER_DAY;
// Round toward negative infinity // Round toward negative infinity
if (ms < 0 && (absoluteDay * MILLIS_PER_DAY != ms)) { if (ms < 0 && (absoluteDay * MILLIS_PER_DAY != ms)) {
...@@ -1236,7 +1234,7 @@ public class DateTimeUtils { ...@@ -1236,7 +1234,7 @@ public class DateTimeUtils {
* @return timestamp with time zone with specified value and current time zone * @return timestamp with time zone with specified value and current time zone
*/ */
public static ValueTimestampTimeZone timestampTimeZoneFromMillis(long ms) { public static ValueTimestampTimeZone timestampTimeZoneFromMillis(long ms) {
int offset = getTimeZone().getOffset(ms); int offset = getTimeZoneOffset(ms);
ms += offset; ms += offset;
long absoluteDay = ms / MILLIS_PER_DAY; long absoluteDay = ms / MILLIS_PER_DAY;
// Round toward negative infinity // Round toward negative infinity
......
...@@ -1043,7 +1043,8 @@ public abstract class Value { ...@@ -1043,7 +1043,8 @@ public abstract class Value {
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this; ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos(); long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
long millis = DateTimeUtils.getMillis(dateValue, timeNanos, ts.getTimeZoneOffsetMins()); long millis = DateTimeUtils.getMillis(dateValue, timeNanos, ts.getTimeZoneOffsetMins());
return ValueTime.fromNanos(DateTimeUtils.nanosFromDate(millis) + timeNanos % 1_000_000); return ValueTime.fromNanos(DateTimeUtils.nanosFromDate(millis + DateTimeUtils.getTimeZoneOffset(millis))
+ timeNanos % 1_000_000);
} }
case ENUM: case ENUM:
throw getDataConversionError(TIME); throw getDataConversionError(TIME);
......
...@@ -47,7 +47,8 @@ public class ValueDate extends Value { ...@@ -47,7 +47,8 @@ public class ValueDate extends Value {
* @return the value * @return the value
*/ */
public static ValueDate get(Date date) { public static ValueDate get(Date date) {
return fromDateValue(DateTimeUtils.dateValueFromDate(date.getTime())); long ms = date.getTime();
return fromDateValue(DateTimeUtils.dateValueFromDate(ms + DateTimeUtils.getTimeZoneOffset(ms)));
} }
/** /**
...@@ -58,7 +59,7 @@ public class ValueDate extends Value { ...@@ -58,7 +59,7 @@ public class ValueDate extends Value {
* @return the value * @return the value
*/ */
public static ValueDate fromMillis(long ms) { public static ValueDate fromMillis(long ms) {
return fromDateValue(DateTimeUtils.dateValueFromDate(ms)); return fromDateValue(DateTimeUtils.dateValueFromDate(ms + DateTimeUtils.getTimeZoneOffset(ms)));
} }
/** /**
......
...@@ -84,7 +84,8 @@ public class ValueTime extends Value { ...@@ -84,7 +84,8 @@ public class ValueTime extends Value {
* @return the value * @return the value
*/ */
public static ValueTime get(Time time) { public static ValueTime get(Time time) {
return fromNanos(DateTimeUtils.nanosFromDate(time.getTime())); long ms = time.getTime();
return fromNanos(DateTimeUtils.nanosFromDate(ms + DateTimeUtils.getTimeZoneOffset(ms)));
} }
/** /**
...@@ -95,7 +96,7 @@ public class ValueTime extends Value { ...@@ -95,7 +96,7 @@ public class ValueTime extends Value {
* @return the value * @return the value
*/ */
public static ValueTime fromMillis(long ms) { public static ValueTime fromMillis(long ms) {
return fromNanos(DateTimeUtils.nanosFromDate(ms)); return fromNanos(DateTimeUtils.nanosFromDate(ms + DateTimeUtils.getTimeZoneOffset(ms)));
} }
/** /**
......
...@@ -89,6 +89,7 @@ public class ValueTimestamp extends Value { ...@@ -89,6 +89,7 @@ public class ValueTimestamp extends Value {
public static ValueTimestamp get(Timestamp timestamp) { public static ValueTimestamp get(Timestamp timestamp) {
long ms = timestamp.getTime(); long ms = timestamp.getTime();
long nanos = timestamp.getNanos() % 1_000_000; long nanos = timestamp.getNanos() % 1_000_000;
ms += DateTimeUtils.getTimeZoneOffset(ms);
long dateValue = DateTimeUtils.dateValueFromDate(ms); long dateValue = DateTimeUtils.dateValueFromDate(ms);
nanos += DateTimeUtils.nanosFromDate(ms); nanos += DateTimeUtils.nanosFromDate(ms);
return fromDateValueAndNanos(dateValue, nanos); return fromDateValueAndNanos(dateValue, nanos);
...@@ -102,6 +103,7 @@ public class ValueTimestamp extends Value { ...@@ -102,6 +103,7 @@ public class ValueTimestamp extends Value {
* @return the value * @return the value
*/ */
public static ValueTimestamp fromMillisNanos(long ms, int nanos) { public static ValueTimestamp fromMillisNanos(long ms, int nanos) {
ms += DateTimeUtils.getTimeZoneOffset(ms);
long dateValue = DateTimeUtils.dateValueFromDate(ms); long dateValue = DateTimeUtils.dateValueFromDate(ms);
long timeNanos = nanos + DateTimeUtils.nanosFromDate(ms); long timeNanos = nanos + DateTimeUtils.nanosFromDate(ms);
return fromDateValueAndNanos(dateValue, timeNanos); return fromDateValueAndNanos(dateValue, timeNanos);
...@@ -114,6 +116,7 @@ public class ValueTimestamp extends Value { ...@@ -114,6 +116,7 @@ public class ValueTimestamp extends Value {
* @return the value * @return the value
*/ */
public static ValueTimestamp fromMillis(long ms) { public static ValueTimestamp fromMillis(long ms) {
ms += DateTimeUtils.getTimeZoneOffset(ms);
long dateValue = DateTimeUtils.dateValueFromDate(ms); long dateValue = DateTimeUtils.dateValueFromDate(ms);
long nanos = DateTimeUtils.nanosFromDate(ms); long nanos = DateTimeUtils.nanosFromDate(ms);
return fromDateValueAndNanos(dateValue, nanos); return fromDateValueAndNanos(dateValue, nanos);
......
...@@ -462,8 +462,10 @@ public class TestDate extends TestBase { ...@@ -462,8 +462,10 @@ public class TestDate extends TestBase {
// test for bug on Java 1.8.0_60 in "Europe/Moscow" timezone. // test for bug on Java 1.8.0_60 in "Europe/Moscow" timezone.
// Doesn't affect most other timezones // Doesn't affect most other timezones
long millis = 1407437460000L; long millis = 1407437460000L;
long result1 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis)); long ms = DateTimeUtils.getTimeUTCWithoutDst(millis);
long result2 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis)); ms += DateTimeUtils.getTimeZoneOffset(ms);
long result1 = DateTimeUtils.nanosFromDate(ms);
long result2 = DateTimeUtils.nanosFromDate(ms);
assertEquals(result1, result2); assertEquals(result1, result2);
} }
......
...@@ -159,6 +159,7 @@ public class TestDateTimeUtils extends TestBase { ...@@ -159,6 +159,7 @@ public class TestDateTimeUtils extends TestBase {
gc.set(year, month - 1, day, j / 2, (j & 1) * 30, 0); gc.set(year, month - 1, day, j / 2, (j & 1) * 30, 0);
long timeMillis = gc.getTimeInMillis(); long timeMillis = gc.getTimeInMillis();
ValueTimestamp ts = DateTimeUtils.convertTimestamp(new Timestamp(timeMillis), gc); ValueTimestamp ts = DateTimeUtils.convertTimestamp(new Timestamp(timeMillis), gc);
timeMillis += DateTimeUtils.getTimeZoneOffset(timeMillis);
assertEquals(ts.getDateValue(), DateTimeUtils.dateValueFromDate(timeMillis)); assertEquals(ts.getDateValue(), DateTimeUtils.dateValueFromDate(timeMillis));
assertEquals(ts.getTimeNanos(), DateTimeUtils.nanosFromDate(timeMillis)); assertEquals(ts.getTimeNanos(), DateTimeUtils.nanosFromDate(timeMillis));
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论