Author: shawkins
Date: 2011-11-28 15:04:04 -0500 (Mon, 28 Nov 2011)
New Revision: 3703
Modified:
trunk/common-core/src/main/java/org/teiid/core/util/TimestampWithTimezone.java
trunk/documentation/admin-guide/src/main/docbook/en-US/content/appendix-c.xml
trunk/engine/src/main/java/org/teiid/query/function/FunctionMethods.java
trunk/engine/src/test/java/org/teiid/query/function/TestFunctionMethods.java
Log:
TEIID-1841 adding system property to drive iso 8601 behavior
Modified: trunk/common-core/src/main/java/org/teiid/core/util/TimestampWithTimezone.java
===================================================================
---
trunk/common-core/src/main/java/org/teiid/core/util/TimestampWithTimezone.java 2011-11-28
18:50:08 UTC (rev 3702)
+++
trunk/common-core/src/main/java/org/teiid/core/util/TimestampWithTimezone.java 2011-11-28
20:04:04 UTC (rev 3703)
@@ -46,9 +46,12 @@
*/
public class TimestampWithTimezone {
+ public static final String ISO8601_WEEK_PROP = "org.teiid.iso8601Week";
//$NON-NLS-1$
+ public static boolean ISO8601_WEEK =
PropertiesUtils.getBooleanProperty(System.getProperties(), ISO8601_WEEK_PROP, false);
+
private static ThreadLocal<Calendar> CALENDAR = new ThreadLocal<Calendar>()
{
protected Calendar initialValue() {
- return Calendar.getInstance();
+ return initialCalendar();
}
};
@@ -58,8 +61,17 @@
public static void resetCalendar(TimeZone tz) {
TimeZone.setDefault(tz);
- CALENDAR.set(Calendar.getInstance());
+ CALENDAR.set(initialCalendar());
}
+
+ static Calendar initialCalendar() {
+ Calendar result = Calendar.getInstance();
+ if (ISO8601_WEEK) {
+ result.setMinimalDaysInFirstWeek(4);
+ result.setFirstDayOfWeek(Calendar.MONDAY);
+ }
+ return result;
+ }
public static Object create(java.util.Date date, TimeZone initial, Calendar target,
Class<?> type) {
if (type.equals(DataTypeManager.DefaultDataClasses.TIME)) {
Modified: trunk/documentation/admin-guide/src/main/docbook/en-US/content/appendix-c.xml
===================================================================
---
trunk/documentation/admin-guide/src/main/docbook/en-US/content/appendix-c.xml 2011-11-28
18:50:08 UTC (rev 3702)
+++
trunk/documentation/admin-guide/src/main/docbook/en-US/content/appendix-c.xml 2011-11-28
20:04:04 UTC (rev 3703)
@@ -35,5 +35,10 @@
Target size in bytes of the ODBC results buffer. This is not a hard maximum, lobs and
wide rows may use larger buffers.
</para>
</listitem>
+ <listitem>
+ <para><emphasis>org.teiid.iso8601Week</emphasis> - defaults to
false.
+ Set to true to use ISO 8601 rules for week calculations regardless of the locale.
When true the dayOfWeek function will begin with 1 for MONDAY rather than SUNDAY, and the
week function will require that week 1 of a year contains the year's first Thursday.
+ </para>
+ </listitem>
</itemizedlist>
</appendix>
\ No newline at end of file
Modified: trunk/engine/src/main/java/org/teiid/query/function/FunctionMethods.java
===================================================================
--- trunk/engine/src/main/java/org/teiid/query/function/FunctionMethods.java 2011-11-28
18:50:08 UTC (rev 3702)
+++ trunk/engine/src/main/java/org/teiid/query/function/FunctionMethods.java 2011-11-28
20:04:04 UTC (rev 3703)
@@ -390,8 +390,16 @@
// ================== Function = dayofweek =====================
- public static Object dayOfWeek(Date x) {
- return Integer.valueOf(getField(x, Calendar.DAY_OF_WEEK));
+ public static int dayOfWeek(Date x) {
+ int result = getField(x, Calendar.DAY_OF_WEEK);
+ if (TimestampWithTimezone.ISO8601_WEEK) {
+ result -= 1;
+ if (result == 0) {
+ return 7;
+ }
+ return result;
+ }
+ return getField(x, Calendar.DAY_OF_WEEK);
}
// ================== Function = dayofyear =====================
@@ -436,8 +444,8 @@
// ================== Function = week =====================
- public static Object week(Date x) {
- return Integer.valueOf(getField(x, Calendar.WEEK_OF_YEAR));
+ public static int week(Date x) {
+ return getField(x, Calendar.WEEK_OF_YEAR);
}
// ================== Function = year =====================
Modified: trunk/engine/src/test/java/org/teiid/query/function/TestFunctionMethods.java
===================================================================
---
trunk/engine/src/test/java/org/teiid/query/function/TestFunctionMethods.java 2011-11-28
18:50:08 UTC (rev 3702)
+++
trunk/engine/src/test/java/org/teiid/query/function/TestFunctionMethods.java 2011-11-28
20:04:04 UTC (rev 3703)
@@ -24,11 +24,25 @@
import static org.junit.Assert.*;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.teiid.core.util.TimestampWithTimezone;
+import org.teiid.query.unittest.TimestampUtil;
@SuppressWarnings("nls")
public class TestFunctionMethods {
+ @BeforeClass public static void oneTimeSetup() {
+ TimestampWithTimezone.ISO8601_WEEK = true;
+ TimestampWithTimezone.resetCalendar(null);
+ }
+
+ @AfterClass public static void oneTimeTearDown() {
+ TimestampWithTimezone.ISO8601_WEEK = false;
+ TimestampWithTimezone.resetCalendar(null);
+ }
+
@Test public void testUnescape() {
assertEquals("a\t\n\n%6",
FunctionMethods.unescape("a\\t\\n\\012\\456"));
}
@@ -36,5 +50,18 @@
@Test public void testUnescape1() {
assertEquals("a\u45AA'",
FunctionMethods.unescape("a\\u45Aa\'"));
}
+
+ @Test public void testIso8601Week() {
+ assertEquals(53, FunctionMethods.week(TimestampUtil.createDate(105, 0, 1)));
+ }
+
+ @Test public void testIso8601Week1() {
+ assertEquals(52, FunctionMethods.week(TimestampUtil.createDate(106, 0, 1)));
+ }
+
+ @Test public void testIso8601Week2() {
+ assertEquals(1, FunctionMethods.dayOfWeek(TimestampUtil.createDate(111, 10, 28)));
+ }
+
}
Show replies by date