提交 5f196a7e authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Pass Value directly to DateTimeUtils.getIso*()

上级 07e507f3
...@@ -884,13 +884,13 @@ public class Function extends Expression implements FunctionCall { ...@@ -884,13 +884,13 @@ public class Function extends Expression implements FunctionCall {
Calendar.YEAR)); Calendar.YEAR));
break; break;
case ISO_YEAR: case ISO_YEAR:
result = ValueInt.get(DateTimeUtils.getIsoYear(v0.getDate())); result = ValueInt.get(DateTimeUtils.getIsoYear(v0));
break; break;
case ISO_WEEK: case ISO_WEEK:
result = ValueInt.get(DateTimeUtils.getIsoWeek(v0.getDate())); result = ValueInt.get(DateTimeUtils.getIsoWeek(v0));
break; break;
case ISO_DAY_OF_WEEK: case ISO_DAY_OF_WEEK:
result = ValueInt.get(DateTimeUtils.getIsoDayOfWeek(v0.getDate())); result = ValueInt.get(DateTimeUtils.getIsoDayOfWeek(v0));
break; break;
case CURDATE: case CURDATE:
case CURRENT_DATE: { case CURRENT_DATE: {
......
...@@ -552,10 +552,11 @@ public class DateTimeUtils { ...@@ -552,10 +552,11 @@ public class DateTimeUtils {
* starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601 * starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601
* *
* @author Robert Rathsack * @author Robert Rathsack
* @param date the date object which day of week should be calculated * @param value the date object which day of week should be calculated
* @return the day of the week, Monday as 1 to Sunday as 7 * @return the day of the week, Monday as 1 to Sunday as 7
*/ */
public static int getIsoDayOfWeek(java.util.Date date) { public static int getIsoDayOfWeek(Value value) {
java.util.Date date = value.getDate();
Calendar cal = DateTimeUtils.createGregorianCalendar(); Calendar cal = DateTimeUtils.createGregorianCalendar();
cal.setTimeInMillis(date.getTime()); cal.setTimeInMillis(date.getTime());
int val = cal.get(Calendar.DAY_OF_WEEK) - 1; int val = cal.get(Calendar.DAY_OF_WEEK) - 1;
...@@ -573,10 +574,11 @@ public class DateTimeUtils { ...@@ -573,10 +574,11 @@ public class DateTimeUtils {
* the December 28th always belongs to the last week. * the December 28th always belongs to the last week.
* *
* @author Robert Rathsack * @author Robert Rathsack
* @param date the date object which week of year should be calculated * @param value the date object which week of year should be calculated
* @return the week of the year * @return the week of the year
*/ */
public static int getIsoWeek(java.util.Date date) { public static int getIsoWeek(Value value) {
java.util.Date date = value.getDate();
Calendar c = DateTimeUtils.createGregorianCalendar(); Calendar c = DateTimeUtils.createGregorianCalendar();
c.setTimeInMillis(date.getTime()); c.setTimeInMillis(date.getTime());
c.setFirstDayOfWeek(Calendar.MONDAY); c.setFirstDayOfWeek(Calendar.MONDAY);
...@@ -588,10 +590,11 @@ public class DateTimeUtils { ...@@ -588,10 +590,11 @@ public class DateTimeUtils {
* Returns the year according to the ISO week definition. * Returns the year according to the ISO week definition.
* *
* @author Robert Rathsack * @author Robert Rathsack
* @param date the date object which year should be calculated * @param value the date object which year should be calculated
* @return the year * @return the year
*/ */
public static int getIsoYear(java.util.Date date) { public static int getIsoYear(Value value) {
java.util.Date date = value.getDate();
Calendar cal = DateTimeUtils.createGregorianCalendar(); Calendar cal = DateTimeUtils.createGregorianCalendar();
cal.setTimeInMillis(date.getTime()); cal.setTimeInMillis(date.getTime());
cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setFirstDayOfWeek(Calendar.MONDAY);
......
...@@ -5,22 +5,18 @@ ...@@ -5,22 +5,18 @@
*/ */
package org.h2.test.unit; package org.h2.test.unit;
import java.sql.Timestamp; import static org.h2.util.DateTimeUtils.getIsoDayOfWeek;
import java.text.ParseException; import static org.h2.util.DateTimeUtils.getIsoWeek;
import java.text.SimpleDateFormat; import static org.h2.util.DateTimeUtils.getIsoYear;
import java.util.Date; import static org.h2.value.ValueDate.parse;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.DateTimeUtils;
/** /**
* Test cases for DateTimeIso8601Utils. * Test cases for DateTimeIso8601Utils.
*/ */
public class TestDateIso8601 extends TestBase { public class TestDateIso8601 extends TestBase {
private final SimpleDateFormat dateFormatter =
new SimpleDateFormat("yyyy-MM-dd");
/** /**
* Run just this test. * Run just this test.
* *
...@@ -49,14 +45,6 @@ public class TestDateIso8601 extends TestBase { ...@@ -49,14 +45,6 @@ public class TestDateIso8601 extends TestBase {
testIsoYearJanuary1thSunday(); testIsoYearJanuary1thSunday();
} }
private Date parse(String s) throws ParseException {
return dateFormatter.parse(s);
}
private static int getIsoDayOfWeek(Date date) {
return DateTimeUtils.getIsoDayOfWeek(date);
}
/** /**
* Test if day of week is returned as Monday = 1 to Sunday = 7. * Test if day of week is returned as Monday = 1 to Sunday = 7.
*/ */
...@@ -70,11 +58,6 @@ public class TestDateIso8601 extends TestBase { ...@@ -70,11 +58,6 @@ public class TestDateIso8601 extends TestBase {
assertEquals(7, getIsoDayOfWeek(parse("2008-10-05"))); assertEquals(7, getIsoDayOfWeek(parse("2008-10-05")));
} }
private static int getIsoWeek(Date date) {
Timestamp ts = new Timestamp(date.getTime());
return DateTimeUtils.getIsoWeek(ts);
}
/** /**
* January 1st is a Monday therefore the week belongs to the next year. * January 1st is a Monday therefore the week belongs to the next year.
*/ */
...@@ -154,11 +137,6 @@ public class TestDateIso8601 extends TestBase { ...@@ -154,11 +137,6 @@ public class TestDateIso8601 extends TestBase {
assertEquals(2, getIsoWeek(parse("2012-01-09"))); assertEquals(2, getIsoWeek(parse("2012-01-09")));
} }
private static int getIsoYear(Date date) {
Timestamp ts = new Timestamp(date.getTime());
return DateTimeUtils.getIsoYear(ts);
}
/** /**
* January 1st is a Monday therefore year is equal to isoYear. * January 1st is a Monday therefore year is equal to isoYear.
*/ */
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论