Unverified 提交 b3f66155 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #880 from katzyn/datetime

Assorted date-time related changes
...@@ -224,7 +224,8 @@ class AggregateDataMedian extends AggregateData { ...@@ -224,7 +224,8 @@ class AggregateDataMedian extends AggregateData {
case Value.DOUBLE: case Value.DOUBLE:
return ValueDouble.get((v0.getFloat() + v1.getDouble()) / 2); return ValueDouble.get((v0.getFloat() + v1.getDouble()) / 2);
case Value.TIME: { case Value.TIME: {
return ValueTime.fromMillis((v0.getTime().getTime() + v1.getTime().getTime()) / 2); ValueTime t0 = (ValueTime) v0.convertTo(Value.TIME), t1 = (ValueTime) v1.convertTo(Value.TIME);
return ValueTime.fromNanos((t0.getNanos() + t1.getNanos()) / 2);
} }
case Value.DATE: { case Value.DATE: {
ValueDate d0 = (ValueDate) v0.convertTo(Value.DATE), d1 = (ValueDate) v1.convertTo(Value.DATE); ValueDate d0 = (ValueDate) v0.convertTo(Value.DATE), d1 = (ValueDate) v1.convertTo(Value.DATE);
......
...@@ -15,7 +15,7 @@ import java.nio.charset.StandardCharsets; ...@@ -15,7 +15,7 @@ import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.SimpleDateFormat; import java.text.DateFormatSymbols;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashMap; import java.util.HashMap;
...@@ -153,6 +153,11 @@ public class Function extends Expression implements FunctionCall { ...@@ -153,6 +153,11 @@ public class Function extends Expression implements FunctionCall {
private static final HashMap<String, Integer> DATE_PART = new HashMap<>(); private static final HashMap<String, Integer> DATE_PART = new HashMap<>();
private static final char[] SOUNDEX_INDEX = new char[128]; private static final char[] SOUNDEX_INDEX = new char[128];
/**
* English names of months and week days.
*/
private static volatile String[][] MONTHS_AND_WEEKS;
protected Expression[] args; protected Expression[] args;
private final FunctionInfo info; private final FunctionInfo info;
...@@ -844,9 +849,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -844,9 +849,8 @@ public class Function extends Expression implements FunctionCall {
database.getMode().treatEmptyStringsAsNull); database.getMode().treatEmptyStringsAsNull);
break; break;
case DAY_NAME: { case DAY_NAME: {
SimpleDateFormat dayName = new SimpleDateFormat( int dayOfWeek = DateTimeUtils.getSundayDayOfWeek(DateTimeUtils.dateAndTimeFromValue(v0)[0]);
"EEEE", Locale.ENGLISH); result = ValueString.get(getMonthsAndWeeks(1)[dayOfWeek],
result = ValueString.get(dayName.format(v0.getDate()),
database.getMode().treatEmptyStringsAsNull); database.getMode().treatEmptyStringsAsNull);
break; break;
} }
...@@ -866,9 +870,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -866,9 +870,8 @@ public class Function extends Expression implements FunctionCall {
result = ValueInt.get(getIntDatePart(v0, info.type)); result = ValueInt.get(getIntDatePart(v0, info.type));
break; break;
case MONTH_NAME: { case MONTH_NAME: {
SimpleDateFormat monthName = new SimpleDateFormat("MMMM", int month = DateTimeUtils.monthFromDateValue(DateTimeUtils.dateAndTimeFromValue(v0)[0]);
Locale.ENGLISH); result = ValueString.get(getMonthsAndWeeks(0)[month - 1],
result = ValueString.get(monthName.format(v0.getDate()),
database.getMode().treatEmptyStringsAsNull); database.getMode().treatEmptyStringsAsNull);
break; break;
} }
...@@ -2025,6 +2028,18 @@ public class Function extends Expression implements FunctionCall { ...@@ -2025,6 +2028,18 @@ public class Function extends Expression implements FunctionCall {
return r2 - r1; return r2 - r1;
} }
private static String[] getMonthsAndWeeks(int field) {
String[][] result = MONTHS_AND_WEEKS;
if (result == null) {
result = new String[2][];
DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.ENGLISH);
result[0] = dfs.getMonths();
result[1] = dfs.getWeekdays();
MONTHS_AND_WEEKS = result;
}
return result[field];
}
private static String substring(String s, int start, int length) { private static String substring(String s, int start, int length) {
int len = s.length(); int len = s.length();
start--; start--;
......
...@@ -628,20 +628,6 @@ public class DateTimeUtils { ...@@ -628,20 +628,6 @@ public class DateTimeUtils {
return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos); return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos);
} }
/**
* Get the year (positive or negative) from a calendar.
*
* @param calendar the calendar
* @return the year
*/
private static int getYear(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
year = 1 - year;
}
return year;
}
/** /**
* Get the number of milliseconds since 1970-01-01 in the local timezone, * Get the number of milliseconds since 1970-01-01 in the local timezone,
* but without daylight saving time into account. * but without daylight saving time into account.
...@@ -1069,10 +1055,12 @@ public class DateTimeUtils { ...@@ -1069,10 +1055,12 @@ public class DateTimeUtils {
* @return the date value * @return the date value
*/ */
private static long dateValueFromCalendar(Calendar cal) { private static long dateValueFromCalendar(Calendar cal) {
int year, month, day; int year = cal.get(Calendar.YEAR);
year = getYear(cal); if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {
month = cal.get(Calendar.MONTH) + 1; year = 1 - year;
day = cal.get(Calendar.DAY_OF_MONTH); }
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return ((long) year << SHIFT_YEAR) | (month << SHIFT_MONTH) | day; return ((long) year << SHIFT_YEAR) | (month << SHIFT_MONTH) | day;
} }
......
...@@ -26,6 +26,7 @@ public class TestClearReferences extends TestBase { ...@@ -26,6 +26,7 @@ public class TestClearReferences extends TestBase {
"org.h2.compress.CompressLZF.cachedHashTable", "org.h2.compress.CompressLZF.cachedHashTable",
"org.h2.engine.DbSettings.defaultSettings", "org.h2.engine.DbSettings.defaultSettings",
"org.h2.engine.SessionRemote.sessionFactory", "org.h2.engine.SessionRemote.sessionFactory",
"org.h2.expression.Function.MONTHS_AND_WEEKS",
"org.h2.jdbcx.JdbcDataSourceFactory.cachedTraceSystem", "org.h2.jdbcx.JdbcDataSourceFactory.cachedTraceSystem",
"org.h2.store.RecoverTester.instance", "org.h2.store.RecoverTester.instance",
"org.h2.store.fs.FilePath.defaultProvider", "org.h2.store.fs.FilePath.defaultProvider",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论