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

Merge pull request #864 from katzyn/datetime

Minor changes in DateUtils and Function
......@@ -1326,7 +1326,7 @@ public class Function extends Expression implements FunctionCall {
if (s2 == null) {
s2 = "";
}
result = ValueString.get(replace(s0, s1, s2),
result = ValueString.get(StringUtils.replaceAll(s0, s1, s2),
database.getMode().treatEmptyStringsAsNull);
}
break;
......@@ -1471,7 +1471,7 @@ public class Function extends Expression implements FunctionCall {
v1 == null ? null : v1.getString()));
break;
case ADD_MONTHS:
result = ValueTimestamp.get(DateTimeUtils.addMonths(v0.getTimestamp(), v1.getInt()));
result = dateadd("MONTH", v1.getInt(), v0);
break;
case TRANSLATE: {
String matching = v1.getString();
......@@ -1980,13 +1980,6 @@ public class Function extends Expression implements FunctionCall {
return s.substring(start, start + length);
}
private static String replace(String s, String replace, String with) {
if (s == null || replace == null || with == null) {
return null;
}
return StringUtils.replaceAll(s, replace, with);
}
private static String repeat(String s, int count) {
StringBuilder buff = new StringBuilder(s.length() * count);
while (count-- > 0) {
......
......@@ -525,7 +525,7 @@ public class DateTimeUtils {
if (month == 2) {
maxDay = c.isLeapYear(year) ? 29 : 28;
} else {
maxDay = 30 + ((month + (month > 7 ? 1 : 0)) & 1);
maxDay = NORMAL_DAYS_PER_MONTH[month];
}
if (day < 1 || day > maxDay) {
throw e;
......@@ -1222,26 +1222,6 @@ public class DateTimeUtils {
return dateValue(y, m + 3, (int) d);
}
/**
* Adds the number of months to the date. If the resulting month's number of
* days is less than the original's day-of-month, the resulting
* day-of-months gets adjusted accordingly: <br>
* 30.04.2007 - 2 months = 28.02.2007
*
* @param refDate the original date
* @param nrOfMonthsToAdd the number of months to add
* @return the new timestamp
*/
public static Timestamp addMonths(Timestamp refDate, int nrOfMonthsToAdd) {
Calendar calendar = DateTimeUtils.createGregorianCalendar();
calendar.setTime(refDate);
calendar.add(Calendar.MONTH, nrOfMonthsToAdd);
Timestamp resultDate = new Timestamp(calendar.getTimeInMillis());
resultDate.setNanos(refDate.getNanos());
return resultDate;
}
/**
* Append a date to the string builder.
*
......
......@@ -82,7 +82,6 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testIfNull();
testToDate();
testToDateException();
testAddMonths();
testDataType();
testVersion();
testFunctionTable();
......@@ -1428,39 +1427,6 @@ public class TestFunctions extends TestBase implements AggregateFunction {
date.setTime(c.getTimeInMillis());
}
private void testAddMonths() throws ParseException {
Timestamp date;
Timestamp expected;
// 01-Aug-03 + 3 months = 01-Nov-03
date = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd").parse("2003-08-01").getTime());
expected = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd").parse("2003-11-01").getTime());
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 3));
// 31-Jan-03 + 1 month = 28-Feb-2003
date = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd").parse("2003-01-31").getTime());
expected = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd").parse("2003-02-28").getTime());
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 1));
// 21-Aug-2003 - 3 months = 21-May-2003
date = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd").parse("2003-08-21").getTime());
expected = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd").parse("2003-05-21").getTime());
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -3));
// 21-Aug-2003 00:00:00:333 - 3 months = 21-May-2003 00:00:00:333
date = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd SSS").parse("2003-08-21 333").getTime());
expected = new Timestamp(
new SimpleDateFormat("yyyy-MM-dd SSS").parse("2003-05-21 333").getTime());
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -3));
}
private void testToCharFromDateTime() throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
......
......@@ -135,7 +135,7 @@ public class TestScript extends TestBase {
"set", "table", "transaction-id", "truncate-value", "user" }) {
testScript("functions/system/" + s + ".sql");
}
for (String s : new String[] { "current_date", "current_timestamp",
for (String s : new String[] { "add_months", "current_date", "current_timestamp",
"current-time", "dateadd", "datediff", "dayname",
"day-of-month", "day-of-week", "day-of-year", "extract",
"formatdatetime", "hour", "minute", "month", "monthname",
......
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
-- 01-Aug-03 + 3 months = 01-Nov-03
SELECT ADD_MONTHS('2003-08-01', 3) AS R;
> R
> ---------------------
> 2003-11-01 00:00:00.0
> rows: 1
-- 31-Jan-03 + 1 month = 28-Feb-2003
SELECT ADD_MONTHS('2003-01-31', 1) AS R;
> R
> ---------------------
> 2003-02-28 00:00:00.0
> rows: 1
-- 21-Aug-2003 - 3 months = 21-May-2003
SELECT ADD_MONTHS('2003-08-21', -3) AS R;
> R
> ---------------------
> 2003-05-21 00:00:00.0
> rows: 1
-- 21-Aug-2003 00:00:00.333 - 3 months = 21-May-2003 00:00:00.333
SELECT ADD_MONTHS('2003-08-21 00:00:00.333', -3) AS R;
> R
> -----------------------
> 2003-05-21 00:00:00.333
> rows: 1
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论