提交 25e6a14d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use CURRENT_TIMESTAMP in ToDateParser

上级 c85578fc
...@@ -1433,19 +1433,16 @@ public class Function extends Expression implements FunctionCall { ...@@ -1433,19 +1433,16 @@ public class Function extends Expression implements FunctionCall {
} }
break; break;
case TO_DATE: case TO_DATE:
result = ToDateParser.toDate(v0.getString(), result = ToDateParser.toDate(session, v0.getString(), v1 == null ? null : v1.getString());
v1 == null ? null : v1.getString());
break; break;
case TO_TIMESTAMP: case TO_TIMESTAMP:
result = ToDateParser.toTimestamp(v0.getString(), result = ToDateParser.toTimestamp(session, v0.getString(), v1 == null ? null : v1.getString());
v1 == null ? null : v1.getString());
break; break;
case ADD_MONTHS: case ADD_MONTHS:
result = DateTimeFunctions.dateadd("MONTH", v1.getInt(), v0); result = DateTimeFunctions.dateadd("MONTH", v1.getInt(), v0);
break; break;
case TO_TIMESTAMP_TZ: case TO_TIMESTAMP_TZ:
result = ToDateParser.toTimestampTz(v0.getString(), result = ToDateParser.toTimestampTz(session, v0.getString(), v1 == null ? null : v1.getString());
v1 == null ? null : v1.getString());
break; break;
case TRANSLATE: { case TRANSLATE: {
String matching = v1.getString(); String matching = v1.getString();
......
...@@ -7,11 +7,10 @@ package org.h2.expression.function; ...@@ -7,11 +7,10 @@ package org.h2.expression.function;
import static java.lang.String.format; import static java.lang.String.format;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import org.h2.engine.Session;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.value.ValueTimestamp; import org.h2.value.ValueTimestamp;
import org.h2.value.ValueTimestampTimeZone; import org.h2.value.ValueTimestampTimeZone;
...@@ -21,6 +20,8 @@ import org.h2.value.ValueTimestampTimeZone; ...@@ -21,6 +20,8 @@ import org.h2.value.ValueTimestampTimeZone;
* This class holds and handles the input data form the TO_DATE-method * This class holds and handles the input data form the TO_DATE-method
*/ */
public class ToDateParser { public class ToDateParser {
private final Session session;
private final String unmodifiedInputStr; private final String unmodifiedInputStr;
private final String unmodifiedFormatStr; private final String unmodifiedFormatStr;
private final ConfigParam functionName; private final ConfigParam functionName;
...@@ -52,12 +53,14 @@ public class ToDateParser { ...@@ -52,12 +53,14 @@ public class ToDateParser {
private int currentYear, currentMonth; private int currentYear, currentMonth;
/** /**
* @param input the input date with the date-time info * @param session the database session
* @param format the format of date-time info
* @param functionName one of [TO_DATE, TO_TIMESTAMP] (both share the same * @param functionName one of [TO_DATE, TO_TIMESTAMP] (both share the same
* code) * code)
* @param input the input date with the date-time info
* @param format the format of date-time info
*/ */
private ToDateParser(ConfigParam functionName, String input, String format) { private ToDateParser(Session session, ConfigParam functionName, String input, String format) {
this.session = session;
this.functionName = functionName; this.functionName = functionName;
inputStr = input.trim(); inputStr = input.trim();
// Keep a copy // Keep a copy
...@@ -72,8 +75,8 @@ public class ToDateParser { ...@@ -72,8 +75,8 @@ public class ToDateParser {
unmodifiedFormatStr = formatStr; unmodifiedFormatStr = formatStr;
} }
private static ToDateParser getTimestampParser(ConfigParam param, String input, String format) { private static ToDateParser getTimestampParser(Session session, ConfigParam param, String input, String format) {
ToDateParser result = new ToDateParser(param, input, format); ToDateParser result = new ToDateParser(session, param, input, format);
parse(result); parse(result);
return result; return result;
} }
...@@ -145,10 +148,10 @@ public class ToDateParser { ...@@ -145,10 +148,10 @@ public class ToDateParser {
} }
private void queryCurrentYearAndMonth() { private void queryCurrentYearAndMonth() {
GregorianCalendar gc = DateTimeUtils.getCalendar(); long dateValue = (session.getDatabase().getMode().dateTimeValueWithinTransaction
gc.setTimeInMillis(System.currentTimeMillis()); ? session.getTransactionStart() : session.getCurrentCommandStart()).getDateValue();
currentYear = gc.get(Calendar.YEAR); currentYear = DateTimeUtils.yearFromDateValue(dateValue);
currentMonth = gc.get(Calendar.MONTH) + 1; currentMonth = DateTimeUtils.monthFromDateValue(dateValue);
} }
int getCurrentYear() { int getCurrentYear() {
...@@ -319,36 +322,39 @@ public class ToDateParser { ...@@ -319,36 +322,39 @@ public class ToDateParser {
/** /**
* Parse a string as a timestamp with the given format. * Parse a string as a timestamp with the given format.
* *
* @param session the database session
* @param input the input * @param input the input
* @param format the format * @param format the format
* @return the timestamp * @return the timestamp
*/ */
public static ValueTimestamp toTimestamp(String input, String format) { public static ValueTimestamp toTimestamp(Session session, String input, String format) {
ToDateParser parser = getTimestampParser(ConfigParam.TO_TIMESTAMP, input, format); ToDateParser parser = getTimestampParser(session, ConfigParam.TO_TIMESTAMP, input, format);
return parser.getResultingValue(); return parser.getResultingValue();
} }
/** /**
* Parse a string as a timestamp with the given format. * Parse a string as a timestamp with the given format.
* *
* @param session the database session
* @param input the input * @param input the input
* @param format the format * @param format the format
* @return the timestamp * @return the timestamp
*/ */
public static ValueTimestampTimeZone toTimestampTz(String input, String format) { public static ValueTimestampTimeZone toTimestampTz(Session session, String input, String format) {
ToDateParser parser = getTimestampParser(ConfigParam.TO_TIMESTAMP_TZ, input, format); ToDateParser parser = getTimestampParser(session, ConfigParam.TO_TIMESTAMP_TZ, input, format);
return parser.getResultingValueWithTimeZone(); return parser.getResultingValueWithTimeZone();
} }
/** /**
* Parse a string as a date with the given format. * Parse a string as a date with the given format.
* *
* @param session the database session
* @param input the input * @param input the input
* @param format the format * @param format the format
* @return the date as a timestamp * @return the date as a timestamp
*/ */
public static ValueTimestamp toDate(String input, String format) { public static ValueTimestamp toDate(Session session, String input, String format) {
ToDateParser parser = getTimestampParser(ConfigParam.TO_DATE, input, format); ToDateParser parser = getTimestampParser(session, ConfigParam.TO_DATE, input, format);
return parser.getResultingValue(); return parser.getResultingValue();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论