Skip to content

Commit d5b4731

Browse files
committed
Merge pull request h2database#163 from stevemcleod/datetimeutils_tweak
In DateTimeUtils.getTimeTry(), Calendar instances used to evaluate Timestamps were only being cached if no timezone was specified. I've added code to reuse Calendar instances if the timezone specified matches the previous use.
2 parents 194ebf9 + 9abb674 commit d5b4731

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

h2/src/main/org/h2/util/DateTimeUtils.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public class DateTimeUtils {
6262
private static final ThreadLocal<Calendar> CACHED_CALENDAR =
6363
new ThreadLocal<Calendar>();
6464

65+
/**
66+
* A cached instance of Calendar used when a timezone is specified.
67+
*/
68+
private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE =
69+
new ThreadLocal<Calendar>();
70+
6571
private DateTimeUtils() {
6672
// utility class
6773
}
@@ -82,6 +88,19 @@ private static Calendar getCalendar() {
8288
return c;
8389
}
8490

91+
/**
92+
* @param tz timezone for the calendar, is never null
93+
* @return a calendar instance for the specified timezone. A cached instance is returned where possible
94+
*/
95+
private static Calendar getCalendar(TimeZone tz) {
96+
Calendar c = CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.get();
97+
if (c == null || !c.getTimeZone().equals(tz)) {
98+
c = Calendar.getInstance(tz);
99+
CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.set(c);
100+
}
101+
return c;
102+
}
103+
85104
/**
86105
* Convert the date to the specified time zone.
87106
*
@@ -394,7 +413,7 @@ private static long getTimeTry(boolean lenient, TimeZone tz,
394413
if (tz == null) {
395414
c = getCalendar();
396415
} else {
397-
c = Calendar.getInstance(tz);
416+
c = getCalendar(tz);
398417
}
399418
c.clear();
400419
c.setLenient(lenient);

0 commit comments

Comments
 (0)