提交 81eb77a0 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #334 from stevemcleod/master

Caching result of Calendar.get(Calendar.ZONE_OFFSET)
...@@ -67,6 +67,16 @@ public class DateTimeUtils { ...@@ -67,6 +67,16 @@ public class DateTimeUtils {
private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE = private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE =
new ThreadLocal<Calendar>(); new ThreadLocal<Calendar>();
/**
* Observed JVM behaviour is that if the timezone of the host computer is changed while the JVM is running,
* the zone offset does not change but keeps the initial value. So it is correct to measure this once and use
* this value throughout the JVM's lifecycle.
*
* In any case, it is safer to use a fixed value throughout the duration of the JVM's life, rather than have
* this offset change, possibly midway through a long-running query.
*/
private static int zoneOffsetMillis = Calendar.getInstance().get(Calendar.ZONE_OFFSET);
private DateTimeUtils() { private DateTimeUtils() {
// utility class // utility class
} }
...@@ -77,6 +87,7 @@ public class DateTimeUtils { ...@@ -77,6 +87,7 @@ public class DateTimeUtils {
*/ */
public static void resetCalendar() { public static void resetCalendar() {
CACHED_CALENDAR.remove(); CACHED_CALENDAR.remove();
zoneOffsetMillis = Calendar.getInstance().get(Calendar.ZONE_OFFSET);
} }
/** /**
...@@ -499,8 +510,7 @@ public class DateTimeUtils { ...@@ -499,8 +510,7 @@ public class DateTimeUtils {
* @return the milliseconds * @return the milliseconds
*/ */
public static long getTimeLocalWithoutDst(java.util.Date d) { public static long getTimeLocalWithoutDst(java.util.Date d) {
Calendar calendar = getCalendar(); return d.getTime() + zoneOffsetMillis;
return d.getTime() + calendar.get(Calendar.ZONE_OFFSET);
} }
/** /**
...@@ -511,8 +521,9 @@ public class DateTimeUtils { ...@@ -511,8 +521,9 @@ public class DateTimeUtils {
* @return the number of milliseconds in UTC * @return the number of milliseconds in UTC
*/ */
public static long getTimeUTCWithoutDst(long millis) { public static long getTimeUTCWithoutDst(long millis) {
return millis - getCalendar().get(Calendar.ZONE_OFFSET); return millis - zoneOffsetMillis;
} }
/** /**
* Return the day of week according to the ISO 8601 specification. Week * Return the day of week according to the ISO 8601 specification. Week
* starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601 * starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论