You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by ji...@apache.org on 2014/05/22 03:33:37 UTC
[1/6] TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
Repository: tajo
Updated Branches:
refs/heads/master 98f142cc7 -> 526dca28b
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestIntervalType.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestIntervalType.java b/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestIntervalType.java
index 4d18be3..6450028 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestIntervalType.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestIntervalType.java
@@ -28,24 +28,32 @@ import static org.junit.Assert.fail;
public class TestIntervalType extends ExprTestBase {
@Test
public void testIntervalPostgresqlCase() throws IOException {
+
// http://www.postgresql.org/docs/8.2/static/functions-datetime.html
testSimpleEval("select date '2001-09-28' + 7", new String[]{"2001-10-05"});
- testSimpleEval("select date '2001-09-28' + interval '1 hour'", new String[]{"2001-09-28 01:00:00"});
+ testSimpleEval("select date '2001-09-28' + interval '1 hour'",
+ new String[]{"2001-09-28 01:00:00" + getUserTimeZoneDisplay()});
- testSimpleEval("select date '2001-09-28' + time '03:00'", new String[]{"2001-09-28 03:00:00"});
+ testSimpleEval("select date '2001-09-28' + time '03:00'",
+ new String[]{"2001-09-28 03:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select time '03:00' + date '2001-09-28'",
+ new String[]{"2001-09-28 03:00:00" + getUserTimeZoneDisplay()});
testSimpleEval("select interval '1 day' + interval '1 hour'", new String[]{"1 day 01:00:00"});
- testSimpleEval("select timestamp '2001-09-28 01:00' + interval '23 hours'", new String[]{"2001-09-29 00:00:00"});
+ testSimpleEval("select timestamp '2001-09-28 01:00' + interval '23 hours'",
+ new String[]{"2001-09-29 00:00:00" + getUserTimeZoneDisplay()});
- testSimpleEval("select time '01:00' + interval '3 hours'", new String[]{"04:00:00"});
+ testSimpleEval("select time '01:00' + interval '3 hours'", new String[]{"04:00:00" + getUserTimeZoneDisplay()});
testSimpleEval("select date '2001-10-01' - date '2001-09-28'", new String[]{"3"});
testSimpleEval("select date '2001-10-01' - 7", new String[]{"2001-09-24"});
- testSimpleEval("select date '2001-09-28' - interval '1 hour'", new String[]{"2001-09-27 23:00:00"});
+ testSimpleEval("select date '2001-09-28' - interval '1 hour'",
+ new String[]{"2001-09-27 23:00:00" + getUserTimeZoneDisplay()});
testSimpleEval("select time '05:00' - time '03:00'", new String[]{"02:00:00"});
- testSimpleEval("select time '05:00' - interval '2 hours'", new String[]{"03:00:00"});
- testSimpleEval("select timestamp '2001-09-28 23:00' - interval '23 hours'", new String[]{"2001-09-28 00:00:00"});
+ testSimpleEval("select time '05:00' - interval '2 hours'", new String[]{"03:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select timestamp '2001-09-28 23:00' - interval '23 hours'",
+ new String[]{"2001-09-28 00:00:00" + getUserTimeZoneDisplay()});
testSimpleEval("select interval '1 day' - interval '1 hour'", new String[]{"23:00:00"});
@@ -58,21 +66,33 @@ public class TestIntervalType extends ExprTestBase {
@Test
public void testCaseByCase() throws Exception {
- testSimpleEval("select date '2001-08-28' + interval '10 day 1 hour'", new String[]{"2001-09-07 01:00:00"});
- testSimpleEval("select interval '10 day 01:00:00' + date '2001-08-28'", new String[]{"2001-09-07 01:00:00"});
- testSimpleEval("select time '10:20:30' + interval '1 day 01:00:00'", new String[]{"11:20:30"});
- testSimpleEval("select interval '1 day 01:00:00' + time '10:20:30'", new String[]{"11:20:30"});
- testSimpleEval("select time '10:20:30' - interval '1 day 01:00:00'", new String[]{"09:20:30"});
+ testSimpleEval("select date '2001-08-28' + interval '10 day 1 hour'",
+ new String[]{"2001-09-07 01:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select interval '10 day 01:00:00' + date '2001-08-28'",
+ new String[]{"2001-09-07 01:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select time '10:20:30' + interval '1 day 01:00:00'",
+ new String[]{"11:20:30" + getUserTimeZoneDisplay()});
+ testSimpleEval("select interval '1 day 01:00:00' + time '10:20:30'",
+ new String[]{"11:20:30" + getUserTimeZoneDisplay()});
+ testSimpleEval("select time '10:20:30' - interval '1 day 01:00:00'",
+ new String[]{"09:20:30" + getUserTimeZoneDisplay()});
testSimpleEval("select (interval '1 month 20 day' + interval '50 day')", new String[]{"1 month 70 days"});
- testSimpleEval("select date '2013-01-01' + interval '1 month 70 day'", new String[]{"2013-04-12 00:00:00"});
- testSimpleEval("select date '2013-01-01' + (interval '1 month 20 day' + interval '50 day')", new String[]{"2013-04-12 00:00:00"});
- testSimpleEval("select interval '1 month 70 day' + date '2013-01-01'", new String[]{"2013-04-12 00:00:00"});
- testSimpleEval("select date '2013-01-01' - interval '1 month 70 day'", new String[]{"2012-09-22 00:00:00"});
-
- testSimpleEval("select timestamp '2001-09-28 23:00' - interval '1 month 2 day 10:20:30'", new String[]{"2001-08-26 12:39:30"});
- testSimpleEval("select timestamp '2001-09-28 23:00' + interval '1 month 2 day 10:20:30'", new String[]{"2001-10-31 09:20:30"});
- testSimpleEval("select interval '1 month 2 day 10:20:30' + timestamp '2001-09-28 23:00'", new String[]{"2001-10-31 09:20:30"});
+ testSimpleEval("select date '2013-01-01' + interval '1 month 70 day'",
+ new String[]{"2013-04-12 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select date '2013-01-01' + (interval '1 month 20 day' + interval '50 day')",
+ new String[]{"2013-04-12 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select interval '1 month 70 day' + date '2013-01-01'",
+ new String[]{"2013-04-12 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select date '2013-01-01' - interval '1 month 70 day'",
+ new String[]{"2012-09-22 00:00:00" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("select timestamp '2001-09-28 23:00' - interval '1 month 2 day 10:20:30'",
+ new String[]{"2001-08-26 12:39:30" + getUserTimeZoneDisplay()});
+ testSimpleEval("select timestamp '2001-09-28 23:00' + interval '1 month 2 day 10:20:30'",
+ new String[]{"2001-10-31 09:20:30" + getUserTimeZoneDisplay()});
+ testSimpleEval("select interval '1 month 2 day 10:20:30' + timestamp '2001-09-28 23:00'",
+ new String[]{"2001-10-31 09:20:30" + getUserTimeZoneDisplay()});
testSimpleEval("select interval '5 month' / 3", new String[]{"1 month 20 days"});
@@ -82,6 +102,8 @@ public class TestIntervalType extends ExprTestBase {
testSimpleEval("select interval '1 month' * 2.3", new String[]{"2 months 9 days"});
testSimpleEval("select interval '3 year 5 month 1 hour' / 1.5", new String[]{"2 years 3 months 10 days 00:40:00"});
+
+ testSimpleEval("select date '2001-09-28' - time '03:00'", new String[]{"2001-09-27 21:00:00" + getUserTimeZoneDisplay()});
}
@Test
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLDateTimeTypes.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLDateTimeTypes.java b/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLDateTimeTypes.java
index c9c8dd4..60f7dcd 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLDateTimeTypes.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLDateTimeTypes.java
@@ -26,22 +26,26 @@ public class TestSQLDateTimeTypes extends ExprTestBase {
@Test
public void testTimestamp() throws IOException {
- testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37';", new String[]{"1970-01-17 10:09:37"});
- testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37.5';", new String[]{"1970-01-17 10:09:37.5"});
- testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37.01';", new String[]{"1970-01-17 10:09:37.01"});
- testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37.003';", new String[]{"1970-01-17 10:09:37.003"});
+ testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37';",
+ new String[]{"1970-01-17 10:09:37" + getUserTimeZoneDisplay()});
+ testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37.5';",
+ new String[]{"1970-01-17 10:09:37.5" + getUserTimeZoneDisplay()});
+ testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37.01';",
+ new String[]{"1970-01-17 10:09:37.01" + getUserTimeZoneDisplay()});
+ testSimpleEval("select TIMESTAMP '1970-01-17 10:09:37.003';",
+ new String[]{"1970-01-17 10:09:37.003" + getUserTimeZoneDisplay()});
}
@Test
public void testToTimestamp() throws IOException {
- testSimpleEval("select to_char(TIMESTAMP '1970-01-17 10:09:37', 'yyyy-MM-dd HH:mm:ss');",
+ testSimpleEval("select to_char(TIMESTAMP '1970-01-17 10:09:37', 'YYYY-MM-DD HH24:MI:SS');",
new String[]{"1970-01-17 10:09:37"});
}
@Test
public void testTimeLiteral() throws IOException {
testSimpleEval("select TIME '10:09:37';",
- new String[]{"10:09:37"});
+ new String[]{"10:09:37" + getUserTimeZoneDisplay()});
}
@Test
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLExpression.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLExpression.java b/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLExpression.java
index 6e35d3b..9bd26b8 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLExpression.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/eval/TestSQLExpression.java
@@ -22,12 +22,15 @@ import org.apache.tajo.catalog.CatalogUtil;
import org.apache.tajo.catalog.Schema;
import org.apache.tajo.catalog.exception.NoSuchFunctionException;
import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.conf.TajoConf;
+import org.apache.tajo.datum.DatumFactory;
import org.apache.tajo.datum.TimestampDatum;
import org.junit.Test;
import java.io.IOException;
import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+import static org.junit.Assert.fail;
public class TestSQLExpression extends ExprTestBase {
@@ -43,9 +46,16 @@ public class TestSQLExpression extends ExprTestBase {
new String[]{"3", "234.0"});
}
- @Test(expected = NoSuchFunctionException.class)
+ @Test
public void testNoSuchFunction() throws IOException {
- testSimpleEval("select test123('abc') col1 ", new String[]{"abc"});
+ try {
+ testSimpleEval("select test123('abc') col1 ", new String[]{"abc"});
+ fail("This test should throw NoSuchFunctionException");
+ } catch (NoSuchFunctionException e) {
+ //success
+ } catch (Exception e) {
+ fail("This test should throw NoSuchFunctionException: " + e);
+ }
}
@Test
@@ -841,10 +851,10 @@ public class TestSQLExpression extends ExprTestBase {
@Test
public void testCastWithNestedFunction() throws IOException {
- int timestamp = (int) (System.currentTimeMillis() / 1000);
- TimestampDatum expected = new TimestampDatum(timestamp);
- testSimpleEval(String.format("select to_timestamp(CAST(split_part('%d.999', '.', 1) as INT8));", timestamp),
- new String[] {expected.asChars()});
+ int unixtime = 1389071574;//(int) (System.currentTimeMillis() / 1000);
+ TimestampDatum expected = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
+ testSimpleEval(String.format("select to_timestamp(CAST(split_part('%d.999', '.', 1) as INT8));", unixtime),
+ new String[] {expected.asChars(TajoConf.getCurrentTimeZone(), true)});
}
@Test
@@ -856,11 +866,13 @@ public class TestSQLExpression extends ExprTestBase {
new String[]{"123.0", "234.0"});
testEval(schema, "table1", "123,234", "select col1::float, col2::float from table1",
new String[]{"123.0", "234.0"});
+
+ TimestampDatum timestamp = DatumFactory.createTimestamp("1980-04-01 01:50:01+09");
testEval(schema, "table1", "1980-04-01 01:50:01,234", "select col1::timestamp as t1, col2::float from table1 " +
"where t1 = '1980-04-01 01:50:01'::timestamp",
- new String[]{"1980-04-01 01:50:01", "234.0"});
+ new String[]{timestamp.asChars(TajoConf.getCurrentTimeZone(), true), "234.0"});
- testSimpleEval("select '1980-04-01 01:50:01'::timestamp;", new String [] {"1980-04-01 01:50:01"});
+ testSimpleEval("select '1980-04-01 01:50:01'::timestamp;", new String [] {timestamp.asChars(TajoConf.getCurrentTimeZone(), true)});
testSimpleEval("select '1980-04-01 01:50:01'::timestamp::text", new String [] {"1980-04-01 01:50:01"});
testSimpleEval("select (cast ('99999'::int8 as text))::int4 + 1", new String [] {"100000"});
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
index 507ef61..9639bb6 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestDateTimeFunctions.java
@@ -20,6 +20,8 @@ package org.apache.tajo.engine.function;
import org.apache.tajo.catalog.Schema;
+import org.apache.tajo.conf.TajoConf;
+import org.apache.tajo.datum.DatumFactory;
import org.apache.tajo.datum.TimestampDatum;
import org.apache.tajo.engine.eval.ExprTestBase;
import org.joda.time.DateTime;
@@ -30,15 +32,65 @@ import java.io.IOException;
import static org.apache.tajo.common.TajoDataTypes.Type.*;
public class TestDateTimeFunctions extends ExprTestBase {
-
@Test
public void testToTimestamp() throws IOException {
long expectedTimestamp = System.currentTimeMillis();
- DateTime expectedDateTime = new DateTime(expectedTimestamp);
+ TimestampDatum expected = DatumFactory.createTimestmpDatumWithUnixTime((int)(expectedTimestamp/ 1000));
// (expectedTimestamp / 1000) means the translation from millis seconds to unix timestamp
String q1 = String.format("select to_timestamp(%d);", (expectedTimestamp / 1000));
- testSimpleEval(q1, new String[]{expectedDateTime.toString(TimestampDatum.DEFAULT_FORMAT_STRING)});
+ testSimpleEval(q1, new String[]{expected.toString(TajoConf.getCurrentTimeZone(), true)});
+
+ testSimpleEval("select to_timestamp('1997-12-30 11:40:50.345', 'YYYY-MM-DD HH24:MI:SS.MS');",
+ new String[]{"1997-12-30 11:40:50.345" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('1997-12-30 11:40:50.345 PM', 'YYYY-MM-DD HH24:MI:SS.MS PM');",
+ new String[]{"1997-12-30 23:40:50.345" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');",
+ new String[]{"0097-02-16 08:14:30" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');",
+ new String[]{"0097-02-16 08:14:30" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('1985 September 12', 'YYYY FMMonth DD');",
+ new String[]{"1985-09-12 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');",
+ new String[]{"1582-08-21 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');",
+ new String[]{"2000-05-12 14:45:48" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');",
+ new String[]{"2000-01-09 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('97/Feb/16', 'YY/Mon/DD');",
+ new String[]{"1997-02-16 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('19971116', 'YYYYMMDD');",
+ new String[]{"1997-11-16 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('20000-1116', 'YYYY-MMDD');",
+ new String[]{"20000-11-16 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('9-1116', 'Y-MMDD');",
+ new String[]{"2009-11-16 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('95-1116', 'YY-MMDD');",
+ new String[]{"1995-11-16 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('995-1116', 'YYY-MMDD');",
+ new String[]{"1995-11-16 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('2005426', 'YYYYWWD');",
+ new String[]{"2005-10-15 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('2005300', 'YYYYDDD');",
+ new String[]{"2005-10-27 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('2005527', 'IYYYIWID');",
+ new String[]{"2006-01-01 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('005527', 'IYYIWID');",
+ new String[]{"2006-01-01 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('05527', 'IYIWID');",
+ new String[]{"2006-01-01 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('5527', 'IIWID');",
+ new String[]{"2006-01-01 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('2005364', 'IYYYIDDD');",
+ new String[]{"2006-01-01 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('20050302', 'YYYYMMDD');",
+ new String[]{"2005-03-02 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp('2005 03 02', 'YYYYMMDD');",
+ new String[]{"2005-03-02 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp(' 2005 03 02', 'YYYYMMDD');",
+ new String[]{"2005-03-02 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("select to_timestamp(' 20050302', 'YYYYMMDD');",
+ new String[]{"2005-03-02 00:00:00" + getUserTimeZoneDisplay()});
}
@Test
@@ -56,14 +108,15 @@ public class TestDateTimeFunctions extends ExprTestBase {
Schema schema2 = new Schema();
schema2.addColumn("col1", TIMESTAMP);
testEval(schema2, "table1",
- "1970-01-17 10:09:37",
+ "1970-01-17 10:09:37" + getUserTimeZoneDisplay(),
"select extract(year from col1), extract(month from col1), extract(day from col1) from table1;",
new String[]{"1970.0", "1.0", "17.0"});
+ // Currently TIME type can be loaded with INT8 type.
Schema schema3 = new Schema();
schema3.addColumn("col1", TIME);
testEval(schema3, "table1",
- "10:09:37.5",
+ "10:09:37.5" + getUserTimeZoneDisplay(),
"select extract(hour from col1), extract(minute from col1), extract(second from col1) from table1;",
new String[]{"10.0", "9.0", "37.5"});
@@ -74,9 +127,9 @@ public class TestDateTimeFunctions extends ExprTestBase {
"select extract(year from col1), extract(month from col1), extract(day from col1) from table1;",
new String[]{"1970.0", "1.0", "17.0"});
- testSimpleEval("select extract(century from TIMESTAMP '1970-01-17 10:09:37');", new String[]{"19.0"});
+ testSimpleEval("select extract(century from TIMESTAMP '1970-01-17 10:09:37');", new String[]{"20.0"});
- testSimpleEval("select extract(century from DATE '1970-01-17');", new String[]{"19.0"});
+ testSimpleEval("select extract(century from DATE '1970-01-17');", new String[]{"20.0"});
testSimpleEval("select extract(decade from TIMESTAMP '1970-01-17 10:09:37');", new String[]{"197.0"});
@@ -152,13 +205,13 @@ public class TestDateTimeFunctions extends ExprTestBase {
Schema schema2 = new Schema();
schema2.addColumn("col1", TIMESTAMP);
testEval(schema2, "table1",
- "1970-01-17 10:09:37",
+ "1970-01-17 22:09:37" + getUserTimeZoneDisplay(),
"select date_part('year', col1), date_part('month', col1), date_part('day', col1) from table1;",
new String[]{"1970.0", "1.0", "17.0"});
Schema schema3 = new Schema();
schema3.addColumn("col1", TIME);
- testEval(schema3, "table1", "10:09:37.5",
+ testEval(schema3, "table1", "10:09:37.5" + getUserTimeZoneDisplay(),
"select date_part('hour', col1), date_part('minute', col1), date_part('second', col1) from table1;",
new String[]{"10.0", "9.0", "37.5"});
@@ -169,9 +222,9 @@ public class TestDateTimeFunctions extends ExprTestBase {
"select date_part('year', col1), date_part('month', col1), date_part('day', col1) from table1;",
new String[]{"1970.0", "1.0", "17.0"});
- testSimpleEval("select date_part('century', TIMESTAMP '1970-01-17 10:09:37');", new String[]{"19.0"});
+ testSimpleEval("select date_part('century', TIMESTAMP '1970-01-17 10:09:37');", new String[]{"20.0"});
- testSimpleEval("select date_part('century', DATE '1970-01-17');", new String[]{"19.0"});
+ testSimpleEval("select date_part('century', DATE '1970-01-17');", new String[]{"20.0"});
testSimpleEval("select date_part('decade', TIMESTAMP '1970-01-17 10:09:37');", new String[]{"197.0"});
@@ -253,45 +306,70 @@ public class TestDateTimeFunctions extends ExprTestBase {
@Test
public void testToDate() throws IOException {
- testSimpleEval("select to_date('2014-01-04', 'yyyy-MM-dd')", new String[]{"2014-01-04"});
- testSimpleEval("select to_date('2014-01-04', 'yyyy-MM-dd') + interval '1 day'", new String[]{"2014-01-05 00:00:00"});
+ testSimpleEval("select to_date('2014-01-04', 'YYYY-MM-DD')", new String[]{"2014-01-04"});
+ testSimpleEval("select to_date('2014-01-04', 'YYYY-MM-DD') + interval '1 day'",
+ new String[]{"2014-01-05 00:00:00" + getUserTimeZoneDisplay()});
}
@Test
public void testAddMonths() throws Exception {
- testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT2);", new String[]{"2014-02-17 00:00:00"});
- testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT4);", new String[]{"2014-02-17 00:00:00"});
- testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT8);", new String[]{"2014-02-17 00:00:00"});
-
- testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT2);", new String[]{"2014-02-17 12:10:20"});
- testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT4);", new String[]{"2014-02-17 12:10:20"});
- testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT8);", new String[]{"2014-02-17 12:10:20"});
-
- testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT2);", new String[]{"2013-11-05 00:00:00"});
- testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT4);", new String[]{"2013-11-05 00:00:00"});
- testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT8);", new String[]{"2013-11-05 00:00:00"});
-
- testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT2);", new String[]{"2013-11-05 12:10:20"});
- testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT4);", new String[]{"2013-11-05 12:10:20"});
- testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT8);", new String[]{"2013-11-05 12:10:20"});
+ testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT2);",
+ new String[]{"2014-02-17 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT4);",
+ new String[]{"2014-02-17 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(date '2013-12-17', 2::INT8);",
+ new String[]{"2014-02-17 00:00:00" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT2);",
+ new String[]{"2014-02-17 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT4);",
+ new String[]{"2014-02-17 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(timestamp '2013-12-17 12:10:20', 2::INT8);",
+ new String[]{"2014-02-17 12:10:20" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT2);",
+ new String[]{"2013-11-05 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT4);",
+ new String[]{"2013-11-05 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(date '2014-02-05', -3::INT8);",
+ new String[]{"2013-11-05 00:00:00" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT2);",
+ new String[]{"2013-11-05 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT4);",
+ new String[]{"2013-11-05 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_months(timestamp '2014-02-05 12:10:20', -3::INT8);",
+ new String[]{"2013-11-05 12:10:20" + getUserTimeZoneDisplay()});
}
@Test
public void testAddDays() throws IOException {
- testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT2);", new String[]{"2014-01-04 00:00:00"});
- testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT4);", new String[]{"2014-01-04 00:00:00"});
- testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT8);", new String[]{"2014-01-04 00:00:00"});
-
- testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT2);", new String[]{"2014-01-04 12:10:20"});
- testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT4);", new String[]{"2014-01-04 12:10:20"});
- testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT8);", new String[]{"2014-01-04 12:10:20"});
-
- testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT2);", new String[]{"2013-11-28 00:00:00"});
- testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT4);", new String[]{"2013-11-28 00:00:00"});
- testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT8);", new String[]{"2013-11-28 00:00:00"});
-
- testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT2);", new String[]{"2013-11-28 12:10:20"});
- testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT4);", new String[]{"2013-11-28 12:10:20"});
- testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT8);", new String[]{"2013-11-28 12:10:20"});
+ testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT2);",
+ new String[]{"2014-01-04 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT4);",
+ new String[]{"2014-01-04 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(date '2013-12-30', 5::INT8);",
+ new String[]{"2014-01-04 00:00:00" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT2);",
+ new String[]{"2014-01-04 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT4);",
+ new String[]{"2014-01-04 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(timestamp '2013-12-30 12:10:20', 5::INT8);",
+ new String[]{"2014-01-04 12:10:20" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT2);",
+ new String[]{"2013-11-28 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT4);",
+ new String[]{"2013-11-28 00:00:00" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(date '2013-12-05', -7::INT8);",
+ new String[]{"2013-11-28 00:00:00" + getUserTimeZoneDisplay()});
+
+ testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT2);",
+ new String[]{"2013-11-28 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT4);",
+ new String[]{"2013-11-28 12:10:20" + getUserTimeZoneDisplay()});
+ testSimpleEval("SELECT add_days(timestamp '2013-12-05 12:10:20', -7::INT8);",
+ new String[]{"2013-11-28 12:10:20" + getUserTimeZoneDisplay()});
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
index 2f5a755..415ed49 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
@@ -21,10 +21,12 @@ package org.apache.tajo.engine.query;
import org.apache.tajo.IntegrationTest;
import org.apache.tajo.QueryTestCaseBase;
import org.apache.tajo.TajoConstants;
+import org.apache.tajo.conf.TajoConf;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.sql.ResultSet;
+import java.util.TimeZone;
@Category(IntegrationTest.class)
public class TestSortQuery extends QueryTestCaseBase {
@@ -117,13 +119,18 @@ public class TestSortQuery extends QueryTestCaseBase {
public final void testSortWithDate() throws Exception {
// skip this test if catalog uses HCatalogStore.
// It is because HCatalogStore does not support Time data type.
- if (!testingCluster.isHCatalogStoreRunning()) {
- // create external table table1 (col1 timestamp, col2 date, col3 time) ...
- executeDDL("create_table_with_date_ddl.sql", "table1");
-
- ResultSet res = executeQuery();
- assertResultSet(res);
- cleanupQuery(res);
+ TimeZone oldTimeZone = TajoConf.setCurrentTimeZone(TimeZone.getTimeZone("UTC"));
+ try {
+ if (!testingCluster.isHCatalogStoreRunning()) {
+ // create external table table1 (col1 timestamp, col2 date, col3 time) ...
+ executeDDL("create_table_with_date_ddl.sql", "table1");
+
+ ResultSet res = executeQuery();
+ assertResultSet(res);
+ cleanupQuery(res);
+ }
+ } finally {
+ TajoConf.setCurrentTimeZone(oldTimeZone);
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-storage/src/main/java/org/apache/tajo/storage/TextSerializerDeserializer.java
----------------------------------------------------------------------
diff --git a/tajo-storage/src/main/java/org/apache/tajo/storage/TextSerializerDeserializer.java b/tajo-storage/src/main/java/org/apache/tajo/storage/TextSerializerDeserializer.java
index 41ee720..ad732c7 100644
--- a/tajo-storage/src/main/java/org/apache/tajo/storage/TextSerializerDeserializer.java
+++ b/tajo-storage/src/main/java/org/apache/tajo/storage/TextSerializerDeserializer.java
@@ -22,6 +22,7 @@ import com.google.protobuf.Message;
import org.apache.commons.codec.binary.Base64;
import org.apache.tajo.catalog.Column;
import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.conf.TajoConf;
import org.apache.tajo.datum.*;
import org.apache.tajo.datum.protobuf.ProtobufJsonFormat;
import org.apache.tajo.util.Bytes;
@@ -77,13 +78,21 @@ public class TextSerializerDeserializer implements SerializerDeserializer {
case FLOAT8:
case INET4:
case DATE:
- case TIME:
- case TIMESTAMP:
case INTERVAL:
bytes = datum.asTextBytes();
length = bytes.length;
out.write(bytes);
break;
+ case TIME:
+ bytes = ((TimeDatum)datum).asChars(TajoConf.getCurrentTimeZone(), true).getBytes();
+ length = bytes.length;
+ out.write(bytes);
+ break;
+ case TIMESTAMP:
+ bytes = ((TimestampDatum)datum).asChars(TajoConf.getCurrentTimeZone(), true).getBytes();
+ length = bytes.length;
+ out.write(bytes);
+ break;
case INET6:
case BLOB:
bytes = Base64.encodeBase64(datum.asByteArray(), false);
@@ -158,7 +167,7 @@ public class TextSerializerDeserializer implements SerializerDeserializer {
break;
case TIMESTAMP:
datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get()
- : DatumFactory.createTimeStamp(new String(bytes, offset, length));
+ : DatumFactory.createTimestamp(new String(bytes, offset, length));
break;
case INTERVAL:
datum = isNull(bytes, offset, length, nullCharacters) ? NullDatum.get()
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-storage/src/test/java/org/apache/tajo/storage/TestStorages.java
----------------------------------------------------------------------
diff --git a/tajo-storage/src/test/java/org/apache/tajo/storage/TestStorages.java b/tajo-storage/src/test/java/org/apache/tajo/storage/TestStorages.java
index 0b764cc..944aa5c 100644
--- a/tajo-storage/src/test/java/org/apache/tajo/storage/TestStorages.java
+++ b/tajo-storage/src/test/java/org/apache/tajo/storage/TestStorages.java
@@ -683,7 +683,7 @@ public class TestStorages {
tuple.put(new Datum[]{
DatumFactory.createDate("1980-04-01"),
DatumFactory.createTime("12:34:56"),
- DatumFactory.createTimeStamp((int) System.currentTimeMillis() / 1000)
+ DatumFactory.createTimestmpDatumWithUnixTime((int)(System.currentTimeMillis() / 1000))
});
appender.addTuple(tuple);
appender.flush();
[2/6] TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
Posted by ji...@apache.org.
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
index 6551077..818c296 100644
--- a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimestampDatum.java
@@ -19,105 +19,162 @@
package org.apache.tajo.datum;
import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.conf.TajoConf;
import org.apache.tajo.exception.InvalidCastException;
import org.apache.tajo.json.CommonGsonHelper;
+import org.apache.tajo.util.datetime.DateTimeUtil;
import org.junit.BeforeClass;
import org.junit.Test;
+import java.util.Calendar;
+import java.util.TimeZone;
+
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class TestTimestampDatum {
-
- private static int timestamp;
+ private static long javatime;
+ private static int unixtime;
+ private static Calendar calendar;
@BeforeClass
public static void setUp() {
- timestamp = (int) (System.currentTimeMillis() / 1000);
+ javatime = System.currentTimeMillis();
+ calendar = Calendar.getInstance(TajoConf.getCurrentTimeZone());
+ calendar.setTimeInMillis(javatime);
+ unixtime = (int) (javatime / 1000);
}
@Test
public final void testType() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
- assertEquals(Type.TIMESTAMP, d.type());
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
+ assertEquals(Type.TIMESTAMP, d.type());
}
@Test(expected = InvalidCastException.class)
public final void testAsInt4() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
d.asInt4();
}
@Test
public final void testAsInt8() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
- long javaTime = timestamp * 1000l;
- assertEquals(javaTime, d.asInt8());
+ Datum d = DatumFactory.createTimestmpDatumWithJavaMillis(unixtime * 1000);
+ long javaTime = unixtime * 1000;
+ assertEquals(DateTimeUtil.javaTimeToJulianTime(javaTime), d.asInt8());
}
@Test(expected = InvalidCastException.class)
public final void testAsFloat4() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
d.asFloat4();
}
@Test(expected = InvalidCastException.class)
public final void testAsFloat8() {
int instance = 1386577582;
- Datum d = DatumFactory.createTimeStamp(instance);
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(instance);
d.asFloat8();
}
@Test
public final void testAsText() {
- Datum d = DatumFactory.createTimeStamp("1980-04-01 01:50:01");
- Datum copy = DatumFactory.createTimeStamp(d.asChars());
+ Datum d = DatumFactory.createTimestamp("1980-04-01 01:50:01");
+ Datum copy = DatumFactory.createTimestamp(d.asChars());
assertEquals(d, copy);
- }
- @Test
- public final void testAsByteArray() {
- TimestampDatum d = DatumFactory.createTimeStamp(timestamp);
- TimestampDatum copy = new TimestampDatum(d.asByteArray());
+ d = DatumFactory.createTimestamp("1980-04-01 01:50:01.10");
+ copy = DatumFactory.createTimestamp(d.asChars());
assertEquals(d, copy);
- }
+ }
@Test
public final void testSize() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
assertEquals(TimestampDatum.SIZE, d.asByteArray().length);
}
@Test
public final void testAsTextBytes() {
- Datum d = DatumFactory.createTimeStamp("1980-04-01 01:50:01");
+ Datum d = DatumFactory.createTimestamp("1980-04-01 01:50:01");
assertArrayEquals(d.toString().getBytes(), d.asTextBytes());
- d = DatumFactory.createTimeStamp("1980-04-01 01:50:01.578");
+ d = DatumFactory.createTimestamp("1980-04-01 01:50:01.578");
assertArrayEquals(d.toString().getBytes(), d.asTextBytes());
}
@Test
public final void testToJson() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
Datum copy = CommonGsonHelper.fromJson(d.toJson(), Datum.class);
assertEquals(d, copy);
}
@Test
- public final void testGetFields() {
- TimestampDatum d = DatumFactory.createTimeStamp("1980-04-01 01:50:01");
- assertEquals(1980, d.getYear());
- assertEquals(4, d.getMonthOfYear());
- assertEquals(1, d.getDayOfMonth());
- assertEquals(1, d.getHourOfDay());
- assertEquals(50, d.getMinuteOfHour());
- assertEquals(01, d.getSecondOfMinute());
+ public final void testTimeZone() {
+ TimestampDatum datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(2014, 5, 1, 15, 20, 30, 0));
+ assertEquals("2014-05-01 15:20:30", datum.asChars());
+ assertEquals("2014-05-02 00:20:30+09", datum.asChars(TimeZone.getTimeZone("GMT+9"), true));
+ }
+
+ @Test
+ public final void testTimestampConstructor() {
+ TimestampDatum datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(2014, 5, 1, 10, 20, 30, 0));
+ assertEquals(2014, datum.getYear());
+ assertEquals(5, datum.getMonthOfYear());
+ assertEquals(1, datum.getDayOfMonth());
+ assertEquals(10, datum.getHourOfDay());
+ assertEquals(20, datum.getMinuteOfHour());
+ assertEquals(30, datum.getSecondOfMinute());
+
+ TimestampDatum datum2 = DatumFactory.createTimestamp("2014-05-01 10:20:30");
+ assertEquals(datum2, datum);
+
+ datum = DatumFactory.createTimestamp("1980-04-01 01:50:01.123");
+ assertEquals(1980, datum.getYear());
+ assertEquals(4, datum.getMonthOfYear());
+ assertEquals(1, datum.getDayOfMonth());
+ assertEquals(1, datum.getHourOfDay());
+ assertEquals(50, datum.getMinuteOfHour());
+ assertEquals(1, datum.getSecondOfMinute());
+ assertEquals(123, datum.getMillisOfSecond());
+
+ datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(1014, 5, 1, 10, 20, 30, 0));
+ assertEquals(1014, datum.getYear());
+ assertEquals(5, datum.getMonthOfYear());
+ assertEquals(1, datum.getDayOfMonth());
+ assertEquals(10, datum.getHourOfDay());
+ assertEquals(20, datum.getMinuteOfHour());
+ assertEquals(30, datum.getSecondOfMinute());
+
+ datum2 = DatumFactory.createTimestamp("1014-05-01 10:20:30");
+ assertEquals(datum2, datum);
+
+ for (int i = 0; i < 100; i++) {
+ Calendar cal = Calendar.getInstance();
+ long jTime = System.currentTimeMillis();
+ int uTime = (int)(jTime / 1000);
+ cal.setTimeInMillis(jTime);
+
+ long julianTimestamp = DateTimeUtil.javaTimeToJulianTime(jTime);
+ assertEquals(uTime, DateTimeUtil.julianTimeToEpoch(julianTimestamp));
+ assertEquals(jTime, DateTimeUtil.julianTimeToJavaTime(julianTimestamp));
+
+ TimestampDatum datum3 = DatumFactory.createTimestmpDatumWithJavaMillis(jTime);
+ assertEquals(cal.get(Calendar.YEAR), datum3.getYear());
+ assertEquals(cal.get(Calendar.MONTH) + 1, datum3.getMonthOfYear());
+ assertEquals(cal.get(Calendar.DAY_OF_MONTH), datum3.getDayOfMonth());
+
+ datum3 = DatumFactory.createTimestmpDatumWithUnixTime(uTime);
+ assertEquals(cal.get(Calendar.YEAR), datum3.getYear());
+ assertEquals(cal.get(Calendar.MONTH) + 1, datum3.getMonthOfYear());
+ assertEquals(cal.get(Calendar.DAY_OF_MONTH), datum3.getDayOfMonth());
+ }
}
@Test
public final void testNull() {
- Datum d = DatumFactory.createTimeStamp(timestamp);
+ Datum d = DatumFactory.createTimestmpDatumWithUnixTime(unixtime);
assertEquals(Boolean.FALSE,d.equals(DatumFactory.createNullDatum()));
assertEquals(DatumFactory.createNullDatum(),d.equalsTo(DatumFactory.createNullDatum()));
assertEquals(-1,d.compareTo(DatumFactory.createNullDatum()));
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeFormat.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeFormat.java b/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeFormat.java
new file mode 100644
index 0000000..0bfe708
--- /dev/null
+++ b/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeFormat.java
@@ -0,0 +1,158 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.util;
+
+import org.apache.tajo.datum.TimestampDatum;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.TimeMeta;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormatter;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class TestDateTimeFormat {
+ @Test
+ public void testToTimestamp() {
+ evalToTimestampAndAssert("1997-12-30 11:40:50.345", "YYYY-MM-DD HH24:MI:SS.MS", 1997, 12, 30, 11, 40, 50, 345);
+ evalToTimestampAndAssert("1997-12-30 11:40:50.345 PM", "YYYY-MM-DD HH24:MI:SS.MS PM", 1997, 12, 30, 23, 40, 50, 345);
+ evalToTimestampAndAssert("0097/Feb/16 --> 08:14:30", "YYYY/Mon/DD --> HH:MI:SS", 97, 2, 16, 8, 14, 30, 0);
+ evalToTimestampAndAssert("97/2/16 8:14:30", "FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS", 97, 2, 16, 8, 14, 30, 0);
+ evalToTimestampAndAssert("1985 September 12", "YYYY FMMonth DD", 1985, 9, 12, 0, 0, 0, 0);
+ evalToTimestampAndAssert("1,582nd VIII 21", "Y,YYYth FMRM DD", 1582, 8, 21, 0, 0, 0, 0);
+ evalToTimestampAndAssert("05121445482000", "MMDDHH24MISSYYYY", 2000, 5, 12, 14, 45, 48, 0);
+ evalToTimestampAndAssert("2000January09Sunday", "YYYYFMMonthDDFMDay", 2000, 1, 9, 0, 0, 0, 0);
+ evalToTimestampAndAssert("97/Feb/16", "YY/Mon/DD", 1997, 2, 16, 0, 0, 0, 0);
+ evalToTimestampAndAssert("19971116", "YYYYMMDD", 1997, 11, 16, 0, 0, 0, 0);
+ evalToTimestampAndAssert("20000-1116", "YYYY-MMDD", 20000, 11, 16, 0, 0, 0, 0);
+ evalToTimestampAndAssert("9-1116", "Y-MMDD", 2009, 11, 16, 0, 0, 0, 0);
+ evalToTimestampAndAssert("95-1116", "YY-MMDD", 1995, 11, 16, 0, 0, 0, 0);
+ evalToTimestampAndAssert("995-1116", "YYY-MMDD", 1995, 11, 16, 0, 0, 0, 0);
+ evalToTimestampAndAssert("2005426", "YYYYWWD", 2005, 10, 15, 0, 0, 0, 0);
+ evalToTimestampAndAssert("2005300", "YYYYDDD", 2005, 10, 27, 0, 0, 0, 0);
+ evalToTimestampAndAssert("2005527", "IYYYIWID", 2006, 1, 1, 0, 0, 0, 0);
+ evalToTimestampAndAssert("005527", "IYYIWID", 2006, 1, 1, 0, 0, 0, 0);
+ evalToTimestampAndAssert("05527", "IYIWID", 2006, 1, 1, 0, 0, 0, 0);
+ evalToTimestampAndAssert("5527", "IIWID", 2006, 1, 1, 0, 0, 0, 0);
+ evalToTimestampAndAssert("2005364", "IYYYIDDD", 2006, 1, 1, 0, 0, 0, 0);
+ evalToTimestampAndAssert("20050302", "YYYYMMDD", 2005, 3, 2, 0, 0, 0, 0);
+ evalToTimestampAndAssert("2005 03 02", "YYYYMMDD", 2005, 3, 2, 0, 0, 0, 0);
+ evalToTimestampAndAssert(" 2005 03 02", "YYYYMMDD", 2005, 3, 2, 0, 0, 0, 0);
+ evalToTimestampAndAssert(" 20050302", "YYYYMMDD", 2005, 3, 2, 0, 0, 0, 0);
+
+ //In the case of using Format Cache
+ evalToTimestampAndAssert("1998-02-28 10:20:30.123 PM", "YYYY-MM-DD HH24:MI:SS.MS PM", 1998, 2, 28, 22, 20, 30, 123);
+
+
+ try {
+ evalToTimestampAndAssert("97/Feb/16", "YYMonDD", 1997, 2, 16, 0, 0, 0, 0);
+ fail("Should be throw exception");
+ } catch (Exception e) {
+ //Invalid value /Fe for Mon. The given value did not match any of the allowed values for this field.
+ }
+
+ try {
+ evalToTimestampAndAssert("2005527", "YYYYIWID", 2005, 5, 27, 0, 0, 0, 0);
+ fail("Should be throw exception");
+ } catch (Exception e) {
+ //Do not mix Gregorian and ISO week date conventions in a formatting template.
+ }
+ try {
+ evalToTimestampAndAssert("19971", "YYYYMMDD", 1997, 1, 1, 0, 0, 0, 0);
+ fail("Should be throw exception");
+ } catch (Exception e) {
+ //If your source string is not fixed-width, try using the "FM" modifier.
+ }
+
+ TimeMeta tm = DateTimeFormat.parseDateTime("10:09:37.5", "HH24:MI:SS.MS");
+ assertEquals(10, tm.hours);
+ }
+
+ @Test
+ public void testToChar() {
+ evalToCharAndAssert("1970-01-17 10:09:37", "YYYY-MM-DD HH24:MI:SS", "1970-01-17 10:09:37");
+ evalToCharAndAssert("1997-12-30 11:40:50.345", "YYYY-MM-DD HH24:MI:SS.MS", "1997-12-30 11:40:50.345");
+ evalToCharAndAssert("1997-12-30 11:40:50.345 PM", "YYYY-MM-DD HH24:MI:SS.MS PM", "1997-12-30 23:40:50.345 PM");
+ evalToCharAndAssert("0097/Feb/16 --> 08:14:30", "YYYY/Mon/DD --> HH:MI:SS", "0097/Feb/16 --> 08:14:30");
+ evalToCharAndAssert("97/2/16 8:14:30", "FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS", "97/2/16 8:14:30");
+ evalToCharAndAssert("1985 September 12", "YYYY FMMonth DD", "1985 September 12");
+ evalToCharAndAssert("1,582nd VIII 21", "Y,YYYth FMRM DD", "1,582nd VIII 21");
+ evalToCharAndAssert("05121445482000", "MMDDHH24MISSYYYY", "05121445482000");
+ evalToCharAndAssert("2000January09Sunday", "YYYYFMMonthDDFMDay", "2000January09Sunday");
+ evalToCharAndAssert("97/Feb/16", "YY/Mon/DD", "97/Feb/16");
+ evalToCharAndAssert("19971116", "YYYYMMDD", "19971116");
+ evalToCharAndAssert("20000-1116", "YYYY-MMDD", "20000-1116");
+ evalToCharAndAssert("9-1116", "Y-MMDD", "9-1116");
+ evalToCharAndAssert("95-1116", "YY-MMDD", "95-1116");
+ evalToCharAndAssert("995-1116", "YYY-MMDD", "995-1116");
+ evalToCharAndAssert("2005426", "YYYYWWD", "2005427");
+ evalToCharAndAssert("2005300", "YYYYDDD", "2005300");
+ evalToCharAndAssert("2005527", "IYYYIWID", "2005527");
+ evalToCharAndAssert("005527", "IYYIWID", "005527");
+ evalToCharAndAssert("05527", "IYIWID", "05527");
+ evalToCharAndAssert("5527", "IIWID", "5527");
+ evalToCharAndAssert("2005364", "IYYYIDDD", "2005364");
+ evalToCharAndAssert("20050302", "YYYYMMDD", "20050302");
+ evalToCharAndAssert("2005 03 02", "YYYYMMDD", "YYYY MM DD", "2005 03 02");
+ evalToCharAndAssert(" 2005 03 02", "YYYYMMDD", " YYYY MM DD", " 2005 03 02");
+ evalToCharAndAssert(" 20050302", "YYYYMMDD", " YYYYMMDD", " 20050302");
+ }
+
+ @Test
+ public void testPerformance() {
+ DateTimeFormatter jodaFormat = org.joda.time.format.DateTimeFormat.forPattern("YYYY-MM-DD HH:mm:ss.SSS");
+ long startTime = System.currentTimeMillis();
+ for (int i = 0; i < 10000000; i++) {
+ DateTime dateTime = jodaFormat.parseDateTime("1997-12-30 11:40:50.345");
+ }
+ long endTime = System.currentTimeMillis();
+ System.out.println("total parse time with JodaTime:" + (endTime - startTime) + " ms");
+
+ startTime = System.currentTimeMillis();
+ for (int i = 0; i < 10000000; i++) {
+ TimestampDatum datum = DateTimeFormat.toTimestamp("1997-12-30 11:40:50.345", "YYYY-MM-DD HH24:MI:SS.MS");
+ }
+ endTime = System.currentTimeMillis();
+ System.out.println("total parse time with TajoDateTimeFormat:" + (endTime - startTime) + " ms");
+ }
+
+ private void evalToTimestampAndAssert(String dateTimeText, String formatText,
+ int year, int month, int day, int hour, int minute, int sec, int msec) {
+ TimestampDatum datum = DateTimeFormat.toTimestamp(dateTimeText, formatText);
+ assertEquals(year, datum.getYear());
+ assertEquals(month, datum.getMonthOfYear());
+ assertEquals(day, datum.getDayOfMonth());
+ assertEquals(hour, datum.getHourOfDay());
+ assertEquals(minute, datum.getMinuteOfHour());
+ assertEquals(sec, datum.getSecondOfMinute());
+ assertEquals(msec, datum.getMillisOfSecond());
+ }
+
+ private void evalToCharAndAssert(String dateTimeText, String toTimestampFormatText, String expected) {
+ evalToCharAndAssert(dateTimeText, toTimestampFormatText, toTimestampFormatText, expected);
+ }
+
+ private void evalToCharAndAssert(String dateTimeText,
+ String toTimestampFormatText, String toCharFormatText, String expected) {
+ TimestampDatum datum = DateTimeFormat.toTimestamp(dateTimeText, toTimestampFormatText);
+ String toCharResult = DateTimeFormat.to_char(datum.toTimeMeta(), toCharFormatText);
+ assertEquals(expected, toCharResult);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeUtil.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeUtil.java b/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeUtil.java
new file mode 100644
index 0000000..e953d44
--- /dev/null
+++ b/tajo-common/src/test/java/org/apache/tajo/util/TestDateTimeUtil.java
@@ -0,0 +1,439 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.util;
+
+import org.apache.tajo.conf.TajoConf;
+import org.apache.tajo.util.datetime.DateTimeConstants;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+import org.junit.Test;
+
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import static org.junit.Assert.*;
+
+public class TestDateTimeUtil {
+ private static final int TEST_YEAR = 2014;
+ private static final int TEST_MONTH_OF_YEAR = 4;
+ private static final int TEST_DAY_OF_MONTH = 18;
+ private static final int TEST_HOUR_OF_DAY = 0;
+ private static final int TEST_MINUTE_OF_HOUR = 15;
+ private static final int TEST_SECOND_OF_MINUTE = 25;
+ private static final DateTime TEST_DATETIME = new DateTime(TEST_YEAR, TEST_MONTH_OF_YEAR, TEST_DAY_OF_MONTH,
+ TEST_HOUR_OF_DAY, TEST_MINUTE_OF_HOUR, TEST_SECOND_OF_MINUTE, DateTimeZone.UTC);
+ private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
+
+ @Test
+ public void testDecodeDateTime() {
+ // http://www.postgresql.org/docs/9.1/static/datatype-datetime.html
+ TimeMeta tm = DateTimeUtil.decodeDateTime("2014-01-07 14:12:54+09");
+ assertEquals(2014, tm.years);
+ assertEquals(1, tm.monthOfYear);
+ assertEquals(7, tm.dayOfMonth);
+ assertEquals(14, tm.hours);
+ assertEquals(12, tm.minutes);
+ assertEquals(54, tm.secs);
+ assertEquals(0, tm.fsecs);
+
+ tm = DateTimeUtil.decodeDateTime("1999-01-08 04:05:06.789");
+ assertEquals(1999, tm.years);
+ assertEquals(1, tm.monthOfYear);
+ assertEquals(8, tm.dayOfMonth);
+ assertEquals(4, tm.hours);
+ assertEquals(5, tm.minutes);
+ assertEquals(6, tm.secs);
+ assertEquals(7 * 100000 + 8 * 10000 + 9 * 1000, tm.fsecs);
+
+ TimeMeta tm2 = DateTimeUtil.decodeDateTime("January 8, 1999 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ try {
+ tm2 = DateTimeUtil.decodeDateTime("January 8, 99 04:05:06.789");
+ assertEquals(tm, tm2);
+ fail("error in YMD mode");
+ } catch (Exception e) {
+ //throws Exception in YMD mode
+ //BAD Format: day overflow:99
+ }
+
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_MDY);
+ tm2 = DateTimeUtil.decodeDateTime("January 8, 99 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_YMD);
+ tm2 = DateTimeUtil.decodeDateTime("1999/1/8 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ tm2 = DateTimeUtil.decodeDateTime("1999/01/08 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ //January 2, 2003 in MDY mode; February 1, 2003 in DMY mode; February 3, 2001 in YMD mode
+ tm2 = DateTimeUtil.decodeDateTime("01/02/03 04:05:06.789");
+ assertEquals(2001, tm2.years);
+ assertEquals(2, tm2.monthOfYear);
+ assertEquals(3, tm2.dayOfMonth);
+ assertEquals(4, tm2.hours);
+ assertEquals(5, tm2.minutes);
+ assertEquals(6, tm2.secs);
+ assertEquals(7 * 100000 + 8 * 10000 + 9 * 1000, tm2.fsecs);
+
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_MDY);
+ tm2 = DateTimeUtil.decodeDateTime("01/02/03 04:05:06.789");
+ assertEquals(2003, tm2.years);
+ assertEquals(1, tm2.monthOfYear);
+ assertEquals(2, tm2.dayOfMonth);
+ assertEquals(4, tm2.hours);
+ assertEquals(5, tm2.minutes);
+ assertEquals(6, tm2.secs);
+ assertEquals(7 * 100000 + 8 * 10000 + 9 * 1000, tm2.fsecs);
+
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_DMY);
+ tm2 = DateTimeUtil.decodeDateTime("01/02/03 04:05:06.789");
+ assertEquals(2003, tm2.years);
+ assertEquals(2, tm2.monthOfYear);
+ assertEquals(1, tm2.dayOfMonth);
+ assertEquals(4, tm2.hours);
+ assertEquals(5, tm2.minutes);
+ assertEquals(6, tm2.secs);
+ assertEquals(7 * 100000 + 8 * 10000 + 9 * 1000, tm2.fsecs);
+
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_YMD);
+ tm2 = DateTimeUtil.decodeDateTime("1999-Jan-08 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ tm2 = DateTimeUtil.decodeDateTime("Jan-08-1999 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ tm2 = DateTimeUtil.decodeDateTime("08-Jan-1999 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ tm2 = DateTimeUtil.decodeDateTime("99-Jan-08 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ //January 8, except error in YMD mode
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_MDY);
+ tm2 = DateTimeUtil.decodeDateTime("08-Jan-99 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ //January 8, except error in YMD mode
+ tm2 = DateTimeUtil.decodeDateTime("Jan-08-99 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ TajoConf.setDateOrder(DateTimeConstants.DATEORDER_YMD);
+ tm2 = DateTimeUtil.decodeDateTime("19990108 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ tm2 = DateTimeUtil.decodeDateTime("990108 04:05:06.789");
+ assertEquals(tm, tm2);
+
+ //year and day of year
+ tm2 = DateTimeUtil.decodeDateTime("1999.008");
+ assertEquals(1999, tm2.years);
+ assertEquals(1, tm2.monthOfYear);
+ assertEquals(8, tm2.dayOfMonth);
+
+ //BC
+ tm = DateTimeUtil.decodeDateTime("19990108 BC 04:05:06.789");
+ assertEquals(-1998, tm.years);
+ assertEquals(1, tm.monthOfYear);
+ assertEquals(8, tm.dayOfMonth);
+ assertEquals(4, tm.hours);
+ assertEquals(5, tm.minutes);
+ assertEquals(6, tm.secs);
+ assertEquals(7 * 100000 + 8 * 10000 + 9 * 1000, tm.fsecs);
+
+ //PM
+ tm = DateTimeUtil.decodeDateTime("2013-04-25 10:20:30.4 PM");
+ assertEquals(2013, tm.years);
+ assertEquals(4, tm.monthOfYear);
+ assertEquals(25, tm.dayOfMonth);
+ assertEquals(22, tm.hours);
+ assertEquals(20, tm.minutes);
+ assertEquals(30, tm.secs);
+ assertEquals(4 * 100000, tm.fsecs);
+
+ // date only
+ tm = DateTimeUtil.decodeDateTime("1980-04-01");
+ assertEquals(1980, tm.years);
+ assertEquals(4, tm.monthOfYear);
+ assertEquals(1, tm.dayOfMonth);
+ }
+
+ @Test
+ public void testToJulianTimestamp() {
+ long julian = DateTimeUtil.toJulianTimestamp("2013-04-25");
+ assertEquals(julian, DateTimeUtil.toJulianTimestamp("2013-4-25"));
+ assertEquals(julian, DateTimeUtil.toJulianTimestamp("2013.4.25"));
+ }
+
+ @Test
+ public void testTimestampToJavaOrUnix() {
+ Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+ long javaTime = System.currentTimeMillis();
+ cal.setTimeInMillis(javaTime);
+
+ long julianTimestamp = DateTimeUtil.javaTimeToJulianTime(cal.getTimeInMillis());
+
+ assertEquals(javaTime, DateTimeUtil.julianTimeToJavaTime(julianTimestamp));
+ assertEquals(javaTime/1000, DateTimeUtil.julianTimeToEpoch(julianTimestamp));
+ }
+
+ @Test
+ public void testLeapYear() {
+ assertTrue(DateTimeUtil.isLeapYear(2000));
+ assertTrue(DateTimeUtil.isLeapYear(2004));
+ assertTrue(DateTimeUtil.isLeapYear(1600));
+ assertFalse(DateTimeUtil.isLeapYear(1900));
+ assertFalse(DateTimeUtil.isLeapYear(2005));
+ }
+
+ @Test
+ public void testAddMonthsToTimeMeta() {
+ // Leap year
+ String dateTimeStr = "2000-01-29 23:11:50.123";
+ TimeMeta tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusMonths(1);
+ assertEquals("2000-02-29 23:11:50.123", tm.toString());
+
+ // Non leap year
+ dateTimeStr = "1999-01-29 23:11:50.123";
+ tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusMonths(1);
+ assertEquals("1999-02-28 23:11:50.123", tm.toString());
+
+ // changing year
+ dateTimeStr = "2013-09-30 23:11:50.123";
+ tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusMonths(5);
+ assertEquals("2014-02-28 23:11:50.123", tm.toString());
+
+ // minus value
+ dateTimeStr = "2013-03-30 23:11:50.123";
+ tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusMonths(-5);
+ assertEquals("2012-10-30 23:11:50.123", tm.toString());
+ }
+
+ @Test
+ public void testAddDaysToTimeMeta() {
+ // Leap year
+ String dateTimeStr = "2000-02-29 23:11:50.123";
+ TimeMeta tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusDays(1);
+ assertEquals("2000-03-01 23:11:50.123", tm.toString());
+
+ // Non leap year
+ dateTimeStr = "1999-01-29 23:11:50.123";
+ tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusDays(1);
+ assertEquals("1999-01-30 23:11:50.123", tm.toString());
+
+ // changing year
+ dateTimeStr = "2013-12-25 23:11:50.123";
+ tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusDays(7);
+ assertEquals("2014-01-01 23:11:50.123", tm.toString());
+
+ // minus value
+ dateTimeStr = "2000-03-05 23:11:50.123";
+ tm = DateTimeUtil.decodeDateTime(dateTimeStr);
+ tm.plusDays(-10);
+ assertEquals("2000-02-24 23:11:50.123", tm.toString());
+ }
+
+ @Test
+ public void testEncodeDateTime() throws Exception {
+ //DateTimeUtil.encodeDateTime()
+
+ }
+
+ @Test
+ public void testAppendSeconds() throws Exception {
+ String[] fractions = new String[]{".999999", ".99999", ".9999", ".999", ".99", ".9", ""};
+
+ for (int i = 0; i < fractions.length; i++) {
+ StringBuilder sb = new StringBuilder("13:52:");
+ DateTimeUtil.appendSecondsToEncodeOutput(sb, 23, 999999, 6 - i, false);
+ assertEquals("13:52:23" + fractions[i], sb.toString());
+ }
+
+ fractions = new String[]{".1", ".01", ".001", ".0001", ".00001", ".000001"};
+ for (int i = 0; i < fractions.length; i++) {
+ StringBuilder sb = new StringBuilder("13:52:");
+ DateTimeUtil.appendSecondsToEncodeOutput(sb, 23, (int)Math.pow(10, (5 - i)), 6, false);
+ assertEquals("13:52:23" + fractions[i], sb.toString());
+ }
+ }
+
+ @Test
+ public void testTrimTrailingZeros() throws Exception {
+ StringBuilder sb1 = new StringBuilder("1.1200");
+ DateTimeUtil.trimTrailingZeros(sb1);
+ assertEquals("1.12", sb1.toString());
+
+ StringBuilder sb2 = new StringBuilder("1.12000120");
+ DateTimeUtil.trimTrailingZeros(sb2);
+ assertEquals("1.1200012", sb2.toString());
+
+ StringBuilder sb3 = new StringBuilder(".12000120");
+ DateTimeUtil.trimTrailingZeros(sb3);
+ assertEquals(".1200012", sb3.toString());
+ }
+
+ @Test
+ public void testTimeMeta() {
+ TimeMeta tm = DateTimeUtil.decodeDateTime("2014-12-31");
+ assertEquals(365, tm.getDayOfYear());
+
+ tm = DateTimeUtil.decodeDateTime("2000-03-01");
+ assertEquals(61, tm.getDayOfYear());
+
+ tm = DateTimeUtil.decodeDateTime("2014-01-01");
+ assertEquals(3, tm.getDayOfWeek());
+ assertEquals(1, tm.getWeekOfYear());
+ assertEquals(21, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("2000-03-01");
+ assertEquals(3, tm.getDayOfWeek());
+ assertEquals(9, tm.getWeekOfYear());
+ assertEquals(20, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("1752-09-14");
+ assertEquals(4, tm.getDayOfWeek());
+ assertEquals(37, tm.getWeekOfYear());
+ assertEquals(18, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("1752-09-02");
+ assertEquals(6, tm.getDayOfWeek());
+ assertEquals(35, tm.getWeekOfYear());
+ assertEquals(18, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("1200-04-01");
+ assertEquals(6, tm.getDayOfWeek());
+ assertEquals(13, tm.getWeekOfYear());
+ assertEquals(12, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("400-04-20");
+ assertEquals(4, tm.getDayOfWeek());
+ assertEquals(16, tm.getWeekOfYear());
+ assertEquals(4, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("310-12-31");
+ assertEquals(6, tm.getDayOfWeek());
+ assertEquals(52, tm.getWeekOfYear());
+ assertEquals(4, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("0080-02-29");
+ assertEquals(4, tm.getDayOfWeek());
+ assertEquals(9, tm.getWeekOfYear());
+ assertEquals(1, tm.getCenturyOfEra());
+
+ tm = DateTimeUtil.decodeDateTime("400-03-01 BC");
+ assertEquals(4, tm.getDayOfWeek());
+ assertEquals(9, tm.getWeekOfYear());
+ assertEquals(-4, tm.getCenturyOfEra());
+ }
+
+ @Test
+ public void testStrtoi() {
+ StringBuilder sb = new StringBuilder();
+ int intVal = 12345;
+ String textVal = "test";
+
+ int value = DateTimeUtil.strtoi(intVal + textVal, 0, sb);
+ assertEquals(intVal, value);
+ assertEquals(textVal, sb.toString());
+
+ textVal = "";
+
+ value = DateTimeUtil.strtoi(intVal + textVal, 0, sb);
+ assertEquals(intVal, value);
+ assertEquals(textVal, sb.toString());
+ }
+
+ @Test
+ public void testGetCenturyOfEra() {
+ assertEquals(1, DateTimeUtil.getCenturyOfEra(1));
+ assertEquals(1, DateTimeUtil.getCenturyOfEra(100));
+ assertEquals(2, DateTimeUtil.getCenturyOfEra(101));
+ assertEquals(10, DateTimeUtil.getCenturyOfEra(1000));
+ assertEquals(20, DateTimeUtil.getCenturyOfEra(1998));
+ assertEquals(20, DateTimeUtil.getCenturyOfEra(1999));
+ assertEquals(20, DateTimeUtil.getCenturyOfEra(2000));
+ assertEquals(21, DateTimeUtil.getCenturyOfEra(2001));
+ assertEquals(21, DateTimeUtil.getCenturyOfEra(2100));
+ assertEquals(22, DateTimeUtil.getCenturyOfEra(2101));
+
+ assertEquals(-6, DateTimeUtil.getCenturyOfEra(-600));
+ assertEquals(-6, DateTimeUtil.getCenturyOfEra(-501));
+ assertEquals(-5, DateTimeUtil.getCenturyOfEra(-500));
+ assertEquals(-5, DateTimeUtil.getCenturyOfEra(-455));
+ assertEquals(-1, DateTimeUtil.getCenturyOfEra(-1));
+ }
+
+ @Test
+ public void testGetTimeZoneDisplayTime() {
+ assertEquals("", DateTimeUtil.getTimeZoneDisplayTime(TimeZone.getTimeZone("GMT")));
+ assertEquals("+09", DateTimeUtil.getTimeZoneDisplayTime(TimeZone.getTimeZone("GMT+9")));
+ assertEquals("+09:10", DateTimeUtil.getTimeZoneDisplayTime(TimeZone.getTimeZone("GMT+9:10")));
+ assertEquals("-09", DateTimeUtil.getTimeZoneDisplayTime(TimeZone.getTimeZone("GMT-9")));
+ assertEquals("-09:10", DateTimeUtil.getTimeZoneDisplayTime(TimeZone.getTimeZone("GMT-9:10")));
+ }
+
+ @Test
+ public void testGetYear() {
+ assertEquals(DateTime.parse("2014-01-01 00:00:00", fmt.withZoneUTC()).getMillis() * 1000,
+ DateTimeUtil.getYear(TEST_DATETIME));
+ }
+
+ @Test
+ public void testGetMonth() {
+ assertEquals(DateTime.parse("2014-04-01 00:00:00", fmt.withZoneUTC()).getMillis() * 1000,
+ DateTimeUtil.getMonth(TEST_DATETIME));
+ }
+
+ @Test
+ public void testGetDay() {
+ assertEquals(DateTime.parse("2014-04-18 00:00:00", fmt.withZoneUTC()).getMillis() * 1000,
+ DateTimeUtil.getDay(TEST_DATETIME));
+ }
+
+ @Test
+ public void testGetHour() {
+ assertEquals(DateTime.parse("2014-04-18 00:00:00",fmt.withZoneUTC()).getMillis() * 1000,
+ DateTimeUtil.getHour(TEST_DATETIME));
+ }
+
+ @Test
+ public void testGetMinute() {
+ assertEquals(DateTime.parse("2014-04-18 00:15:00",fmt.withZoneUTC()).getMillis() * 1000,
+ DateTimeUtil.getMinute(TEST_DATETIME));
+ }
+
+ @Test
+ public void testGetSecond() {
+ assertEquals(DateTime.parse("2014-04-18 00:15:25",fmt.withZoneUTC()).getMillis() * 1000,
+ DateTimeUtil.getSecond(TEST_DATETIME));
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/util/TestTimeStampUtil.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/util/TestTimeStampUtil.java b/tajo-common/src/test/java/org/apache/tajo/util/TestTimeStampUtil.java
deleted file mode 100644
index 353063d..0000000
--- a/tajo-common/src/test/java/org/apache/tajo/util/TestTimeStampUtil.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.tajo.util;
-
-import org.joda.time.DateTimeZone;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
-import org.joda.time.DateTime;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-public class TestTimeStampUtil {
- private static final int TEST_YEAR = 2014;
- private static final int TEST_MONTH_OF_YEAR = 4;
- private static final int TEST_DAY_OF_MONTH = 18;
- private static final int TEST_HOUR_OF_DAY = 0;
- private static final int TEST_MINUTE_OF_HOUR = 15;
- private static final int TEST_SECOND_OF_MINUTE = 25;
- private static final DateTime TEST_DATETIME = new DateTime(TEST_YEAR, TEST_MONTH_OF_YEAR, TEST_DAY_OF_MONTH,
- TEST_HOUR_OF_DAY, TEST_MINUTE_OF_HOUR, TEST_SECOND_OF_MINUTE, DateTimeZone.UTC);
- private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
-
- @Test
- public void testGetYear() {
- assertEquals(DateTime.parse("2014-01-01 00:00:00", fmt.withZoneUTC()).getMillis() * 1000,
- TimeStampUtil.getYear(TEST_DATETIME));
- }
-
- @Test
- public void testGetMonth() {
- assertEquals(DateTime.parse("2014-04-01 00:00:00", fmt.withZoneUTC()).getMillis() * 1000,
- TimeStampUtil.getMonth(TEST_DATETIME));
- }
-
- @Test
- public void testGetDay() {
- assertEquals(DateTime.parse("2014-04-18 00:00:00", fmt.withZoneUTC()).getMillis() * 1000,
- TimeStampUtil.getDay(TEST_DATETIME));
- }
-
- @Test
- public void testGetHour() {
- assertEquals(DateTime.parse("2014-04-18 00:00:00",fmt.withZoneUTC()).getMillis() * 1000,
- TimeStampUtil.getHour(TEST_DATETIME));
- }
-
- @Test
- public void testGetMinute() {
- assertEquals(DateTime.parse("2014-04-18 00:15:00",fmt.withZoneUTC()).getMillis() * 1000,
- TimeStampUtil.getMinute(TEST_DATETIME));
- }
-
- @Test
- public void testGetSecond() {
- assertEquals(DateTime.parse("2014-04-18 00:15:25",fmt.withZoneUTC()).getMillis() * 1000,
- TimeStampUtil.getSecond(TEST_DATETIME));
- }
-}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromDate.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromDate.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromDate.java
index a010a7d..288fbe1 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromDate.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromDate.java
@@ -49,12 +49,12 @@ public class DatePartFromDate extends GeneralFunction {
@Override
public Datum eval(Tuple params) {
Datum target = params.get(0);
- DateDatum date = null;
if(target instanceof NullDatum || params.get(1) instanceof NullDatum) {
return NullDatum.get();
}
+ DateDatum date;
if(params.get(1) instanceof DateDatum) {
date = (DateDatum)(params.get(1));
} else {
@@ -124,8 +124,7 @@ public class DatePartFromDate extends GeneralFunction {
private class DowExtractorFromDate implements DatePartExtractorFromDate {
@Override
public Datum extract(DateDatum date) {
- Integer tdow = date.getDayOfWeek();
- return DatumFactory.createFloat8((double) ((tdow == 7) ? 0 : tdow));
+ return DatumFactory.createFloat8((double) date.getDayOfWeek());
}
}
@@ -139,7 +138,7 @@ public class DatePartFromDate extends GeneralFunction {
private class ISODowExtractorFromDate implements DatePartExtractorFromDate {
@Override
public Datum extract(DateDatum date) {
- return DatumFactory.createFloat8((double) date.getDayOfWeek());
+ return DatumFactory.createFloat8((double) date.getISODayOfWeek());
}
}
@@ -174,7 +173,7 @@ public class DatePartFromDate extends GeneralFunction {
private class WeekExtractorFromDate implements DatePartExtractorFromDate {
@Override
public Datum extract(DateDatum date) {
- return DatumFactory.createFloat8((double) date.getWeekOfWeekyear());
+ return DatumFactory.createFloat8((double) date.getWeekOfYear());
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTime.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTime.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTime.java
index 28e14fb..b3184e3 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTime.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTime.java
@@ -20,13 +20,20 @@ package org.apache.tajo.engine.function.datetime;
import org.apache.tajo.catalog.Column;
import org.apache.tajo.common.TajoDataTypes;
-import org.apache.tajo.datum.*;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.DatumFactory;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.datum.TimeDatum;
import org.apache.tajo.engine.function.GeneralFunction;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.datetime.DateTimeConstants;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
-import static org.apache.tajo.common.TajoDataTypes.Type.*;
+import static org.apache.tajo.common.TajoDataTypes.Type.FLOAT8;
+import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
@Description(
functionName = "date_part",
@@ -85,55 +92,57 @@ public class DatePartFromTime extends GeneralFunction {
}
}
- return extractor.extract(time);
+ TimeMeta tm = time.toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm);
+ return extractor.extract(tm);
}
private interface DatePartExtractorFromTime {
- public Datum extract(TimeDatum time);
+ public Datum extract(TimeMeta tm);
}
private class HourExtractorFromTime implements DatePartExtractorFromTime {
@Override
- public Datum extract(TimeDatum time) {
- return DatumFactory.createFloat8((double) time.getHourOfDay());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.hours);
}
}
private class MicrosecondsExtractorFromTime implements DatePartExtractorFromTime {
@Override
- public Datum extract(TimeDatum time) {
- return DatumFactory.createFloat8((double) (time.getSecondOfMinute() * 1000000 + time.getMillisOfSecond() * 1000));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (tm.secs * 1000000 + tm.fsecs));
}
}
private class MillisecondsExtractorFromTime implements DatePartExtractorFromTime {
@Override
- public Datum extract(TimeDatum time) {
- return DatumFactory.createFloat8((double) (time.getSecondOfMinute() * 1000 + time.getMillisOfSecond()));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (tm.secs * 1000 + tm.fsecs / 1000.0));
}
}
private class MinuteExtractorFromTime implements DatePartExtractorFromTime {
@Override
- public Datum extract(TimeDatum time) {
- return DatumFactory.createFloat8((double) time.getMinuteOfHour());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.minutes);
}
}
private class SecondExtractorFromTime implements DatePartExtractorFromTime {
@Override
- public Datum extract(TimeDatum time) {
- if (time.getMillisOfSecond() != 0) {
- return DatumFactory.createFloat8(time.getSecondOfMinute() + (((double) time.getMillisOfSecond()) / 1000));
+ public Datum extract(TimeMeta tm) {
+ if (tm.fsecs != 0) {
+ return DatumFactory.createFloat8(tm.secs + (((double) tm.fsecs) / (double)DateTimeConstants.USECS_PER_SEC));
} else {
- return DatumFactory.createFloat8((double) time.getSecondOfMinute());
+ return DatumFactory.createFloat8((double) tm.secs);
}
}
}
private class NullExtractorFromTime implements DatePartExtractorFromTime {
@Override
- public Datum extract(TimeDatum time) {
+ public Datum extract(TimeMeta tm) {
return NullDatum.get();
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTimestamp.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTimestamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTimestamp.java
index 3b46929..98900ef 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTimestamp.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DatePartFromTimestamp.java
@@ -25,6 +25,9 @@ import org.apache.tajo.engine.function.GeneralFunction;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.datetime.DateTimeConstants;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import static org.apache.tajo.common.TajoDataTypes.Type.*;
@@ -49,7 +52,7 @@ public class DatePartFromTimestamp extends GeneralFunction {
@Override
public Datum eval(Tuple params) {
Datum target = params.get(0);
- TimestampDatum timestamp = null;
+ TimestampDatum timestamp;
if(target instanceof NullDatum || params.get(1) instanceof NullDatum) {
return NullDatum.get();
@@ -111,147 +114,150 @@ public class DatePartFromTimestamp extends GeneralFunction {
}
}
- return extractor.extract(timestamp);
+ TimeMeta tm = timestamp.toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm);
+
+ return extractor.extract(tm);
}
private interface DatePartExtractorFromTimestamp {
- public Datum extract(TimestampDatum timestamp);
+ public Datum extract(TimeMeta tm);
}
private class CenturyExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getCenturyOfEra());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.getCenturyOfEra());
}
}
private class DayExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getDayOfMonth());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.dayOfMonth);
}
}
private class DecadeExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) (timestamp.getYear() / 10));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (tm.years / 10));
}
}
private class DowExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- Integer tdow = timestamp.getDayOfWeek();
+ public Datum extract(TimeMeta tm) {
+ Integer tdow = tm.getDayOfWeek();
return DatumFactory.createFloat8((double) ((tdow == 7) ? 0 : tdow));
}
}
private class DoyExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getDayOfYear());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double)tm.getDayOfYear());
}
}
private class EpochExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getUnixTime());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double)DateTimeUtil.julianTimeToEpoch(DateTimeUtil.toJulianTimestamp(tm)));
}
}
private class HourExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getHourOfDay());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.hours);
}
}
private class ISODowExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getDayOfWeek());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.getISODayOfWeek());
}
}
private class ISOYearExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getWeekyear());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.getWeekyear());
}
}
private class MicrosecondsExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) (timestamp.getSecondOfMinute() * 1000000 + timestamp.getMillisOfSecond() * 1000));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (tm.secs * 1000000 + tm.fsecs));
}
}
private class MillenniumExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) (((timestamp.getYear() - 1) / 1000) + 1));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (((tm.years - 1) / 1000) + 1));
}
}
private class MillisecondsExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) (timestamp.getSecondOfMinute() * 1000 + timestamp.getMillisOfSecond()));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (tm.secs * 1000 + tm.fsecs / 1000.0));
}
}
private class MinuteExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getMinuteOfHour());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.minutes);
}
}
private class MonthExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getMonthOfYear());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.monthOfYear);
}
}
private class QuarterExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) (((timestamp.getMonthOfYear() - 1) / 3) + 1));
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) (((tm.monthOfYear - 1) / 3) + 1));
}
}
private class SecondExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- if (timestamp.getMillisOfSecond() != 0) {
- return DatumFactory.createFloat8(timestamp.getSecondOfMinute() + (((double) timestamp.getMillisOfSecond()) / 1000));
+ public Datum extract(TimeMeta tm) {
+ if (tm.fsecs != 0) {
+ return DatumFactory.createFloat8(tm.secs + (((double) tm.fsecs) / (double) DateTimeConstants.USECS_PER_SEC));
} else {
- return DatumFactory.createFloat8((double) timestamp.getSecondOfMinute());
+ return DatumFactory.createFloat8((double) tm.secs);
}
}
}
private class WeekExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getWeekOfWeekyear());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.getWeekOfYear());
}
}
private class YearExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
- return DatumFactory.createFloat8((double) timestamp.getYear());
+ public Datum extract(TimeMeta tm) {
+ return DatumFactory.createFloat8((double) tm.years);
}
}
private class NullExtractorFromTimestamp implements DatePartExtractorFromTimestamp {
@Override
- public Datum extract(TimestampDatum timestamp) {
+ public Datum extract(TimeMeta tm) {
return NullDatum.get();
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
index 6aaded0..8705b06 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/DateTimePartFromUnixTimeStamp.java
@@ -25,117 +25,117 @@ import org.apache.tajo.engine.function.GeneralFunction;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.storage.Tuple;
-import org.apache.tajo.util.TimeStampUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
import org.joda.time.DateTime;
import static org.apache.tajo.common.TajoDataTypes.Type.*;
@Description(
- functionName = "utc_usec_to",
- description = "Extract field from time",
- example = "> SELECT utc_usec_to('day', 1274259481071200);\n"
- + "1274227200000000",
- returnType = TajoDataTypes.Type.INT8,
- paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT8}),
- @ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT8, TajoDataTypes.Type.INT4})}
+ functionName = "utc_usec_to",
+ description = "Extract field from time",
+ example = "> SELECT utc_usec_to('day', 1274259481071200);\n"
+ + "1274227200000000",
+ returnType = TajoDataTypes.Type.INT8,
+ paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT8}),
+ @ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT8, TajoDataTypes.Type.INT4})}
)
-public class DateTimePartFromUnixTimeStamp extends GeneralFunction {
+public class DateTimePartFromUnixTimestamp extends GeneralFunction {
- private DateTimePartExtractorFromUnixTime extractor = null;
- private WeekPartExtractorFromUnixTime weekExtractor = null;
+ private DateTimePartExtractorFromUnixTime extractor = null;
+ private WeekPartExtractorFromUnixTime weekExtractor = null;
- public DateTimePartFromUnixTimeStamp() {
- super(new Column[]{
- new Column("target", TEXT),
- new Column("source", INT8),
- new Column("dayOfWeek", INT4),
+ public DateTimePartFromUnixTimestamp() {
+ super(new Column[]{
+ new Column("target", TEXT),
+ new Column("source", INT8),
+ new Column("dayOfWeek", INT4),
- });
- }
+ });
+ }
- @Override
- public Datum eval(Tuple params) {
+ @Override
+ public Datum eval(Tuple params) {
- Datum target = params.get(0);
- DateTime dateTime;
- Int4Datum dayOfWeek = null;
+ Datum target = params.get(0);
+ DateTime dateTime;
+ Int4Datum dayOfWeek = null;
- if (target instanceof NullDatum || params.get(1) instanceof NullDatum) {
- return NullDatum.get();
- }
+ if (target instanceof NullDatum || params.get(1) instanceof NullDatum) {
+ return NullDatum.get();
+ }
- if (params.get(1) instanceof Int8Datum) {
- dateTime = TimeStampUtil.getUTCDateTime((Int8Datum) (params.get(1)));
- } else {
- return NullDatum.get();
- }
+ if (params.get(1) instanceof Int8Datum) {
+ dateTime = DateTimeUtil.getUTCDateTime((Int8Datum) (params.get(1)));
+ } else {
+ return NullDatum.get();
+ }
- if ( null == extractor || null == weekExtractor) {
-
- String extractType = target.asChars().toLowerCase();
-
- if (extractType.equals("day")) {
- extractor = new DayExtractorFromTime();
- } else if (extractType.equals("hour")) {
- extractor = new HourExtractorFromTime();
- } else if (extractType.equals("month")) {
- extractor = new MonthExtractorFromTime();
- } else if (extractType.equals("year")) {
- extractor = new YearExtractorFromTime();
- } else if (extractType.equals("week")) {
- if (params.get(2) instanceof NullDatum) {
- return NullDatum.get();
- }
- dayOfWeek = (Int4Datum) params.get(2);
- weekExtractor = new WeekExtractorFromTime();
- }
- }
+ if ( null == extractor || null == weekExtractor) {
- return null != weekExtractor ? weekExtractor.extract(dateTime, dayOfWeek.asInt4()) : extractor.extract(dateTime);
- }
+ String extractType = target.asChars().toLowerCase();
- private interface DateTimePartExtractorFromUnixTime {
- public Datum extract(DateTime dateTime);
+ if (extractType.equals("day")) {
+ extractor = new DayExtractorFromTime();
+ } else if (extractType.equals("hour")) {
+ extractor = new HourExtractorFromTime();
+ } else if (extractType.equals("month")) {
+ extractor = new MonthExtractorFromTime();
+ } else if (extractType.equals("year")) {
+ extractor = new YearExtractorFromTime();
+ } else if (extractType.equals("week")) {
+ if (params.get(2) instanceof NullDatum) {
+ return NullDatum.get();
+ }
+ dayOfWeek = (Int4Datum) params.get(2);
+ weekExtractor = new WeekExtractorFromTime();
+ }
}
- private interface WeekPartExtractorFromUnixTime {
- public Datum extract(DateTime dateTime, int week);
- }
+ return null != weekExtractor ? weekExtractor.extract(dateTime, dayOfWeek.asInt4()) : extractor.extract(dateTime);
+ }
- private class DayExtractorFromTime implements DateTimePartExtractorFromUnixTime {
- @Override
- public Datum extract(DateTime dateTime) {
- return DatumFactory.createInt8(TimeStampUtil.getDay(dateTime));
- }
+ private interface DateTimePartExtractorFromUnixTime {
+ public Datum extract(DateTime dateTime);
+ }
+
+ private interface WeekPartExtractorFromUnixTime {
+ public Datum extract(DateTime dateTime, int week);
+ }
+
+ private class DayExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(DateTimeUtil.getDay(dateTime));
}
+ }
- private class HourExtractorFromTime implements DateTimePartExtractorFromUnixTime {
- @Override
- public Datum extract(DateTime dateTime) {
- return DatumFactory.createInt8(TimeStampUtil.getHour(dateTime));
- }
+ private class HourExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(DateTimeUtil.getHour(dateTime));
}
+ }
- private class MonthExtractorFromTime implements DateTimePartExtractorFromUnixTime {
- @Override
- public Datum extract(DateTime dateTime) {
- return DatumFactory.createInt8(TimeStampUtil.getMonth(dateTime));
- }
+ private class MonthExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(DateTimeUtil.getMonth(dateTime));
}
+ }
- private class YearExtractorFromTime implements DateTimePartExtractorFromUnixTime {
- @Override
- public Datum extract(DateTime dateTime) {
- return DatumFactory.createInt8(TimeStampUtil.getYear(dateTime));
- }
+ private class YearExtractorFromTime implements DateTimePartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime) {
+ return DatumFactory.createInt8(DateTimeUtil.getYear(dateTime));
}
+ }
- private class WeekExtractorFromTime implements WeekPartExtractorFromUnixTime {
- @Override
- public Datum extract(DateTime dateTime , int week) {
- return DatumFactory.createInt8(TimeStampUtil.getDayOfWeek(dateTime,week));
- }
+ private class WeekExtractorFromTime implements WeekPartExtractorFromUnixTime {
+ @Override
+ public Datum extract(DateTime dateTime , int week) {
+ return DatumFactory.createInt8(DateTimeUtil.getDayOfWeek(dateTime,week));
}
+ }
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToCharTimestamp.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToCharTimestamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToCharTimestamp.java
index 2a74ff5..4ad76c4 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToCharTimestamp.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToCharTimestamp.java
@@ -29,39 +29,33 @@ import org.apache.tajo.engine.function.GeneralFunction;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.storage.Tuple;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
-import static org.apache.tajo.common.TajoDataTypes.Type.INT8;
import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+import static org.apache.tajo.common.TajoDataTypes.Type.TIMESTAMP;
@Description(
functionName = "to_char",
- description = "Convert time stamp to string",
- example = "> SELECT to_char(1389071652, 'yyyy-MM');\n"
+ description = "Convert time stamp to string. Format should be a SQL standard format string.",
+ example = "> SELECT to_char(TIMESTAMP '2014-01-17 10:09:37', 'YYYY-MM');\n"
+ "2014-01",
returnType = TajoDataTypes.Type.TEXT,
paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TIMESTAMP, TajoDataTypes.Type.TEXT})}
)
public class ToCharTimestamp extends GeneralFunction {
- private boolean constantFormat;
- private DateTimeFormatter formatter;
-
public ToCharTimestamp() {
super(new Column[] {
- new Column("timestamp", INT8),
+ new Column("timestamp", TIMESTAMP),
new Column("format", TEXT)
});
}
@Override
public void init(FunctionEval.ParamType[] paramTypes) {
- if (paramTypes[1] == FunctionEval.ParamType.CONSTANT) {
- constantFormat = true;
- }
}
-
@Override
public Datum eval(Tuple params) {
if(params.isNull(0) || params.isNull(1)) {
@@ -69,11 +63,11 @@ public class ToCharTimestamp extends GeneralFunction {
}
TimestampDatum valueDatum = (TimestampDatum) params.get(0);
+ TimeMeta tm = valueDatum.toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm);
+
Datum pattern = params.get(1);
- if (formatter == null || !constantFormat) {
- formatter = DateTimeFormat.forPattern(pattern.asChars());
- }
- return DatumFactory.createText(valueDatum.toChars(formatter));
+ return DatumFactory.createText(DateTimeFormat.to_char(tm, pattern.asChars()));
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToDate.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToDate.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToDate.java
index ba6a020..09a4395 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToDate.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToDate.java
@@ -27,24 +27,19 @@ import org.apache.tajo.engine.function.GeneralFunction;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.storage.Tuple;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
@Description(
functionName = "to_date",
- description = "Convert string to date. Format should be a java format string.",
- example = "> SELECT to_date('2014-01-01', 'yyyy-MM-dd');\n"
+ description = "Convert string to date. Format should be a SQL standard format string.",
+ example = "> SELECT to_date('2014-01-01', 'YYYY-MM-DD');\n"
+ "2014-01-01",
returnType = TajoDataTypes.Type.DATE,
paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.TEXT})}
)
public class ToDate extends GeneralFunction {
- private static Map<String, DateTimeFormatter> formattercCache =
- new ConcurrentHashMap<String, DateTimeFormatter>();
-
public ToDate() {
super(new Column[]{
new Column("string", TajoDataTypes.Type.TEXT),
@@ -60,12 +55,8 @@ public class ToDate extends GeneralFunction {
String value = params.get(0).asChars();
String pattern = params.get(1).asChars();
- DateTimeFormatter formatter = formattercCache.get(pattern);
- if (formatter == null) {
- formatter = DateTimeFormat.forPattern(pattern);
- formattercCache.put(pattern, formatter);
- }
+ TimeMeta tm = DateTimeFormat.parseDateTime(value, pattern);
- return new DateDatum(formatter.parseDateTime(value).toLocalDate());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestamp.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestamp.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestamp.java
deleted file mode 100644
index 1cf6870..0000000
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestamp.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.tajo.engine.function.datetime;
-
-import org.apache.tajo.catalog.Column;
-import org.apache.tajo.common.TajoDataTypes;
-import org.apache.tajo.datum.Datum;
-import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.datum.NullDatum;
-import org.apache.tajo.engine.function.GeneralFunction;
-import org.apache.tajo.engine.function.annotation.Description;
-import org.apache.tajo.engine.function.annotation.ParamTypes;
-import org.apache.tajo.storage.Tuple;
-
-import static org.apache.tajo.common.TajoDataTypes.Type.INT4;
-
-@Description(
- functionName = "to_timestamp",
- description = "Convert UNIX epoch to time stamp",
- example = "> SELECT to_timestamp(1389071574);\n"
- + "2014-01-07 14:12:54",
- returnType = TajoDataTypes.Type.TIMESTAMP,
- paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.INT4}),
- @ParamTypes(paramTypes = {TajoDataTypes.Type.INT8})}
-)
-public class ToTimestamp extends GeneralFunction {
- public ToTimestamp() {
- super(new Column[] {new Column("timestamp", INT4)});
- }
-
- @Override
- public Datum eval(Tuple params) {
- Datum value = params.get(0);
- if (value instanceof NullDatum) {
- return NullDatum.get();
- }
- return DatumFactory.createTimeStamp(value.asInt4());
- }
-}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampInt.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampInt.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampInt.java
new file mode 100644
index 0000000..d14cfd6
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampInt.java
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.engine.function.datetime;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.datum.Datum;
+import org.apache.tajo.datum.DatumFactory;
+import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.engine.function.GeneralFunction;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.storage.Tuple;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.INT4;
+
+@Description(
+ functionName = "to_timestamp",
+ description = "Convert UNIX epoch to time stamp",
+ example = "> SELECT to_timestamp(1389071574);\n"
+ + "2014-01-07 14:12:54",
+ returnType = TajoDataTypes.Type.TIMESTAMP,
+ paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.INT4}),
+ @ParamTypes(paramTypes = {TajoDataTypes.Type.INT8})}
+)
+public class ToTimestampInt extends GeneralFunction {
+ public ToTimestampInt() {
+ super(new Column[] {new Column("timestamp", INT4)});
+ }
+
+ @Override
+ public Datum eval(Tuple params) {
+ Datum value = params.get(0);
+ if (value instanceof NullDatum) {
+ return NullDatum.get();
+ }
+ return DatumFactory.createTimestmpDatumWithUnixTime(value.asInt4());
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampText.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampText.java b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampText.java
new file mode 100644
index 0000000..f42a171
--- /dev/null
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/datetime/ToTimestampText.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.engine.function.datetime;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.datum.*;
+import org.apache.tajo.engine.function.GeneralFunction;
+import org.apache.tajo.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.storage.Tuple;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+
+@Description(
+ functionName = "to_timestamp",
+ description = "Convert string to time stamp",
+ detail = "Patterns for Date/Time Formatting: http://www.postgresql.org/docs/8.4/static/functions-formatting.html",
+ example = "> select to_timestamp('05 Dec 2000 15:12:02.020', 'DD Mon YYYY HH24:MI:SS.MS');\n"
+ + "2000-12-05 15:12:02.02",
+ returnType = TajoDataTypes.Type.TIMESTAMP,
+ paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.TEXT})}
+)
+public class ToTimestampText extends GeneralFunction {
+ public ToTimestampText() {
+ super(new Column[]{new Column("DateTimeText", TEXT), new Column("Pattern", TEXT)});
+ }
+
+ @Override
+ public Datum eval(Tuple params) {
+ if(params.isNull(0) || params.isNull(1)) {
+ return NullDatum.get();
+ }
+
+ TextDatum dateTimeTextDatum = (TextDatum) params.get(0);
+ TextDatum patternDatum = (TextDatum) params.get(1);
+
+ TimeMeta tm = DateTimeFormat.parseDateTime(dateTimeTextDatum.asChars(), patternDatum.asChars());
+ DateTimeUtil.toUTCTimezone(tm);
+
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/planner/ExprAnnotator.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/planner/ExprAnnotator.java b/tajo-core/src/main/java/org/apache/tajo/engine/planner/ExprAnnotator.java
index e74fd70..ba80c25 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/planner/ExprAnnotator.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/planner/ExprAnnotator.java
@@ -34,7 +34,8 @@ import org.apache.tajo.engine.planner.logical.NodeType;
import org.apache.tajo.exception.InternalException;
import org.apache.tajo.util.Pair;
import org.apache.tajo.util.TUtil;
-import org.joda.time.DateTime;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.util.Map;
import java.util.Stack;
@@ -611,7 +612,7 @@ public class ExprAnnotator extends BaseAlgebraVisitor<ExprAnnotator.Context, Eva
FunctionDesc countRows = catalog.getFunction("count", CatalogProtos.FunctionType.AGGREGATION,
new DataType[] {});
if (countRows == null) {
- throw new NoSuchFunctionException(countRows.getSignature(), new DataType[]{});
+ throw new NoSuchFunctionException(expr.getSignature(), new DataType[]{});
}
try {
@@ -704,8 +705,16 @@ public class ExprAnnotator extends BaseAlgebraVisitor<ExprAnnotator.Context, Eva
@Override
public EvalNode visitDateLiteral(Context context, Stack<Expr> stack, DateLiteral expr) throws PlanningException {
DateValue dateValue = expr.getDate();
- int [] dates = dateToIntArray(dateValue.getYears(), dateValue.getMonths(), dateValue.getDays());
- return new ConstEval(new DateDatum(dates[0], dates[1], dates[2]));
+ int[] dates = dateToIntArray(dateValue.getYears(), dateValue.getMonths(), dateValue.getDays());
+
+ TimeMeta tm = new TimeMeta();
+ tm.years = dates[0];
+ tm.monthOfYear = dates[1];
+ tm.dayOfMonth = dates[2];
+
+ DateTimeUtil.j2date(DateTimeUtil.date2j(dates[0], dates[1], dates[2]), tm);
+
+ return new ConstEval(new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth)));
}
@Override
@@ -721,14 +730,20 @@ public class ExprAnnotator extends BaseAlgebraVisitor<ExprAnnotator.Context, Eva
timeValue.getMinutes(),
timeValue.getSeconds(),
timeValue.getSecondsFraction());
- DateTime dateTime;
+
+ long timestamp;
if (timeValue.hasSecondsFraction()) {
- dateTime = new DateTime(dates[0], dates[1], dates[2], times[0], times[1], times[2], times[3]);
+ timestamp = DateTimeUtil.toJulianTimestamp(dates[0], dates[1], dates[2], times[0], times[1], times[2],
+ times[3] * 1000);
} else {
- dateTime = new DateTime(dates[0], dates[1], dates[2], times[0], times[1], times[2]);
+ timestamp = DateTimeUtil.toJulianTimestamp(dates[0], dates[1], dates[2], times[0], times[1], times[2], 0);
}
- return new ConstEval(new TimestampDatum(dateTime));
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.toJulianTimeMeta(timestamp, tm);
+ DateTimeUtil.toUTCTimezone(tm);
+
+ return new ConstEval(new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm)));
}
@Override
@@ -744,13 +759,17 @@ public class ExprAnnotator extends BaseAlgebraVisitor<ExprAnnotator.Context, Eva
timeValue.getSeconds(),
timeValue.getSecondsFraction());
- TimeDatum datum;
+ long time;
if (timeValue.hasSecondsFraction()) {
- datum = new TimeDatum(times[0], times[1], times[2], times[3]);
+ time = DateTimeUtil.toTime(times[0], times[1], times[2], times[3] * 1000);
} else {
- datum = new TimeDatum(times[0], times[1], times[2]);
+ time = DateTimeUtil.toTime(times[0], times[1], times[2], 0);
}
- return new ConstEval(datum);
+ TimeDatum timeDatum = new TimeDatum(time);
+ TimeMeta tm = timeDatum.toTimeMeta();
+ DateTimeUtil.toUTCTimezone(tm);
+
+ return new ConstEval(new TimeDatum(DateTimeUtil.toTime(tm)));
}
public static int [] dateToIntArray(String years, String months, String days)
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/main/java/org/apache/tajo/engine/planner/UniformRangePartition.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/planner/UniformRangePartition.java b/tajo-core/src/main/java/org/apache/tajo/engine/planner/UniformRangePartition.java
index f6922ed..88cb061 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/planner/UniformRangePartition.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/planner/UniformRangePartition.java
@@ -422,10 +422,10 @@ public class UniformRangePartition extends RangePartitionAlgorithm {
break;
case TIMESTAMP:
if (overflowFlag[i]) {
- end.put(i, DatumFactory.createTimeStampFromMillis(
+ end.put(i, DatumFactory.createTimestmpDatumWithJavaMillis(
range.getStart().get(i).asInt8() + incs[i].longValue()));
} else {
- end.put(i, DatumFactory.createTimeStampFromMillis(last.get(i).asInt8() + incs[i].longValue()));
+ end.put(i, DatumFactory.createTimestmpDatumWithJavaMillis(last.get(i).asInt8() + incs[i].longValue()));
}
break;
case INET4:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java b/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java
index bab6ec7..0742a80 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/eval/ExprTestBase.java
@@ -26,8 +26,9 @@ import org.apache.tajo.catalog.proto.CatalogProtos;
import org.apache.tajo.cli.InvalidStatementException;
import org.apache.tajo.cli.ParsedResult;
import org.apache.tajo.cli.SimpleParser;
-import org.apache.tajo.datum.NullDatum;
-import org.apache.tajo.datum.TextDatum;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.conf.TajoConf;
+import org.apache.tajo.datum.*;
import org.apache.tajo.engine.json.CoreGsonHelper;
import org.apache.tajo.engine.parser.SQLAnalyzer;
import org.apache.tajo.engine.planner.*;
@@ -39,6 +40,7 @@ import org.apache.tajo.storage.Tuple;
import org.apache.tajo.storage.VTuple;
import org.apache.tajo.util.Bytes;
import org.apache.tajo.util.CommonTestingUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
import org.apache.tajo.util.KeyValueSet;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -60,6 +62,10 @@ public class ExprTestBase {
private static LogicalOptimizer optimizer;
private static LogicalPlanVerifier annotatedPlanVerifier;
+ public static String getUserTimeZoneDisplay() {
+ return DateTimeUtil.getTimeZoneDisplayTime(TajoConf.getCurrentTimeZone());
+ }
+
@BeforeClass
public static void setUp() throws Exception {
util = new TajoTestingCluster();
@@ -190,7 +196,16 @@ public class ExprTestBase {
}
for (int i = 0; i < expected.length; i++) {
- assertEquals(query, expected[i], outTuple.get(i).asChars());
+ Datum datum = outTuple.get(i);
+ String outTupleAsChars;
+ if (datum.type() == Type.TIMESTAMP) {
+ outTupleAsChars = ((TimestampDatum) datum).asChars(TajoConf.getCurrentTimeZone(), true);
+ } else if (datum.type() == Type.TIME) {
+ outTupleAsChars = ((TimeDatum) datum).asChars(TajoConf.getCurrentTimeZone(), true);
+ } else {
+ outTupleAsChars = datum.asChars();
+ }
+ assertEquals(query, expected[i], outTupleAsChars);
}
} catch (InvalidStatementException e) {
assertFalse(e.getMessage(), true);
[4/6] TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
Posted by ji...@apache.org.
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeFormat.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeFormat.java b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeFormat.java
new file mode 100644
index 0000000..bc8435b
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeFormat.java
@@ -0,0 +1,2143 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.util.datetime;
+
+import org.apache.tajo.datum.TimestampDatum;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * This class originated from src/backend/utils/adt/formatting.c of PostgreSQL
+ */
+public class DateTimeFormat {
+ /* ----------
+ * Full months_short
+ * ----------
+ */
+ static final String[] months_full = {
+ "January", "February", "March", "April", "May", "June", "July",
+ "August", "September", "October", "November", "December", null
+ };
+
+ static String[] days_short = {
+ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", null
+ };
+
+ static String[] months_short = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", null};
+
+ static String[] days_full = {"Sunday", "Monday", "Tuesday", "Wednesday",
+ "Thursday", "Friday", "Saturday", null};
+
+ static int[][] ysum = {
+ {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
+ {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
+ };
+
+
+ /**
+ * AD / BC
+ * ----------
+ * There is no 0 AD. Years go from 1 BC to 1 AD, so we make it
+ * positive and map year == -1 to year zero, and shift all negative
+ * years up one. For interval years, we just return the year.
+ * @param year
+ * @param is_interval
+ * @return
+ */
+ static int ADJUST_YEAR(int year, boolean is_interval) {
+ return ((is_interval) ? (year) : ((year) <= 0 ? -((year) - 1) : (year)));
+ }
+
+ static final String A_D_STR = "A.D.";
+ static final String a_d_STR = "a.d.";
+ static final String AD_STR = "AD";
+ static final String ad_STR = "ad";
+
+ static final String B_C_STR = "B.C.";
+ static final String b_c_STR = "b.c.";
+ static final String BC_STR = "BC";
+ static final String bc_STR = "bc";
+
+ /**
+ * AD / BC strings for seq_search.
+ *
+ * These are given in two variants, a long form with periods and a standard
+ * form without.
+ *
+ * The array is laid out such that matches for AD have an even index, and
+ * matches for BC have an odd index. So the boolean value for BC is given by
+ * taking the array index of the match, modulo 2.
+ */
+ static final String[] adbc_strings = {ad_STR, bc_STR, AD_STR, BC_STR, null};
+ static final String[] adbc_strings_long = {a_d_STR, b_c_STR, A_D_STR, B_C_STR, null};
+
+ /**
+ * ----------
+ * AM / PM
+ * ----------
+ */
+ static final String A_M_STR = "A.M.";
+ static final String a_m_STR = "a.m.";
+ static final String AM_STR = "AM";
+ static final String am_STR = "am";
+
+ static final String P_M_STR = "P.M.";
+ static final String p_m_STR = "p.m.";
+ static final String PM_STR = "PM";
+ static final String pm_STR = "pm";
+
+ /**
+ * AM / PM strings for seq_search.
+ *
+ * These are given in two variants, a long form with periods and a standard
+ * form without.
+ *
+ * The array is laid out such that matches for AM have an even index, and
+ * matches for PM have an odd index. So the boolean value for PM is given by
+ * taking the array index of the match, modulo 2.
+ */
+ static final String[] ampm_strings = {am_STR, pm_STR, AM_STR, PM_STR, null};
+ static final String[] ampm_strings_long = {a_m_STR, p_m_STR, A_M_STR, P_M_STR, null};
+
+ /**
+ * ----------
+ * Months in roman-numeral
+ * (Must be in reverse order for seq_search (in FROM_CHAR), because
+ * 'VIII' must have higher precedence than 'V')
+ * ----------
+ */
+ static final String[] rm_months_upper =
+ {"XII", "XI", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I", null};
+
+ static final String[] rm_months_lower =
+ {"xii", "xi", "x", "ix", "viii", "vii", "vi", "v", "iv", "iii", "ii", "i", null};
+
+ /**
+ * ----------
+ * Roman numbers
+ * ----------
+ */
+ static final String[] rm1 = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", null};
+ static final String[] rm10 = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", null};
+ static final String[] rm100 = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", null};
+
+ /**
+ * ----------
+ * Ordinal postfixes
+ * ----------
+ */
+ static final String[] numTH = {"ST", "ND", "RD", "TH", null};
+ static final String[] numth = {"st", "nd", "rd", "th", null};
+
+ /**
+ * ----------
+ * Flags & Options:
+ * ----------
+ */
+ static final int ONE_UPPER = 1; /* Name */
+ static final int ALL_UPPER = 2; /* NAME */
+ static final int ALL_LOWER = 3; /* name */
+
+ static final int MAX_MONTH_LEN = 9;
+ static final int MAX_MON_LEN = 3;
+ static final int MAX_DAY_LEN = 9;
+ static final int MAX_DY_LEN = 3;
+ static final int MAX_RM_LEN = 4;
+
+ static final int DCH_S_FM = 0x01;
+ static final int DCH_S_TH = 0x02;
+ static final int DCH_S_th = 0x04;
+ static final int DCH_S_SP = 0x08;
+ static final int DCH_S_TM = 0x10;
+
+ static final int NODE_TYPE_END = 1;
+ static final int NODE_TYPE_ACTION = 2;
+ static final int NODE_TYPE_CHAR = 3;
+
+ static final int SUFFTYPE_PREFIX = 1;
+ static final int SUFFTYPE_POSTFIX = 2;
+
+ static final int CLOCK_24_HOUR = 0;
+ static final int CLOCK_12_HOUR = 1;
+
+ static final int MONTHS_PER_YEAR = 12;
+ static final int HOURS_PER_DAY = 24;
+
+ /**
+ * ----------
+ * Maximal length of one node
+ * ----------
+ */
+ static final int DCH_MAX_ITEM_SIZ = 9; /* max julian day */
+ static final int NUM_MAX_ITEM_SIZ = 8; /* roman number (RN has 15 chars) */
+
+ enum FORMAT_TYPE {
+ DCH_TYPE, NUM_TYPE
+ }
+
+ /**
+ * ----------
+ * Suffixes definition for DATE-TIME TO/FROM CHAR
+ * ----------
+ */
+ static KeySuffix[] DCH_suff = {
+ new KeySuffix("FM", 2, DCH_S_FM, SUFFTYPE_PREFIX),
+ new KeySuffix("fm", 2, DCH_S_FM, SUFFTYPE_PREFIX),
+ new KeySuffix("TM", 2, DCH_S_TM, SUFFTYPE_PREFIX),
+ new KeySuffix("tm", 2, DCH_S_TM, SUFFTYPE_PREFIX),
+ new KeySuffix("TH", 2, DCH_S_TH, SUFFTYPE_POSTFIX),
+ new KeySuffix("th", 2, DCH_S_th, SUFFTYPE_POSTFIX),
+ new KeySuffix("SP", 2, DCH_S_SP, SUFFTYPE_POSTFIX),
+ };
+
+ /**
+ * ----------
+ * Format-pictures (KeyWord).
+ *
+ * The KeyWord field; alphabetic sorted, *BUT* strings alike is sorted
+ * complicated -to-> easy:
+ *
+ * (example: "DDD","DD","Day","D" )
+ *
+ * (this specific sort needs the algorithm for sequential search for strings,
+ * which not has exact end; -> How keyword is in "HH12blabla" ? - "HH"
+ * or "HH12"? You must first try "HH12", because "HH" is in string, but
+ * it is not good.
+ *
+ * (!)
+ * - Position for the keyword is similar as position in the enum DCH/NUM_poz.
+ * (!)
+ *
+ * For fast search is used the 'int index[]', index is ascii table from position
+ * 32 (' ') to 126 (~), in this index is DCH_ / NUM_ enums for each ASCII
+ * position or -1 if char is not used in the KeyWord. Search example for
+ * string "MM":
+ * 1) see in index to index['M' - 32],
+ * 2) take keywords position (enum DCH_MI) from index
+ * 3) run sequential search in keywords[] from this position
+ *
+ * ----------
+ */
+ enum DCH_poz {
+ DCH_A_D(0),
+ DCH_A_M(1),
+ DCH_AD(2),
+ DCH_AM(3),
+ DCH_B_C(4),
+ DCH_BC(5),
+ DCH_CC(6),
+ DCH_DAY(7),
+ DCH_DDD(8),
+ DCH_DD(9),
+
+ DCH_DY(10),
+ DCH_Day(11),
+ DCH_Dy(12),
+ DCH_D(13),
+ DCH_FX(14), /* global suffix */
+ DCH_HH24(15),
+ DCH_HH12(16),
+ DCH_HH(17),
+ DCH_IDDD(18),
+ DCH_ID(19),
+
+ DCH_IW(20),
+ DCH_IYYY(21),
+ DCH_IYY(22),
+ DCH_IY(23),
+ DCH_I(24),
+ DCH_J(25),
+ DCH_MI(26),
+ DCH_MM(27),
+ DCH_MONTH(28),
+ DCH_MON(29),
+
+ DCH_MS(30),
+ DCH_Month(31),
+ DCH_Mon(32),
+ DCH_P_M(33),
+ DCH_PM(34),
+ DCH_Q(35),
+ DCH_RM(36),
+ DCH_SSSS(37),
+ DCH_SS(38),
+ DCH_TZ(39),
+
+ DCH_US(40),
+ DCH_WW(41),
+ DCH_W(42),
+ DCH_Y_YYY(43),
+ DCH_YYYY(44),
+ DCH_YYY(45),
+ DCH_YY(46),
+ DCH_Y(47),
+ DCH_a_d(48),
+ DCH_a_m(49),
+
+ DCH_ad(50),
+ DCH_am(51),
+ DCH_b_c(52),
+ DCH_bc(53),
+ DCH_cc(54),
+ DCH_day(55),
+ DCH_ddd(56),
+ DCH_dd(57),
+ DCH_dy(58),
+ DCH_d(59),
+
+ DCH_fx(60),
+ DCH_hh24(61),
+ DCH_hh12(62),
+ DCH_hh(63),
+ DCH_iddd(64),
+ DCH_id(65),
+ DCH_iw(66),
+ DCH_iyyy(67),
+ DCH_iyy(68),
+ DCH_iy(69),
+
+ DCH_i(70),
+ DCH_j(71),
+ DCH_mi(72),
+ DCH_mm(73),
+ DCH_month(74),
+ DCH_mon(75),
+ DCH_ms(76),
+ DCH_p_m(77),
+ DCH_pm(78),
+ DCH_q(79),
+
+ DCH_rm(80),
+ DCH_ssss(89),
+ DCH_ss(90),
+ DCH_tz(91),
+ DCH_us(92),
+ DCH_ww(93),
+ DCH_w(94),
+ DCH_y_yyy(95),
+ DCH_yyyy(96),
+ DCH_yyy(97),
+ DCH_yy(98),
+ DCH_y(99),
+ _DCH_last_(Integer.MAX_VALUE);
+
+ int value;
+ DCH_poz(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return value;
+ }
+ }
+
+ /**
+ * ----------
+ * FromCharDateMode
+ * ----------
+ *
+ * This value is used to nominate one of several distinct (and mutually
+ * exclusive) date conventions that a keyword can belong to.
+ */
+ enum FromCharDateMode
+ {
+ FROM_CHAR_DATE_NONE, /* Value does not affect date mode. */
+ FROM_CHAR_DATE_GREGORIAN, /* Gregorian (day, month, year) style date */
+ FROM_CHAR_DATE_ISOWEEK /* ISO 8601 week date */
+ }
+
+ /**
+ * ----------
+ * KeyWords for DATE-TIME version
+ * ----------
+ */
+ static final Object[][] DCH_keywordValues = {
+ /* name, len, id, is_digit, date_mode */
+ {"A.D.", 4, DCH_poz.DCH_A_D, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* A */
+ {"A.M.", 4, DCH_poz.DCH_A_M, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"AD", 2, DCH_poz.DCH_AD, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"AM", 2, DCH_poz.DCH_AM, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"B.C.", 4, DCH_poz.DCH_B_C, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* B */
+ {"BC", 2, DCH_poz.DCH_BC, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"CC", 2, DCH_poz.DCH_CC, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* C */
+ {"DAY", 3, DCH_poz.DCH_DAY, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* D */
+ {"DDD", 3, DCH_poz.DCH_DDD, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"DD", 2, DCH_poz.DCH_DD, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"DY", 2, DCH_poz.DCH_DY, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"Day", 3, DCH_poz.DCH_Day, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"Dy", 2, DCH_poz.DCH_Dy, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"D", 1, DCH_poz.DCH_D, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"FX", 2, DCH_poz.DCH_FX, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* F */
+ {"HH24", 4, DCH_poz.DCH_HH24, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* H */
+ {"HH12", 4, DCH_poz.DCH_HH12, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"HH", 2, DCH_poz.DCH_HH, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"IDDD", 4, DCH_poz.DCH_IDDD, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK}, /* I */
+ {"ID", 2, DCH_poz.DCH_ID, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"IW", 2, DCH_poz.DCH_IW, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"IYYY", 4, DCH_poz.DCH_IYYY, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"IYY", 3, DCH_poz.DCH_IYY, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"IY", 2, DCH_poz.DCH_IY, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"I", 1, DCH_poz.DCH_I, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"J", 1, DCH_poz.DCH_J, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* J */
+ {"MI", 2, DCH_poz.DCH_MI, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* M */
+ {"MM", 2, DCH_poz.DCH_MM, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"MONTH", 5, DCH_poz.DCH_MONTH, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"MON", 3, DCH_poz.DCH_MON, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"MS", 2, DCH_poz.DCH_MS, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"Month", 5, DCH_poz.DCH_Month, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"Mon", 3, DCH_poz.DCH_Mon, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"P.M.", 4, DCH_poz.DCH_P_M, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* P */
+ {"PM", 2, DCH_poz.DCH_PM, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"Q", 1, DCH_poz.DCH_Q, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* Q */
+ {"RM", 2, DCH_poz.DCH_RM, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}, /* R */
+ {"SSSS", 4, DCH_poz.DCH_SSSS, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* S */
+ {"SS", 2, DCH_poz.DCH_SS, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"TZ", 2, DCH_poz.DCH_TZ, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* T */
+ {"US", 2, DCH_poz.DCH_US, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* U */
+ {"WW", 2, DCH_poz.DCH_WW, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}, /* W */
+ {"W", 1, DCH_poz.DCH_W, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"Y,YYY", 5, DCH_poz.DCH_Y_YYY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}, /* Y */
+ {"YYYY", 4, DCH_poz.DCH_YYYY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"YYY", 3, DCH_poz.DCH_YYY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"YY", 2, DCH_poz.DCH_YY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"Y", 1, DCH_poz.DCH_Y, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"a.d.", 4, DCH_poz.DCH_a_d, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* a */
+ {"a.m.", 4, DCH_poz.DCH_a_m, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"ad", 2, DCH_poz.DCH_ad, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"am", 2, DCH_poz.DCH_am, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"b.c.", 4, DCH_poz.DCH_b_c, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* b */
+ {"bc", 2, DCH_poz.DCH_bc, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"cc", 2, DCH_poz.DCH_CC, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* c */
+ {"day", 3, DCH_poz.DCH_day, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* d */
+ {"ddd", 3, DCH_poz.DCH_DDD, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"dd", 2, DCH_poz.DCH_DD, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"dy", 2, DCH_poz.DCH_dy, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"d", 1, DCH_poz.DCH_D, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"fx", 2, DCH_poz.DCH_FX, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* f */
+ {"hh24", 4, DCH_poz.DCH_HH24, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* h */
+ {"hh12", 4, DCH_poz.DCH_HH12, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"hh", 2, DCH_poz.DCH_HH, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"iddd", 4, DCH_poz.DCH_IDDD, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK}, /* i */
+ {"id", 2, DCH_poz.DCH_ID, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"iw", 2, DCH_poz.DCH_IW, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"iyyy", 4, DCH_poz.DCH_IYYY, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"iyy", 3, DCH_poz.DCH_IYY, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"iy", 2, DCH_poz.DCH_IY, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"i", 1, DCH_poz.DCH_I, true, FromCharDateMode.FROM_CHAR_DATE_ISOWEEK},
+ {"j", 1, DCH_poz.DCH_J, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* j */
+ {"mi", 2, DCH_poz.DCH_MI, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* m */
+ {"mm", 2, DCH_poz.DCH_MM, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"month", 5, DCH_poz.DCH_month, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"mon", 3, DCH_poz.DCH_mon, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"ms", 2, DCH_poz.DCH_MS, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"p.m.", 4, DCH_poz.DCH_p_m, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* p */
+ {"pm", 2, DCH_poz.DCH_pm, false, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"q", 1, DCH_poz.DCH_Q, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* q */
+ {"rm", 2, DCH_poz.DCH_rm, false, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}, /* r */
+ {"ssss", 4, DCH_poz.DCH_SSSS, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* s */
+ {"ss", 2, DCH_poz.DCH_SS, true, FromCharDateMode.FROM_CHAR_DATE_NONE},
+ {"tz", 2, DCH_poz.DCH_tz, false, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* t */
+ {"us", 2, DCH_poz.DCH_US, true, FromCharDateMode.FROM_CHAR_DATE_NONE}, /* u */
+ {"ww", 2, DCH_poz.DCH_WW, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}, /* w */
+ {"w", 1, DCH_poz.DCH_W, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"y,yyy", 5, DCH_poz.DCH_Y_YYY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}, /* y */
+ {"yyyy", 4, DCH_poz.DCH_YYYY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"yyy", 3, DCH_poz.DCH_YYY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"yy", 2, DCH_poz.DCH_YY, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN},
+ {"y", 1, DCH_poz.DCH_Y, true, FromCharDateMode.FROM_CHAR_DATE_GREGORIAN}
+ };
+
+ static final KeyWord[] DCH_keywords = new KeyWord[DCH_keywordValues.length];
+
+ static Map<Character, Integer> DCH_index = new HashMap<Character, Integer>();
+
+ static {
+ int index = 0;
+ for(Object[] eachKeywordValue: DCH_keywordValues) {
+ KeyWord keyword = new KeyWord();
+ keyword.name = (String)eachKeywordValue[0];
+ keyword.len = ((Integer)eachKeywordValue[1]).intValue();
+ keyword.id = ((DCH_poz)eachKeywordValue[2]).getValue();
+ keyword.idType = ((DCH_poz)eachKeywordValue[2]);
+ keyword.is_digit = ((Boolean)eachKeywordValue[3]).booleanValue();
+ keyword.date_mode = (FromCharDateMode)eachKeywordValue[4];
+
+ Character c = new Character(keyword.name.charAt(0));
+ Integer pos = DCH_index.get(c);
+ if (pos == null) {
+ DCH_index.put(c, index);
+ }
+ DCH_keywords[index++] = keyword;
+ }
+ }
+
+ /**
+ * ----------
+ * Format parser structs
+ * ----------
+ */
+ static class KeySuffix {
+ String name; /* suffix string */
+ int len; /* suffix length */
+ int id; /* used in node->suffix */
+ int type; /* prefix / postfix */
+
+ public KeySuffix(String name, int len, int id, int type) {
+ this.name = name;
+ this.len = len;
+ this.id = id;
+ this.type = type;
+ }
+ }
+
+ static class KeyWord {
+ String name;
+ int len;
+ int id;
+ DCH_poz idType;
+ boolean is_digit;
+ FromCharDateMode date_mode;
+ }
+
+ static class FormatNode {
+ int type; /* node type */
+ KeyWord key; /* if node type is KEYWORD */
+ char character; /* if node type is CHAR */
+ int suffix; /* keyword suffix */
+ }
+
+ static class TmFromChar {
+ FromCharDateMode mode = FromCharDateMode.FROM_CHAR_DATE_NONE;
+ int hh;
+ int pm;
+ int mi;
+ int ss;
+ int ssss;
+ int d; /* stored as 1-7, Sunday = 1, 0 means missing */
+ int dd;
+ int ddd;
+ int mm;
+ int ms;
+ int year;
+ int bc;
+ int ww;
+ int w;
+ int cc;
+ int j;
+ int us;
+ int yysz; /* is it YY or YYYY ? */
+ int clock; /* 12 or 24 hour clock? */
+ }
+ static Map<String, FormatNode[]> formatNodeCache = new HashMap<String, FormatNode[]>();
+
+ /**
+ * ----------
+ * Skip TM / th in FROM_CHAR
+ * ----------
+ */
+ static int SKIP_THth(int suf) {
+ return (S_THth(suf) != 0 ? 2 : 0);
+ }
+
+ /**
+ * ----------
+ * Suffix tests
+ * ----------
+ */
+ static int S_THth(int s) {
+ return ((((s) & DCH_S_TH) != 0 || ((s) & DCH_S_th) != 0) ? 1 : 0);
+ }
+ static int S_TH(int s) {
+ return (((s) & DCH_S_TH) != 0 ? 1 : 0);
+ }
+ static int S_th(int s) {
+ return (((s) & DCH_S_th) != 0 ? 1 : 0);
+ }
+ static int S_TH_TYPE(int s) {
+ return (((s) & DCH_S_TH) != 0 ? TH_UPPER : TH_LOWER);
+ }
+
+ static final int TH_UPPER = 1;
+ static final int TH_LOWER = 2;
+
+ /* Oracle toggles FM behavior, we don't; see docs. */
+ static int S_FM(int s) {
+ return (((s) & DCH_S_FM) != 0 ? 1 : 0);
+ }
+ static int S_SP(int s) {
+ return (((s) & DCH_S_SP) != 0 ? 1 : 0);
+ }
+ static int S_TM(int s) {
+ return (((s) & DCH_S_TM) != 0 ? 1 : 0);
+ }
+
+ public static TimeMeta parseDateTime(String dateText, String formatText) {
+ TimeMeta tm = new TimeMeta();
+
+ //TODO consider TimeZone
+ doToTimestamp(dateText, formatText, tm);
+
+ if (tm.dayOfYear > 0 && tm.dayOfMonth > 0) {
+ tm.dayOfYear = 0;
+ }
+
+ return tm;
+ }
+
+ /**
+ * Make Timestamp from date_str which is formatted at argument 'fmt'
+ * ( toTimestamp is reverse to_char() )
+ * @param dateText
+ * @param formatText
+ * @return
+ */
+ public static TimestampDatum toTimestamp(String dateText, String formatText) {
+ TimeMeta tm = parseDateTime(dateText, formatText);
+
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ }
+
+ /**
+ * Parse the 'dateText' according to 'formatText', return results as a TimeMeta tm
+ * and fractional seconds.
+ *
+ * We parse 'formatText' into a list of FormatNodes, which is then passed to
+ * DCH_from_char to populate a TmFromChar with the parsed contents of
+ * 'dateText'.
+ *
+ * The TmFromChar is then analysed and converted into the final results in struct 'tm'.
+ *
+ * This function does very little error checking, e.g.
+ * to_timestamp('20096040','YYYYMMDD') works
+ * @param dateText
+ * @param formatText
+ * @param tm
+ */
+ static void doToTimestamp(String dateText, String formatText, TimeMeta tm) {
+ TmFromChar tmfc = new TmFromChar();
+ int formatLength = formatText.length();
+
+ if (formatLength > 0) {
+ FormatNode[] formatNodes;
+ synchronized(formatNodeCache) {
+ formatNodes = formatNodeCache.get(formatText);
+ }
+
+ if (formatNodes == null) {
+ formatNodes = new FormatNode[formatLength + 1];
+ for (int i = 0; i < formatNodes.length; i++) {
+ formatNodes[i] = new FormatNode();
+ }
+ parseFormat(formatNodes, formatText, FORMAT_TYPE.DCH_TYPE);
+ formatNodes[formatLength].type = NODE_TYPE_END; /* Paranoia? */
+
+ synchronized(formatNodeCache) {
+ formatNodeCache.put(formatText, formatNodes);
+ }
+ }
+ DCH_from_char(formatNodes, dateText, tmfc);
+ }
+
+ /*
+ * Convert values that user define for FROM_CHAR (to_date/to_timestamp) to
+ * standard 'tm'
+ */
+ if (tmfc.ssss != 0) {
+ int x = tmfc.ssss;
+
+ tm.hours = x / DateTimeConstants.SECS_PER_HOUR;
+ x %= DateTimeConstants.SECS_PER_HOUR;
+ tm.minutes = x / DateTimeConstants.SECS_PER_MINUTE;
+ x %= DateTimeConstants.SECS_PER_MINUTE;
+ tm.secs = x;
+ }
+
+ if (tmfc.ss != 0) {
+ tm.secs = tmfc.ss;
+ }
+ if (tmfc.mi != 0) {
+ tm.minutes = tmfc.mi;
+ }
+ if (tmfc.hh != 0) {
+ tm.hours = tmfc.hh;
+ }
+
+ if (tmfc.clock == CLOCK_12_HOUR) {
+ if (tm.hours < 1 || tm.hours > HOURS_PER_DAY / 2) {
+ throw new IllegalArgumentException(
+ "hour \"" + tm.hours + "\" is invalid for the 12-hour clock, " +
+ "Use the 24-hour clock, or give an hour between 1 and 12.");
+ }
+ if (tmfc.pm != 0 && tm.hours < HOURS_PER_DAY / 2) {
+ tm.hours += HOURS_PER_DAY / 2;
+ } else if (tmfc.pm == 0 && tm.hours == HOURS_PER_DAY / 2) {
+ tm.hours = 0;
+ }
+ }
+
+ if (tmfc.year != 0) {
+ /*
+ * If CC and YY (or Y) are provided, use YY as 2 low-order digits for
+ * the year in the given century. Keep in mind that the 21st century
+ * AD runs from 2001-2100, not 2000-2099; 6th century BC runs from
+ * 600BC to 501BC.
+ */
+ if (tmfc.cc != 0 && tmfc.yysz <= 2) {
+ if (tmfc.bc != 0) {
+ tmfc.cc = -tmfc.cc;
+ }
+ tm.years = tmfc.year % 100;
+ if (tm.years != 0) {
+ if (tmfc.cc >= 0) {
+ tm.years += (tmfc.cc - 1) * 100;
+ } else {
+ tm.years = (tmfc.cc + 1) * 100 - tm.years + 1;
+ }
+ } else {
+ /* find century year for dates ending in "00" */
+ tm.years = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1);
+ }
+ } else {
+ /* If a 4-digit year is provided, we use that and ignore CC. */
+ tm.years = tmfc.year;
+ if (tmfc.bc != 0 && tm.years > 0) {
+ tm.years = -(tm.years - 1);
+ }
+ }
+ }
+ else if (tmfc.cc != 0) { /* use first year of century */
+ if (tmfc.bc != 0) {
+ tmfc.cc = -tmfc.cc;
+ }
+ if (tmfc.cc >= 0) {
+ /* +1 becuase 21st century started in 2001 */
+ tm.years = (tmfc.cc - 1) * 100 + 1;
+ } else {
+ /* +1 because year == 599 is 600 BC */
+ tm.years = tmfc.cc * 100 + 1;
+ }
+ }
+
+ if (tmfc.j != 0) {
+ DateTimeUtil.j2date(tmfc.j, tm);
+ }
+ if (tmfc.ww != 0) {
+ if (tmfc.mode == FromCharDateMode.FROM_CHAR_DATE_ISOWEEK) {
+ /*
+ * If tmfc.d is not set, then the date is left at the beginning of
+ * the ISO week (Monday).
+ */
+ if (tmfc.d != 0) {
+ DateTimeUtil.isoweekdate2date(tmfc.ww, tmfc.d, tm);
+ } else {
+ DateTimeUtil.isoweek2date(tmfc.ww, tm);
+ }
+ } else {
+ tmfc.ddd = (tmfc.ww - 1) * 7 + 1;
+ }
+ }
+
+ if (tmfc.w != 0) {
+ tmfc.dd = (tmfc.w - 1) * 7 + 1;
+ }
+ if (tmfc.d != 0) {
+ //tm.tm_wday = tmfc.d - 1; /* convert to native numbering */
+ }
+ if (tmfc.dd != 0) {
+ tm.dayOfMonth = tmfc.dd;
+ }
+ if (tmfc.ddd != 0) {
+ tm.dayOfYear = tmfc.ddd;
+ }
+ if (tmfc.mm != 0) {
+ tm.monthOfYear = tmfc.mm;
+ }
+ if (tmfc.ddd != 0 && (tm.monthOfYear <= 1 || tm.dayOfMonth <= 1)) {
+ /*
+ * The month and day field have not been set, so we use the
+ * day-of-year field to populate them. Depending on the date mode,
+ * this field may be interpreted as a Gregorian day-of-year, or an ISO
+ * week date day-of-year.
+ */
+ if (tm.years == 0 && tmfc.bc == 0) {
+ throw new IllegalArgumentException("cannot calculate day of year without year information");
+ }
+ if (tmfc.mode == FromCharDateMode.FROM_CHAR_DATE_ISOWEEK) {
+ /* zeroth day of the ISO year, in Julian */
+ int j0 = DateTimeUtil.isoweek2j(tm.years, 1) - 1;
+ DateTimeUtil.j2date(j0 + tmfc.ddd, tm);
+ } else {
+ int i;
+
+ boolean leap = DateTimeUtil.isLeapYear(tm.years);
+ int[] y = ysum[leap ? 1 : 0];
+
+ for (i = 1; i <= MONTHS_PER_YEAR; i++) {
+ if (tmfc.ddd < y[i])
+ break;
+ }
+ if (tm.monthOfYear <= 1) {
+ tm.monthOfYear = i;
+ }
+
+ if (tm.dayOfMonth <= 1) {
+ tm.dayOfMonth = tmfc.ddd - y[i - 1];
+ }
+ tm.dayOfYear = 0;
+ }
+ }
+
+ if (tmfc.ms != 0) {
+ tm.fsecs += tmfc.ms * 1000;
+ }
+ if (tmfc.us != 0) {
+ tm.fsecs += tmfc.us;
+ }
+ }
+
+ /**
+ * Format parser, search small keywords and keyword's suffixes, and make
+ * format-node tree.
+ *
+ * for DATE-TIME & NUMBER version
+ * @param node
+ * @param str
+ * @param ver
+ */
+ static void parseFormat(FormatNode[] node, String str, FORMAT_TYPE ver) {
+ KeySuffix s;
+ boolean node_set = false;
+ int suffix;
+ int last = 0;
+
+ int nodeIndex = 0;
+ int charIdx = 0;
+ char[] chars = str.toCharArray();
+
+ while (charIdx < chars.length) {
+ suffix = 0;
+
+ // Prefix
+ if (ver == FORMAT_TYPE.DCH_TYPE && (s = suff_search(chars, charIdx, SUFFTYPE_PREFIX)) != null) {
+ suffix |= s.id;
+ if (s.len > 0) {
+ charIdx += s.len;
+ }
+ }
+
+ // Keyword
+ if (charIdx < chars.length && (node[nodeIndex].key = index_seq_search(chars, charIdx)) != null) {
+ node[nodeIndex].type = NODE_TYPE_ACTION;
+ node[nodeIndex].suffix = 0;
+ node_set = true;
+ if (node[nodeIndex].key.len > 0) {
+ charIdx += node[nodeIndex].key.len;
+ }
+
+ // NUM version: Prepare global NUMDesc struct
+ if (ver == FORMAT_TYPE.NUM_TYPE) {
+ //NUMDesc_prepare(Num, node);
+ }
+
+ // Postfix
+ if (ver == FORMAT_TYPE.DCH_TYPE && charIdx < chars.length && (s = suff_search(chars, charIdx, SUFFTYPE_POSTFIX)) != null) {
+ suffix |= s.id;
+ if (s.len > 0) {
+ charIdx += s.len;
+ }
+ }
+ } else if (charIdx < chars.length) {
+ // Special characters '\' and '"'
+ if (chars[charIdx] == '"' && last != '\\') {
+ int x = 0;
+
+ while (charIdx < chars.length ) {
+ charIdx++;
+ if (chars[charIdx] == '"' && x != '\\') {
+ charIdx++;
+ break;
+ } else if (chars[charIdx] == '\\' && x != '\\') {
+ x = '\\';
+ continue;
+ }
+ node[nodeIndex].type = NODE_TYPE_CHAR;
+ node[nodeIndex].character = chars[charIdx];
+ node[nodeIndex].key = null;
+ node[nodeIndex].suffix = 0;
+ nodeIndex++;
+ x = chars[charIdx];
+ }
+ node_set = false;
+ suffix = 0;
+ last = 0;
+ } else if (charIdx < chars.length - 1 && chars[charIdx] == '\\' && last != '\\' && chars[charIdx + 1] == '"') {
+ last = chars[charIdx];
+ charIdx++;
+ } else if (charIdx < chars.length) {
+ node[nodeIndex].type = NODE_TYPE_CHAR;
+ node[nodeIndex].character = chars[charIdx];
+ node[nodeIndex].key = null;
+ node_set = true;
+ last = 0;
+ charIdx++;
+ }
+ }
+
+ // end
+ if (node_set) {
+ if (node[nodeIndex].type == NODE_TYPE_ACTION) {
+ node[nodeIndex].suffix = suffix;
+ }
+ nodeIndex++;
+ node[nodeIndex].suffix = 0;
+ node_set = false;
+ }
+ }
+
+ node[nodeIndex].type = NODE_TYPE_END;
+ node[nodeIndex].suffix = 0;
+ }
+
+ /**
+ * Process a string as denoted by a list of FormatNodes.
+ * The TmFromChar struct pointed to by 'out' is populated with the results.
+ *
+ * Note: we currently don't have any to_interval() function, so there
+ * is no need here for INVALID_FOR_INTERVAL checks.
+ * @param nodes
+ * @param dateText
+ * @param out
+ */
+ static void DCH_from_char(FormatNode[] nodes, String dateText, TmFromChar out) {
+ int len;
+ AtomicInteger value = new AtomicInteger();
+ boolean fx_mode = false;
+
+ char[] chars = dateText.toCharArray();
+ int charIdx = 0;
+ int nodeIdx = 0;
+ for (; nodeIdx < nodes.length; nodeIdx++) {
+ FormatNode node = nodes[nodeIdx];
+ if (node.type == NODE_TYPE_END || charIdx >= chars.length) {
+ break;
+ }
+ if (node.type != NODE_TYPE_ACTION) {
+ charIdx++;
+ /* Ignore spaces when not in FX (fixed width) mode */
+ if (Character.isSpaceChar(node.character) && !fx_mode) {
+ while (charIdx < chars.length && Character.isSpaceChar(chars[charIdx])) {
+ charIdx++;
+ }
+ }
+ continue;
+ }
+
+ from_char_set_mode(out, node.key.date_mode);
+
+ switch (node.key.idType) {
+ case DCH_FX:
+ fx_mode = true;
+ break;
+ case DCH_A_M:
+ case DCH_P_M:
+ case DCH_a_m:
+ case DCH_p_m:
+ value.set(out.pm);
+ charIdx += from_char_seq_search(value, dateText, charIdx, ampm_strings_long, ALL_UPPER, node.key.len, node);
+ assertOutValue(out.pm, value.get() % 2, node);
+ out.pm = value.get() % 2;
+ out.clock = CLOCK_12_HOUR;
+ break;
+ case DCH_AM:
+ case DCH_PM:
+ case DCH_am:
+ case DCH_pm:
+ value.set(out.pm);
+ charIdx += from_char_seq_search(value, dateText, charIdx, ampm_strings, ALL_UPPER, node.key.len, node);
+ assertOutValue(out.pm, value.get() % 2, node);
+ out.pm = value.get() % 2;
+ out.clock = CLOCK_12_HOUR;
+ break;
+ case DCH_HH:
+ case DCH_HH12:
+ value.set(out.hh);
+ charIdx += from_char_parse_int_len(value, dateText, charIdx, 2, nodes, nodeIdx);
+ out.hh = value.get();
+ out.clock = CLOCK_12_HOUR;
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_HH24:
+ value.set(out.hh);
+ charIdx += from_char_parse_int_len(value, dateText, charIdx, 2, nodes, nodeIdx);
+ out.hh = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_MI:
+ value.set(out.mi);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.mi = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_SS:
+ value.set(out.ss);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.ss = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_MS: /* millisecond */
+ value.set(out.ms);
+ len = from_char_parse_int_len(value, dateText, charIdx, 3, nodes, nodeIdx);
+ charIdx += len;
+ out.ms = value.get();
+ /*
+ * 25 is 0.25 and 250 is 0.25 too; 025 is 0.025 and not 0.25
+ */
+ out.ms *= len == 1 ? 100 :
+ len == 2 ? 10 : 1;
+
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_US: /* microsecond */
+ value.set(out.us);
+ len = from_char_parse_int_len(value, dateText, charIdx, 6, nodes, nodeIdx);
+ charIdx += len;
+ out.us = value.get();
+ out.us *= len == 1 ? 100000 :
+ len == 2 ? 10000 :
+ len == 3 ? 1000 :
+ len == 4 ? 100 :
+ len == 5 ? 10 : 1;
+
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_SSSS:
+ value.set(out.ssss);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.ssss = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_tz:
+ case DCH_TZ:
+ throw new IllegalArgumentException("\"TZ\"/\"tz\" format patterns are not supported in to_date");
+ case DCH_A_D:
+ case DCH_B_C:
+ case DCH_a_d:
+ case DCH_b_c:
+ value.set(out.bc);
+ charIdx += from_char_seq_search(value, dateText, charIdx, adbc_strings_long, ALL_UPPER, node.key.len, node);
+ assertOutValue(out.bc, value.get() % 2, node);
+ out.bc = value.get() % 2;
+ break;
+ case DCH_AD:
+ case DCH_BC:
+ case DCH_ad:
+ case DCH_bc:
+ value.set(out.bc);
+ charIdx += from_char_seq_search(value, dateText, charIdx, adbc_strings, ALL_UPPER, node.key.len, node);
+ assertOutValue(out.bc, value.get() % 2, node);
+ out.bc = value.get() % 2;
+ break;
+ case DCH_MONTH:
+ case DCH_Month:
+ case DCH_month:
+ value.set(out.mm);
+ charIdx += from_char_seq_search(value, dateText, charIdx, months_full, ONE_UPPER, MAX_MONTH_LEN, node);
+ assertOutValue(out.mm, value.get() + 1, node);
+ out.mm = value.get() + 1;
+ break;
+ case DCH_MON:
+ case DCH_Mon:
+ case DCH_mon:
+ value.set(out.mm);
+ charIdx += from_char_seq_search(value, dateText, charIdx, months_short, ONE_UPPER, MAX_MON_LEN, node);
+ assertOutValue(out.mm, value.get() + 1, node);
+ out.mm = value.get() + 1;
+ break;
+ case DCH_MM:
+ value.set(out.mm);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.mm = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_DAY:
+ case DCH_Day:
+ case DCH_day:
+ value.set(out.d);
+ charIdx += from_char_seq_search(value, dateText, charIdx, days_full, ONE_UPPER, MAX_DAY_LEN, node);
+ assertOutValue(out.d, value.get(), node);
+ out.d = value.get();
+ out.d++;
+ break;
+ case DCH_DY:
+ case DCH_Dy:
+ case DCH_dy:
+ value.set(out.d);
+ charIdx += from_char_seq_search(value, dateText, charIdx, days_full, ONE_UPPER, MAX_DY_LEN, node);
+ assertOutValue(out.d, value.get(), node);
+ out.d = value.get();
+ out.d++;
+ break;
+ case DCH_DDD:
+ value.set(out.ddd);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.ddd = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_IDDD:
+ value.set(out.ddd);
+ charIdx += from_char_parse_int_len(value, dateText, charIdx, 3, nodes, nodeIdx);
+ out.ddd = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_DD:
+ value.set(out.dd);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.dd = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_D:
+ value.set(out.d);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.d = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_ID:
+ value.set(out.d);
+ charIdx += from_char_parse_int_len(value, dateText, charIdx, 1, nodes, nodeIdx);
+ out.d = value.get();
+ /* Shift numbering to match Gregorian where Sunday = 1 */
+ if (++out.d > 7) {
+ out.d = 1;
+ }
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_WW:
+ case DCH_IW:
+ value.set(out.ww);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.ww = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_Q:
+ /*
+ * We ignore 'Q' when converting to date because it is unclear
+ * which date in the quarter to use, and some people specify
+ * both quarter and month, so if it was honored it might
+ * conflict with the supplied month. That is also why we don't
+ * throw an error.
+ *
+ * We still parse the source string for an integer, but it
+ * isn't stored anywhere in 'out'.
+ */
+ charIdx += from_char_parse_int(null, dateText, charIdx, nodes, nodeIdx);
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_CC:
+ value.set(out.cc);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.cc = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_Y_YYY: {
+ int commaIndex = dateText.indexOf(",", charIdx);
+ if (commaIndex <= 0) {
+ throw new IllegalArgumentException("invalid input string for \"Y,YYY\"");
+ }
+ int millenia = Integer.parseInt(dateText.substring(charIdx, commaIndex));
+ int years = Integer.parseInt(dateText.substring(commaIndex + 1, commaIndex + 1 + 3));
+ years += (millenia * 1000);
+ assertOutValue(out.year, years, node);
+ out.year = years;
+ out.yysz = 4;
+ charIdx += strdigits_len(dateText, charIdx) + 4 + SKIP_THth(node.suffix);
+ }
+ break;
+ case DCH_YYYY:
+ case DCH_IYYY:
+ value.set(out.year);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.year = value.get();
+ out.yysz = 4;
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_YYY:
+ case DCH_IYY: {
+ int retVal = from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ charIdx += retVal;
+ out.year = value.get();
+ if (retVal < 4) {
+ out.year = adjust_partial_year_to_2020(out.year);
+ }
+ out.yysz = 3;
+ charIdx += SKIP_THth(node.suffix);
+ }
+ break;
+ case DCH_YY:
+ case DCH_IY: {
+ value.set(out.year);
+ int retVal = from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ charIdx += retVal;
+ out.year = value.get();
+ if (retVal < 4) {
+ out.year = adjust_partial_year_to_2020(out.year);
+ }
+ out.yysz = 2;
+ charIdx += SKIP_THth(node.suffix);
+ }
+ break;
+ case DCH_Y:
+ case DCH_I:
+ value.set(out.year);
+ int retVal = from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ charIdx += retVal;
+ out.year = value.get();
+ if (retVal < 4) {
+ out.year = adjust_partial_year_to_2020(out.year);
+ }
+ out.yysz = 1;
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_RM:
+ value.set(out.mm);
+ charIdx += from_char_seq_search(value, dateText, charIdx, rm_months_upper, ALL_UPPER, MAX_RM_LEN, node);
+ assertOutValue(out.mm, MONTHS_PER_YEAR - value.get(), node);
+ out.mm = MONTHS_PER_YEAR - value.get();
+ break;
+ case DCH_rm:
+ value.set(out.mm);
+ charIdx += from_char_seq_search(value, dateText, charIdx, rm_months_lower, ALL_LOWER, MAX_RM_LEN, node);
+ assertOutValue(out.mm, MONTHS_PER_YEAR - value.get(), node);
+ out.mm = MONTHS_PER_YEAR - value.get();
+ break;
+ case DCH_W:
+ value.set(out.w);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.w = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ case DCH_J:
+ value.set(out.j);
+ charIdx += from_char_parse_int(value, dateText, charIdx, nodes, nodeIdx);
+ out.j = value.get();
+ charIdx += SKIP_THth(node.suffix);
+ break;
+ }
+ }
+ }
+
+ static KeySuffix suff_search(char[] chars, int startIdx, int type) {
+ for (KeySuffix eachSuffix: DCH_suff) {
+ if (eachSuffix.type != type) {
+ continue;
+ }
+
+ if (strncmp(chars, startIdx, eachSuffix.name, eachSuffix.len)) {
+ return eachSuffix;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Fast sequential search, use index for data selection which
+ * go to seq. cycle (it is very fast for unwanted strings)
+ * (can't be used binary search in format parsing)
+ * @param chars
+ * @param startIdx
+ * @return
+ */
+ static KeyWord index_seq_search(char[] chars, int startIdx) {
+ if (KeyWord_INDEX_FILTER(chars[startIdx]) == 0) {
+ return null;
+ }
+
+ Integer pos = DCH_index.get(chars[startIdx]);
+
+ if (pos != null) {
+ KeyWord keyword = DCH_keywords[pos];
+ do {
+ if (strncmp(chars, startIdx, keyword.name, keyword.len)) {
+ return keyword;
+ }
+ pos++;
+ if (pos >= DCH_keywords.length) {
+ return null;
+ }
+ keyword = DCH_keywords[pos];
+ } while (chars[startIdx] == keyword.name.charAt(0));
+ }
+ return null;
+ }
+
+ static boolean strncmp(char[] chars, int startIdx, String str, int len) {
+ if (chars.length - startIdx < len) {
+ return false;
+ }
+
+ int index = startIdx;
+ for (int i = 0; i < len; i++, index++) {
+ if (chars[index] != str.charAt(i)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ static int KeyWord_INDEX_FILTER(char c) {
+ return ((c) <= ' ' || (c) >= '~' ? 0 : 1);
+ }
+
+ /**
+ * Set the date mode of a from-char conversion.
+ *
+ * Puke if the date mode has already been set, and the caller attempts to set
+ * it to a conflicting mode.
+ * @param tmfc
+ * @param mode
+ */
+ static void from_char_set_mode(TmFromChar tmfc, FromCharDateMode mode) {
+ if (mode != FromCharDateMode.FROM_CHAR_DATE_NONE) {
+ if (tmfc.mode == FromCharDateMode.FROM_CHAR_DATE_NONE) {
+ tmfc.mode = mode;
+ } else if (tmfc.mode != mode) {
+ throw new IllegalArgumentException("invalid combination of date conventions: " +
+ "Do not mix Gregorian and ISO week date " +
+ "conventions in a formatting template.");
+ }
+ }
+ }
+
+ /**
+ * Perform a sequential search in 'array' for text matching the first 'max'
+ * characters of the source string.
+ *
+ * If a match is found, copy the array index of the match into the integer
+ * pointed to by 'dest', advance 'src' to the end of the part of the string
+ * which matched, and return the number of characters consumed.
+ *
+ * If the string doesn't match, throw an error.
+ * @param dest
+ * @param src
+ * @param charIdx
+ * @param array
+ * @param type
+ * @param max
+ * @param node
+ * @return
+ */
+ static int from_char_seq_search(AtomicInteger dest, String src, int charIdx, String[] array, int type, int max,
+ FormatNode node) {
+ AtomicInteger len = new AtomicInteger(0);
+
+ dest.set(seq_search(src, charIdx, array, type, max, len));
+ if (len.get() <= 0) {
+ String copy;
+ if (charIdx + node.key.len >= src.length()) {
+ copy = src.substring(charIdx, charIdx + node.key.len);
+ } else {
+ copy = src.substring(charIdx);
+ }
+ throw new IllegalArgumentException("Invalid value \"" + copy + "\" for \"" + node.key.name + "\". " +
+ "The given value did not match any of the allowed values for this field.");
+ }
+ return len.get();
+ }
+
+ /**
+ * Sequential search with to upper/lower conversion
+ * @param name
+ * @param charIdx
+ * @param array
+ * @param type
+ * @param max
+ * @param len
+ * @return
+ */
+ static int seq_search(String name, int charIdx, String[] array, int type, int max, AtomicInteger len) {
+ if (name == null || name.length() <= charIdx) {
+ return -1;
+ }
+
+ char[] nameChars = name.toCharArray();
+ char nameChar = nameChars[charIdx];
+
+ /* set first char */
+ if (type == ONE_UPPER || type == ALL_UPPER) {
+ nameChar = Character.toUpperCase(nameChar);
+ } else if (type == ALL_LOWER) {
+ nameChar = Character.toLowerCase(nameChar);
+ }
+
+ int arrayIndex = 0;
+ for (int last = 0; array[arrayIndex] != null; arrayIndex++) {
+ String arrayStr = array[arrayIndex];
+ /* comperate first chars */
+ if (nameChar != arrayStr.charAt(0)) {
+ continue;
+ }
+ int arrayStrLen = arrayStr.length();
+ int arrayCharIdx = 1;
+ int nameCharIdx = charIdx + 1;
+
+ for (int idx = 1; ; nameCharIdx++, arrayCharIdx++, idx++) {
+ // search fragment (max) only
+ if (max != 0 && idx == max) {
+ len.set(idx + 1); // '\0'
+ return arrayIndex;
+ }
+ // full size
+ if (arrayCharIdx == arrayStrLen - 1) {
+ len.set(idx + 1); // '\0'
+ return arrayIndex;
+ }
+ // Not found in array 'a'
+ if (nameCharIdx == nameChars.length - 1) {
+ break;
+ }
+ /*
+ * Convert (but convert new chars only)
+ */
+ nameChar = nameChars[nameCharIdx];
+ if (idx > last) {
+ if (type == ONE_UPPER || type == ALL_LOWER) {
+ nameChar = Character.toLowerCase(nameChar);
+ } else if (type == ALL_UPPER) {
+ nameChar = Character.toUpperCase(nameChar);
+ }
+ last = idx;
+ }
+ if (nameChar != arrayStr.charAt(arrayCharIdx)){
+ break;
+ }
+ }
+ }
+
+ return -1;
+ }
+
+ /**
+ * Read a single integer from the source string, into the int pointed to by
+ * 'dest'. If 'dest' is NULL, the result is discarded.
+ *
+ * In fixed-width mode (the node does not have the FM suffix), consume at most
+ * 'len' characters. However, any leading whitespace isn't counted in 'len'.
+ *
+ * We use strtol() to recover the integer value from the source string, in
+ * accordance with the given FormatNode.
+ *
+ * If the conversion completes successfully, src will have been advanced to
+ * point at the character immediately following the last character used in the
+ * conversion.
+ *
+ * Return the number of characters consumed.
+ *
+ * Note that from_char_parse_int() provides a more convenient wrapper where
+ * the length of the field is the same as the length of the format keyword (as
+ * with DD and MI).
+ * @param dest
+ * @param src
+ * @param charIdx
+ * @param len
+ * @param nodes
+ * @param nodeIndex
+ * @return
+ */
+ static int from_char_parse_int_len(AtomicInteger dest, String src, int charIdx, int len, FormatNode[] nodes, int nodeIndex) {
+ long result;
+ int initCharIdx = charIdx;
+ StringBuilder tempSb = new StringBuilder();
+
+ /*
+ * Skip any whitespace before parsing the integer.
+ */
+ charIdx = strspace_len(src, charIdx);
+
+ int used = src.length() <= charIdx + len ? src.length() - (charIdx + len) : len;
+ if (used <= 0) {
+ used = src.length() - charIdx;
+ }
+ String copy = src.substring(charIdx, charIdx + used);
+
+ if (S_FM(nodes[nodeIndex].suffix) != 0 || is_next_separator(nodes, nodeIndex)) {
+ /*
+ * This node is in Fill Mode, or the next node is known to be a
+ * non-digit value, so we just slurp as many characters as we can get.
+ */
+ result = DateTimeUtil.strtol(src, charIdx, tempSb);
+ charIdx = src.length() - tempSb.length();
+ } else {
+
+ /*
+ * We need to pull exactly the number of characters given in 'len' out
+ * of the string, and convert those.
+ */
+ if (used < len) {
+ throw new IllegalArgumentException("source string too short for \"" + nodes[nodeIndex].key.name + "\" + formatting field");
+ }
+ result = DateTimeUtil.strtol(copy, 0, tempSb);
+ used = copy.length() - tempSb.length();
+
+ if (used > 0 && used < len) {
+ throw new IllegalArgumentException("invalid value \"" + copy + "\" for \"" + nodes[nodeIndex].key.name + "\"." +
+ "Field requires " + len + " characters, but only " + used);
+ }
+ charIdx += used;
+ }
+
+ if (charIdx == initCharIdx) {
+ throw new IllegalArgumentException("invalid value \"" + copy + "\" for \"" + nodes[nodeIndex].key.name + "\"." +
+ "Value must be an integer.");
+ }
+ if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("value for \"" + nodes[nodeIndex].key.name + "\"" +
+ " in source string is out of range." +
+ "Value must be in the range " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE + ".");
+ }
+ if (dest != null) {
+ assertOutValue(dest.get(), (int)result, nodes[nodeIndex]);
+ dest.set((int)result);
+ }
+ return charIdx - initCharIdx;
+ }
+
+ /**
+ * Call from_char_parse_int_len(), using the length of the format keyword as
+ * the expected length of the field.
+ *
+ * Don't call this function if the field differs in length from the format
+ * keyword (as with HH24; the keyword length is 4, but the field length is 2).
+ * In such cases, call from_char_parse_int_len() instead to specify the
+ * required length explicitly.
+ * @param dest
+ * @param src
+ * @param charIdx
+ * @param nodes
+ * @param nodeIdx
+ * @return
+ */
+ static int from_char_parse_int(AtomicInteger dest, String src, int charIdx, FormatNode[] nodes, int nodeIdx) {
+ return from_char_parse_int_len(dest, src, charIdx, nodes[nodeIdx].key.len, nodes, nodeIdx);
+ }
+
+ static int strspace_len(String str, int charIdx) {
+ int len = str.length();
+ while (charIdx < len && Character.isSpaceChar(str.charAt(charIdx))) {
+ charIdx++;
+ }
+ return charIdx;
+ }
+
+ static int strdigits_len(String str, int charIdx) {
+ int len = strspace_len(str, charIdx);
+ int index = charIdx + len;
+
+ int strLen = str.length();
+
+ while (index < strLen && Character.isDigit(str.charAt(index)) && len <= DCH_MAX_ITEM_SIZ) {
+ len++;
+ index++;
+ }
+ return len;
+ }
+
+ static void assertOutValue(int dest, int value, FormatNode node) {
+ if (dest != 0 && dest != value) {
+ throw new IllegalArgumentException(
+ "conflicting values for \"" + node.key.name + "\" field in formatting string," +
+ "This value contradicts a previous setting for the same field type(" + dest + "," + value + ")");
+ }
+ }
+
+ /**
+ * Return true if next format picture is not digit value
+ * @param nodes
+ * @param nodeIndex
+ * @return
+ */
+ static boolean is_next_separator(FormatNode[] nodes, int nodeIndex) {
+ int index = nodeIndex;
+ if (nodes[index].type == NODE_TYPE_END) {
+ return false;
+ }
+
+ if (nodes[index].type == NODE_TYPE_ACTION && S_THth(nodes[index].suffix) != 0) {
+ return true;
+ }
+
+ /*
+ * Next node
+ */
+ index++;
+
+ /* end of format string is treated like a non-digit separator */
+ if (nodes[index].type == NODE_TYPE_END) {
+ return true;
+ }
+
+ if (nodes[index].type == NODE_TYPE_ACTION) {
+ return nodes[index].key.is_digit ? false : true;
+ } else if (Character.isDigit(nodes[index].character)) {
+ return false;
+ }
+
+ return true; /* some non-digit input (separator) */
+ }
+
+ /**
+ * Adjust all dates toward 2020; this is effectively what happens when we
+ * assume '70' is 1970 and '69' is 2069.
+ * @param year
+ * @return
+ */
+ static int adjust_partial_year_to_2020(int year) {
+ if (year < 70) {
+ /* Force 0-69 into the 2000's */
+ return year + 2000;
+ } else if (year < 100) {
+ /* Force 70-99 into the 1900's */
+ return year + 1900;
+ } else if (year < 520) {
+ /* Force 100-519 into the 2000's */
+ return year + 2000;
+ } else if (year < 1000) {
+ /* Force 520-999 into the 1000's */
+ return year + 1000;
+ } else {
+ return year;
+ }
+ }
+
+ /**
+ * Converts TimeMeta to a string using given the format pattern text.
+ * @param tm
+ * @param formatText
+ * @return
+ */
+ public static String to_char(TimeMeta tm, String formatText) {
+ int fmt_len = formatText.length();
+
+ StringBuilder out = new StringBuilder();
+ if (fmt_len > 0) {
+ FormatNode[] formatNodes;
+
+ synchronized(formatNodeCache) {
+ formatNodes = formatNodeCache.get(formatText);
+ }
+
+ if (formatNodes == null) {
+ formatNodes = new FormatNode[fmt_len + 1];
+ for (int i = 0; i < formatNodes.length; i++) {
+ formatNodes[i] = new FormatNode();
+ }
+ parseFormat(formatNodes, formatText, FORMAT_TYPE.DCH_TYPE);
+ formatNodes[fmt_len].type = NODE_TYPE_END; /* Paranoia? */
+
+ synchronized(formatNodeCache) {
+ formatNodeCache.put(formatText, formatNodes);
+ }
+ }
+ DCH_to_char(formatNodes, false, tm, out);
+ return out.toString();
+ } else {
+ throw new IllegalArgumentException("No format text.");
+ }
+ }
+
+ /**
+ * Process a TmToChar struct as denoted by a list of FormatNodes.
+ * The formatted data is written to the string pointed to by 'out'.
+ * @param nodes
+ * @param isInterval
+ * @param tm
+ * @param out
+ */
+ private static void DCH_to_char(FormatNode[] nodes, boolean isInterval, TimeMeta tm, StringBuilder out) {
+ int i;
+ for (FormatNode node: nodes) {
+ if (node.type == NODE_TYPE_END) {
+ break;
+ }
+ if (node.type != NODE_TYPE_ACTION) {
+ out.append(node.character);
+ continue;
+ }
+
+ switch (node.key.idType) {
+ case DCH_A_M:
+ case DCH_P_M:
+ out.append((tm.hours % HOURS_PER_DAY >= HOURS_PER_DAY / 2) ? P_M_STR : A_M_STR);
+ break;
+ case DCH_AM:
+ case DCH_PM:
+ out.append((tm.hours % HOURS_PER_DAY >= HOURS_PER_DAY / 2) ? PM_STR : AM_STR);
+ break;
+ case DCH_a_m:
+ case DCH_p_m:
+ out.append((tm.hours % HOURS_PER_DAY >= HOURS_PER_DAY / 2) ? p_m_STR : a_m_STR);
+ break;
+ case DCH_am:
+ case DCH_pm:
+ out.append((tm.hours % HOURS_PER_DAY >= HOURS_PER_DAY / 2) ? pm_STR : am_STR);
+ break;
+ case DCH_HH:
+ case DCH_HH12: {
+ /*
+ * display time as shown on a 12-hour clock, even for
+ * intervals
+ */
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%02d");
+ out.append(String.format(formatStr,
+ tm.hours % (HOURS_PER_DAY / 2) == 0 ? HOURS_PER_DAY / 2 : tm.hours % (HOURS_PER_DAY / 2)));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_HH24: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%02d");
+ out.append(String.format(formatStr, tm.hours));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_MI: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%02d");
+ out.append(String.format(formatStr, tm.minutes));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_SS: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%02d");
+ out.append(String.format(formatStr, tm.secs));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_MS: /* millisecond */
+ out.append(String.format("%03d", (int) (tm.fsecs / 1000.0)));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_US: /* microsecond */
+ out.append(String.format("%06d", (int) tm.fsecs));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_SSSS:
+ out.append(String.format("%d", tm.hours * DateTimeConstants.SECS_PER_HOUR +
+ tm.minutes * DateTimeConstants.SECS_PER_MINUTE + tm.secs));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_tz:
+ invalidForInterval(isInterval, node);
+ //TODO
+ break;
+ case DCH_A_D:
+ case DCH_B_C:
+ invalidForInterval(isInterval, node);
+ out.append((tm.years <= 0 ? B_C_STR : A_D_STR));
+ break;
+ case DCH_AD:
+ case DCH_BC:
+ invalidForInterval(isInterval, node);
+ out.append((tm.years <= 0 ? BC_STR : AD_STR));
+ break;
+ case DCH_a_d:
+ case DCH_b_c:
+ invalidForInterval(isInterval, node);
+ out.append((tm.years <= 0 ? b_c_STR : a_d_STR));
+ break;
+ case DCH_ad:
+ case DCH_bc:
+ invalidForInterval(isInterval, node);
+ out.append((tm.years <= 0 ? bc_STR : ad_STR));
+ break;
+ case DCH_MONTH:
+ invalidForInterval(isInterval, node);
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ if (S_TM(node.suffix) != 0) {
+ out.append(months_full[tm.monthOfYear - 1].toUpperCase());
+ } else {
+ String formatStr =(S_FM(node.suffix) != 0 ? "%0d": "%-09d");
+ out.append(String.format(formatStr, months_full[tm.monthOfYear - 1].toUpperCase()));
+ }
+ break;
+ case DCH_Month:
+ invalidForInterval(isInterval, node);
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ if (S_TM(node.suffix) != 0) {
+ out.append(months_full[tm.monthOfYear - 1]);
+ } else {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s": "%-9s");
+ out.append(String.format(formatStr, months_full[tm.monthOfYear - 1]));
+ }
+ break;
+ case DCH_month:
+ invalidForInterval(isInterval, node);
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ if (S_TM(node.suffix) != 0) {
+ out.append(months_full[tm.monthOfYear - 1].toLowerCase());
+ } else {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s": "%-9s");
+ out.append(String.format(formatStr, months_full[tm.monthOfYear - 1].toLowerCase()));
+ }
+ break;
+ case DCH_MON:
+ invalidForInterval(isInterval, node);
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ if (S_TM(node.suffix) != 0) {
+ out.append(months_short[tm.monthOfYear - 1].toUpperCase());
+ } else {
+ out.append(months_short[tm.monthOfYear - 1].toUpperCase());
+ }
+ break;
+ case DCH_Mon:
+ invalidForInterval(isInterval, node);
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ if (S_TM(node.suffix) != 0) {
+ out.append(months_short[tm.monthOfYear - 1]);
+ } else {
+ out.append(months_short[tm.monthOfYear - 1]);
+ }
+ break;
+ case DCH_mon:
+ invalidForInterval(isInterval, node);
+ if (tm.monthOfYear == 0)
+ break;
+ if (S_TM(node.suffix) != 0) {
+ out.append(months_short[tm.monthOfYear - 1].toLowerCase());
+ } else {
+ out.append(months_short[tm.monthOfYear - 1].toLowerCase());
+ }
+ break;
+ case DCH_MM: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%02d");
+ out.append(String.format(formatStr, tm.monthOfYear));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_DAY: {
+ invalidForInterval(isInterval, node);
+ if (S_TM(node.suffix) != 0) {
+ out.append(days_full[tm.getDayOfWeek()].toUpperCase());
+ } else {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s" : "%-9s");
+ out.append(String.format(formatStr, days_full[tm.getDayOfWeek()].toUpperCase()));
+ }
+ break;
+ }
+ case DCH_Day:
+ invalidForInterval(isInterval, node);
+ if (S_TM(node.suffix) != 0) {
+ out.append(days_full[tm.getDayOfWeek()]);
+ } else {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s" : "%-9s");
+ out.append(String.format(formatStr, days_full[tm.getDayOfWeek()]));
+ }
+ break;
+ case DCH_day:
+ invalidForInterval(isInterval, node);
+ if (S_TM(node.suffix) != 0) {
+ out.append(days_full[tm.getDayOfWeek()].toLowerCase());
+ } else {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s" : "%-9s");
+ out.append(String.format(formatStr, days_full[tm.getDayOfWeek()].toLowerCase()));
+ }
+ break;
+ case DCH_DY:
+ invalidForInterval(isInterval, node);
+ if (S_TM(node.suffix) != 0) {
+ out.append(days_short[tm.getDayOfWeek()]);
+ } else {
+ out.append(days_short[tm.getDayOfWeek()]);
+ }
+ break;
+ case DCH_Dy:
+ invalidForInterval(isInterval, node);
+ if (S_TM(node.suffix) != 0) {
+ out.append(days_short[tm.getDayOfWeek()]);
+ } else {
+ out.append(days_short[tm.getDayOfWeek()]);
+ }
+ break;
+ case DCH_dy:
+ invalidForInterval(isInterval, node);
+ if (S_TM(node.suffix) != 0) {
+ out.append(days_short[tm.getDayOfWeek()]);
+ } else {
+ out.append(days_short[tm.getDayOfWeek()]);
+ }
+ break;
+ case DCH_DDD:
+ case DCH_IDDD: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%0d" : "%03d");
+ out.append(String.format(formatStr,
+ (node.key.idType == DCH_poz.DCH_DDD) ?
+ tm.getDayOfYear() : DateTimeUtil.date2isoyearday(tm.years, tm.monthOfYear, tm.dayOfMonth)
+ ));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_DD: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%02d");
+ out.append(String.format(formatStr, tm.dayOfMonth));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_D:
+ invalidForInterval(isInterval, node);
+ out.append(String.format("%d", tm.getDayOfWeek() + 1));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_ID:
+ invalidForInterval(isInterval, node);
+ out.append(String.format("%d", (tm.getDayOfWeek() == 0) ? 7 : tm.getDayOfWeek()));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_WW: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%0d" : "%02d");
+ out.append(String.format(formatStr, (tm.getDayOfYear() - 1) / 7 + 1));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_IW: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%0d" : "%02d");
+ out.append(String.format(formatStr,
+ DateTimeUtil.date2isoweek(tm.years, tm.monthOfYear, tm.dayOfMonth)));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_Q:
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ out.append(String.format("%d", (tm.monthOfYear - 1) / 3 + 1));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_CC: {
+ if (isInterval) { /* straight calculation */
+ i = tm.years / 100;
+ } else {
+ if (tm.years > 0) {
+ /* Century 20 == 1901 - 2000 */
+ i = (tm.years - 1) / 100 + 1;
+ } else {
+ /* Century 6BC == 600BC - 501BC */
+ i = tm.years / 100 - 1;
+ }
+ }
+ if (i <= 99 && i >= -99) {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%0d" : "%02d");
+ out.append(String.format(formatStr, i));
+ } else {
+ out.append(String.format("%d", i));
+ }
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_Y_YYY:
+ i = ADJUST_YEAR(tm.years, isInterval) / 1000;
+ out.append(String.format("%d,%03d", i, ADJUST_YEAR(tm.years, isInterval) - (i * 1000)));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_YYYY:
+ case DCH_IYYY: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%d" : "%04d");
+ out.append(String.format(formatStr,
+ (node.key.idType == DCH_poz.DCH_YYYY ? ADJUST_YEAR(tm.years, isInterval) :
+ ADJUST_YEAR(DateTimeUtil.date2isoyear(tm.years, tm.monthOfYear, tm.dayOfMonth), isInterval))
+ ));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_YYY:
+ case DCH_IYY: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%0d" : "%03d");
+ out.append(String.format(formatStr,
+ (node.key.idType == DCH_poz.DCH_YYY ? ADJUST_YEAR(tm.years, isInterval) :
+ ADJUST_YEAR(DateTimeUtil.date2isoyear(tm.years, tm.monthOfYear, tm.dayOfMonth), isInterval)) % 1000
+ ));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ case DCH_YY:
+ case DCH_IY: {
+ String formatStr = (S_FM(node.suffix) != 0 ? "%0d" : "%02d");
+ out.append(String.format(formatStr,
+ (node.key.idType == DCH_poz.DCH_YY ? ADJUST_YEAR(tm.years, isInterval) :
+ ADJUST_YEAR(DateTimeUtil.date2isoyear(tm.years, tm.monthOfYear, tm.dayOfMonth), isInterval)) % 100
+ ));
+ if (S_THth(node.suffix) != 0)
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ break;
+ }
+ case DCH_Y:
+ case DCH_I:
+ out.append(String.format("%1d",
+ (node.key.idType == DCH_poz.DCH_Y ?
+ ADJUST_YEAR(tm.years, isInterval) :
+ ADJUST_YEAR(DateTimeUtil.date2isoyear(tm.years, tm.monthOfYear, tm.dayOfMonth),
+ isInterval)) % 10));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_RM: {
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s" : "%-4s");
+ out.append(String.format(formatStr,
+ rm_months_upper[MONTHS_PER_YEAR - tm.monthOfYear]));
+ break;
+ }
+ case DCH_rm: {
+ if (tm.monthOfYear == 0) {
+ break;
+ }
+ String formatStr = (S_FM(node.suffix) != 0 ? "%s" : "%-4s");
+ out.append(String.format(formatStr, rm_months_lower[MONTHS_PER_YEAR - tm.monthOfYear]));
+ break;
+ }
+ case DCH_W:
+ out.append(String.format("%d", (tm.dayOfMonth - 1) / 7 + 1));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ case DCH_J:
+ out.append(String.format("%d", DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth)));
+ if (S_THth(node.suffix) != 0) {
+ str_numth(out, out, S_TH_TYPE(node.suffix));
+ }
+ break;
+ }
+ }
+ }
+
+ /**
+ * Return ST/ND/RD/TH for simple (1..9) numbers
+ * type --> 0 upper, 1 lower
+ * @param num
+ * @param type
+ * @return
+ */
+ static String get_th(StringBuilder num, int type) {
+ int len = num.length();
+ char last = num.charAt(len - 1);
+
+ if (!Character.isDigit(last)) {
+ throw new IllegalArgumentException("\"" + num.toString() + "\" is not a number");
+ }
+
+ /*
+ * All "teens" (<x>1[0-9]) get 'TH/th', while <x>[02-9][123] still get
+ * 'ST/st', 'ND/nd', 'RD/rd', respectively
+ */
+ char seclast;
+ if ((len > 1) && ((seclast = num.charAt(len - 2)) == '1')) {
+ last = 0;
+ }
+
+ switch (last) {
+ case '1':
+ if (type == TH_UPPER)
+ return numTH[0];
+ return numth[0];
+ case '2':
+ if (type == TH_UPPER)
+ return numTH[1];
+ return numth[1];
+ case '3':
+ if (type == TH_UPPER)
+ return numTH[2];
+ return numth[2];
+ default:
+ if (type == TH_UPPER)
+ return numTH[3];
+ return numth[3];
+ }
+ }
+
+ /**
+ * Convert string-number to ordinal string-number
+ * type --> 0 upper, 1 lower
+ * @param dest
+ * @param num
+ * @param type
+ */
+ static void str_numth(StringBuilder dest, StringBuilder num, int type) {
+ if (!dest.equals(num)) {
+ dest.append(num);
+ }
+ dest.append(get_th(num, type));
+ }
+
+ private static void invalidForInterval(boolean isInterval, FormatNode node) {
+ if (isInterval) {
+ throw new IllegalArgumentException("\"" + node.key.name + "\" not support for interval");
+ }
+ }
+}
[6/6] git commit: TAJO-825: Datetime type refactoring. (Hyoungjun Kim
via jihoon)
Posted by ji...@apache.org.
TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/526dca28
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/526dca28
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/526dca28
Branch: refs/heads/master
Commit: 526dca28b50d10973735accc87adc787de2db09d
Parents: 98f142c
Author: Jihoon Son <ji...@apache.org>
Authored: Thu May 22 10:32:26 2014 +0900
Committer: Jihoon Son <ji...@apache.org>
Committed: Thu May 22 10:32:26 2014 +0900
----------------------------------------------------------------------
CHANGES | 2 +
.../org/apache/tajo/catalog/CatalogServer.java | 8 +-
.../org/apache/tajo/jdbc/TajoResultSetBase.java | 23 +-
.../java/org/apache/tajo/conf/TajoConf.java | 80 +-
.../java/org/apache/tajo/datum/DateDatum.java | 231 +-
.../org/apache/tajo/datum/DatumFactory.java | 69 +-
.../java/org/apache/tajo/datum/Float4Datum.java | 12 +-
.../java/org/apache/tajo/datum/Float8Datum.java | 12 +-
.../java/org/apache/tajo/datum/Int2Datum.java | 12 +-
.../java/org/apache/tajo/datum/Int4Datum.java | 12 +-
.../java/org/apache/tajo/datum/Int8Datum.java | 12 +-
.../org/apache/tajo/datum/IntervalDatum.java | 43 +-
.../java/org/apache/tajo/datum/TimeDatum.java | 124 +-
.../org/apache/tajo/datum/TimestampDatum.java | 255 ++-
.../exception/ValueOutOfRangeException.java | 25 +
.../java/org/apache/tajo/json/DatumAdapter.java | 5 +-
.../org/apache/tajo/util/TimeStampUtil.java | 66 -
.../tajo/util/datetime/DateTimeConstants.java | 631 ++++++
.../tajo/util/datetime/DateTimeFormat.java | 2143 ++++++++++++++++++
.../apache/tajo/util/datetime/DateTimeUtil.java | 2102 +++++++++++++++++
.../org/apache/tajo/util/datetime/TimeMeta.java | 154 ++
.../org/apache/tajo/datum/TestDateDatum.java | 7 -
.../apache/tajo/datum/TestIntervalDatum.java | 77 +-
.../org/apache/tajo/datum/TestTimeDatum.java | 7 -
.../apache/tajo/datum/TestTimestampDatum.java | 121 +-
.../apache/tajo/util/TestDateTimeFormat.java | 158 ++
.../org/apache/tajo/util/TestDateTimeUtil.java | 439 ++++
.../org/apache/tajo/util/TestTimeStampUtil.java | 75 -
.../function/datetime/DatePartFromDate.java | 9 +-
.../function/datetime/DatePartFromTime.java | 43 +-
.../datetime/DatePartFromTimestamp.java | 90 +-
.../datetime/DateTimePartFromUnixTimeStamp.java | 166 +-
.../function/datetime/ToCharTimestamp.java | 28 +-
.../tajo/engine/function/datetime/ToDate.java | 23 +-
.../engine/function/datetime/ToTimestamp.java | 55 -
.../function/datetime/ToTimestampInt.java | 55 +
.../function/datetime/ToTimestampText.java | 62 +
.../tajo/engine/planner/ExprAnnotator.java | 43 +-
.../engine/planner/UniformRangePartition.java | 4 +-
.../apache/tajo/engine/eval/ExprTestBase.java | 21 +-
.../tajo/engine/eval/TestIntervalType.java | 62 +-
.../tajo/engine/eval/TestSQLDateTimeTypes.java | 16 +-
.../tajo/engine/eval/TestSQLExpression.java | 28 +-
.../engine/function/TestDateTimeFunctions.java | 164 +-
.../apache/tajo/engine/query/TestSortQuery.java | 21 +-
.../storage/TextSerializerDeserializer.java | 15 +-
.../org/apache/tajo/storage/TestStorages.java | 2 +-
47 files changed, 6901 insertions(+), 911 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 8281614..5645b13 100644
--- a/CHANGES
+++ b/CHANGES
@@ -15,6 +15,8 @@ Release 0.9.0 - unreleased
IMPROVEMENT
+ TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
+
TAJO-811: add simple fifo scheduler support. (jinho)
TAJO-801: Multiple distinct should be supported. (Hyoungjun Kim via hyunsik)
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
----------------------------------------------------------------------
diff --git a/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java b/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
index c0f6d36..4e391aa 100644
--- a/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
+++ b/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
@@ -864,10 +864,12 @@ public class CatalogServer extends AbstractService {
* to implement compareTo so decided to take the shortcut.
*
* */
- for (FunctionDescProto existing : functions.get(signature)) {
- if (existing.getParameterTypesList() != null &&
+ if (functions.containsKey(signature)) {
+ for (FunctionDescProto existing : functions.get(signature)) {
+ if (existing.getParameterTypesList() != null &&
CatalogUtil.isMatchedFunction(existing.getParameterTypesList(), params)) {
- return existing;
+ return existing;
+ }
}
}
return null;
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java
----------------------------------------------------------------------
diff --git a/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java b/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java
index 4c307b3..60eb2f1 100644
--- a/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java
+++ b/tajo-client/src/main/java/org/apache/tajo/jdbc/TajoResultSetBase.java
@@ -20,8 +20,11 @@ package org.apache.tajo.jdbc;
import org.apache.tajo.catalog.Schema;
import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.conf.TajoConf;
import org.apache.tajo.datum.Datum;
import org.apache.tajo.datum.NullDatum;
+import org.apache.tajo.datum.TimeDatum;
+import org.apache.tajo.datum.TimestampDatum;
import org.apache.tajo.storage.Tuple;
import java.io.IOException;
@@ -178,6 +181,12 @@ public abstract class TajoResultSetBase implements ResultSet {
case FLOAT4: return d.asFloat4();
case FLOAT8: return d.asFloat8();
case NUMERIC: return d.asFloat8();
+ case TIME: {
+ return ((TimeDatum)d).asChars(TajoConf.getCurrentTimeZone(), false);
+ }
+ case TIMESTAMP: {
+ return ((TimestampDatum)d).asChars(TajoConf.getCurrentTimeZone(), false);
+ }
default: return d.asChars();
}
}
@@ -212,7 +221,19 @@ public abstract class TajoResultSetBase implements ResultSet {
public String getString(String name) throws SQLException {
Datum datum = cur.get(findColumn(name));
handleNull(datum);
- return datum.asChars();
+
+ TajoDataTypes.Type dataType = datum.type();
+
+ switch(dataType) {
+ case TIME: {
+ return ((TimeDatum)datum).asChars(TajoConf.getCurrentTimeZone(), false);
+ }
+ case TIMESTAMP: {
+ return ((TimestampDatum)datum).asChars(TajoConf.getCurrentTimeZone(), false);
+ }
+ default :
+ return datum.asChars();
+ }
}
@Override
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java b/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java
index 6580525..b62b246 100644
--- a/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java
+++ b/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java
@@ -25,14 +25,24 @@ import org.apache.hadoop.fs.Path;
import org.apache.tajo.TajoConstants;
import org.apache.tajo.util.NetUtils;
import org.apache.tajo.util.TUtil;
+import org.apache.tajo.util.datetime.DateTimeConstants;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.util.Map;
+import java.util.TimeZone;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
public class TajoConf extends Configuration {
+ private static TimeZone CURRENT_TIMEZONE;
+ private static int DATE_ORDER = -1;
+ private static final ReentrantReadWriteLock confLock = new ReentrantReadWriteLock();
+ private static final Lock writeLock = confLock.writeLock();
+ private static final Lock readLock = confLock.readLock();
+
static {
Configuration.addDefaultResource("catalog-default.xml");
Configuration.addDefaultResource("catalog-site.xml");
@@ -40,6 +50,8 @@ public class TajoConf extends Configuration {
Configuration.addDefaultResource("storage-site.xml");
Configuration.addDefaultResource("tajo-default.xml");
Configuration.addDefaultResource("tajo-site.xml");
+
+ confStaticInit();
}
private static final String EMPTY_VALUE = "";
@@ -59,6 +71,68 @@ public class TajoConf extends Configuration {
addResource(path);
}
+ private static void confStaticInit() {
+ TimeZone.setDefault(getCurrentTimeZone());
+ getDateOrder();
+ }
+
+ public static TimeZone getCurrentTimeZone() {
+ writeLock.lock();
+ try {
+ if (CURRENT_TIMEZONE == null) {
+ TajoConf tajoConf = new TajoConf();
+ CURRENT_TIMEZONE = TimeZone.getTimeZone(tajoConf.getVar(ConfVars.TAJO_TIMEZONE));
+ }
+ return CURRENT_TIMEZONE;
+ } finally {
+ writeLock.unlock();
+ }
+ }
+
+ public static TimeZone setCurrentTimeZone(TimeZone timeZone) {
+ readLock.lock();
+ try {
+ TimeZone oldTimeZone = CURRENT_TIMEZONE;
+ CURRENT_TIMEZONE = timeZone;
+ return oldTimeZone;
+ } finally {
+ readLock.unlock();
+ }
+ }
+
+ public static int getDateOrder() {
+ writeLock.lock();
+ try {
+ if (DATE_ORDER < 0) {
+ TajoConf tajoConf = new TajoConf();
+ String dateOrder = tajoConf.getVar(ConfVars.TAJO_DATE_ORDER);
+ if ("YMD".equals(dateOrder)) {
+ DATE_ORDER = DateTimeConstants.DATEORDER_YMD;
+ } else if ("DMY".equals(dateOrder)) {
+ DATE_ORDER = DateTimeConstants.DATEORDER_DMY;
+ } else if ("MDY".equals(dateOrder)) {
+ DATE_ORDER = DateTimeConstants.DATEORDER_MDY;
+ } else {
+ DATE_ORDER = DateTimeConstants.DATEORDER_YMD;
+ }
+ }
+ return DATE_ORDER;
+ } finally {
+ writeLock.unlock();
+ }
+ }
+
+ public static int setDateOrder(int dateOrder) {
+ readLock.lock();
+ try {
+ int oldDateOrder = DATE_ORDER;
+ DATE_ORDER = dateOrder;
+ return oldDateOrder;
+ } finally {
+ readLock.unlock();
+ }
+ }
+
public static enum ConfVars {
//////////////////////////////////
@@ -249,7 +323,11 @@ public class TajoConf extends Configuration {
CLI_PRINT_PAUSE_NUM_RECORDS("tajo.cli.print.pause.num.records", 100),
CLI_PRINT_PAUSE("tajo.cli.print.pause", true),
CLI_PRINT_ERROR_TRACE("tajo.cli.print.error.trace", true),
- CLI_OUTPUT_FORMATTER_CLASS("tajo.cli.output.formatter", "org.apache.tajo.cli.DefaultTajoCliOutputFormatter");
+ CLI_OUTPUT_FORMATTER_CLASS("tajo.cli.output.formatter", "org.apache.tajo.cli.DefaultTajoCliOutputFormatter"),
+
+ //TIME & DATE
+ TAJO_TIMEZONE("tajo.timezone", System.getProperty("user.timezone")),
+ TAJO_DATE_ORDER("tajo.date.order", "YMD")
;
public final String varname;
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
index ad68303..10f7077 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/DateDatum.java
@@ -19,80 +19,83 @@
package org.apache.tajo.datum;
import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.common.TajoDataTypes.Type;
import org.apache.tajo.exception.InvalidCastException;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.Bytes;
-import org.joda.time.*;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
+import org.apache.tajo.util.datetime.DateTimeConstants.DateStyle;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
public class DateDatum extends Datum {
public static final int SIZE = 4;
- /** ISO 8601/SQL standard format - ex) 1997-12-17 */
- public static final String DEFAULT_FORMAT_STRING = "yyyy-MM-dd";
- private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormat.forPattern(DEFAULT_FORMAT_STRING);
- private final LocalDate date;
+
+ private int year;
+ private int monthOfYear;
+ private int dayOfMonth;
public DateDatum(int value) {
super(TajoDataTypes.Type.DATE);
- date = decode(value);
- }
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.j2date(value, tm);
- public DateDatum(int year, int month, int day) {
- super(TajoDataTypes.Type.DATE);
- date = new LocalDate(year, month, day);
+ year = tm.years;
+ monthOfYear = tm.monthOfYear;
+ dayOfMonth = tm.dayOfMonth;
}
- public DateDatum(String dateStr) {
- super(TajoDataTypes.Type.DATE);
- this.date = LocalDate.parse(dateStr, DEFAULT_FORMATTER);
- }
-
- public DateDatum(LocalDate date) {
- super(TajoDataTypes.Type.DATE);
- this.date = date;
+ public TimeMeta toTimeMeta() {
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.j2date(DateTimeUtil.date2j(year, monthOfYear, dayOfMonth), tm);
+ return tm;
}
- public LocalDate getDate() {
- //LocalDate is immutable
- return date;
+ public int getCenturyOfEra() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getCenturyOfEra();
}
- public DateDatum(byte [] bytes) {
- this(Bytes.toInt(bytes));
+ public int getYear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.years;
}
- public int getCenturyOfEra() {
- return date.getCenturyOfEra();
+ public int getWeekyear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getWeekyear();
}
- public int getYear() {
- return date.getYear();
+ public int getMonthOfYear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.monthOfYear;
}
- public int getMonthOfYear() {
- return date.getMonthOfYear();
+ public int getDayOfYear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getDayOfYear();
}
- public int getWeekyear() {
- return date.getWeekyear();
+ public int getDayOfWeek() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getDayOfWeek();
}
- public int getWeekOfWeekyear() {
- return date.getWeekOfWeekyear();
+ public int getISODayOfWeek() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getISODayOfWeek();
}
- public int getDayOfWeek() {
- return date.getDayOfWeek();
+ public int getWeekOfYear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getWeekOfYear();
}
public int getDayOfMonth() {
- return date.getDayOfMonth();
+ TimeMeta tm = toTimeMeta();
+ return tm.dayOfMonth;
}
- public int getDayOfYear() {
- return date.getDayOfYear();
- }
public String toString() {
return asChars();
@@ -104,19 +107,31 @@ public class DateDatum extends Datum {
case INT4:
case INT8:
case FLOAT4:
- case FLOAT8:
- return new DateDatum(date.plusDays(datum.asInt2()));
- case INTERVAL:
- IntervalDatum interval = (IntervalDatum)datum;
- LocalDate localDate;
+ case FLOAT8: {
+ TimeMeta tm = toTimeMeta();
+ tm.plusDays(datum.asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
+ }
+ case INTERVAL: {
+ IntervalDatum interval = (IntervalDatum) datum;
+ TimeMeta tm = toTimeMeta();
+ tm.plusMillis(interval.getMilliSeconds());
if (interval.getMonths() > 0) {
- localDate = date.plusMonths(interval.getMonths());
- } else {
- localDate = date;
+ tm.plusMonths(interval.getMonths());
}
- return new TimestampDatum(localDate.toDateTimeAtStartOfDay().getMillis() + interval.getMilliSeconds());
- case TIME:
- return new TimestampDatum(createDateTime(date, ((TimeDatum)datum).getTime(), true));
+ DateTimeUtil.toUTCTimezone(tm);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ }
+ case TIME: {
+ TimeMeta tm1 = toTimeMeta();
+
+ TimeMeta tm2 = ((TimeDatum)datum).toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm2); //TimeDatum is UTC
+
+ tm1.plusTime(DateTimeUtil.toTime(tm2));
+ DateTimeUtil.toUTCTimezone(tm1);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm1));
+ }
default:
throw new InvalidOperationException(datum.type());
}
@@ -128,68 +143,51 @@ public class DateDatum extends Datum {
case INT4:
case INT8:
case FLOAT4:
- case FLOAT8:
- return new DateDatum(date.minusDays(datum.asInt2()));
- case INTERVAL:
- IntervalDatum interval = (IntervalDatum)datum;
- LocalDate localDate;
+ case FLOAT8: {
+ TimeMeta tm = toTimeMeta();
+ tm.plusDays(0 - datum.asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
+ }
+ case INTERVAL: {
+ IntervalDatum interval = (IntervalDatum) datum;
+ TimeMeta tm = toTimeMeta();
if (interval.getMonths() > 0) {
- localDate = date.minusMonths(interval.getMonths());
- } else {
- localDate = date;
+ tm.plusMonths(0 - interval.getMonths());
}
- return new TimestampDatum(localDate.toDateTimeAtStartOfDay().getMillis() - interval.getMilliSeconds());
- case TIME:
- return new TimestampDatum(createDateTime(date, ((TimeDatum)datum).getTime(), false));
- case DATE:
- return new Int4Datum(Days.daysBetween(((DateDatum)datum).date, date).getDays());
+ tm.plusMillis(0 - interval.getMilliSeconds());
+ DateTimeUtil.toUTCTimezone(tm);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ }
+ case TIME: {
+ TimeMeta tm1 = toTimeMeta();
+
+ TimeMeta tm2 = ((TimeDatum)datum).toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm2); //TimeDatum is UTC
+
+ tm1.plusTime(0 - DateTimeUtil.toTime(tm2));
+ DateTimeUtil.toUTCTimezone(tm1);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm1));
+ }
+ case DATE: {
+ TimeMeta tm1 = toTimeMeta();
+ TimeMeta tm2 = ((DateDatum) datum).toTimeMeta();
+
+ int day1 = DateTimeUtil.date2j(tm1.years, tm1.monthOfYear, tm1.dayOfMonth);
+ int day2 = DateTimeUtil.date2j(tm2.years, tm2.monthOfYear, tm2.dayOfMonth);
+ return new Int4Datum(day1 - day2);
+ }
default:
throw new InvalidOperationException(datum.type());
}
}
- public static DateTime createDateTime(LocalDate date, LocalTime time, boolean plus) {
- //TODO create too many temporary instance. This must be improved.
- DateTime dateTime = new DateTime(date.toDate().getTime());
- if (plus) {
- return dateTime
- .plusHours(time.getHourOfDay())
- .plusMinutes(time.getMinuteOfHour())
- .plusSeconds(time.getSecondOfMinute())
- .plusMillis(time.getMillisOfSecond());
- } else {
- return dateTime
- .minusHours(time.getHourOfDay())
- .minusMinutes(time.getMinuteOfHour())
- .minusSeconds(time.getSecondOfMinute())
- .minusMillis(time.getMillisOfSecond());
- }
- }
-
@Override
public int asInt4() {
return encode();
}
- private static LocalDate decode(int val) {
- int year = (val >> 16);
- int monthOfYear = (0xFFFF & val) >> 8;
- int dayOfMonth = (0x00FF & val);
- return new LocalDate(year, monthOfYear, dayOfMonth);
- }
-
- /**
- * Year MonthOfYear DayOfMonth
- * 31-16 15-8 7 - 0
- *
- * 0xFF 0xFF 0xFF 0xFF
- */
private int encode() {
- int instance = 0;
- instance |= (date.getYear() << 16); // 1970 ~ : 2 bytes
- instance |= (date.getMonthOfYear() << 8); // 1 - 12 : 1 byte
- instance |= (date.getDayOfMonth()); // 0 - 31 : 1 byte
- return instance;
+ return DateTimeUtil.date2j(year, monthOfYear, dayOfMonth);
}
@Override
@@ -209,11 +207,13 @@ public class DateDatum extends Datum {
@Override
public String asChars() {
- return date.toString(DEFAULT_FORMATTER);
+ TimeMeta tm = toTimeMeta();
+ return DateTimeUtil.encodeDate(tm, DateStyle.ISO_DATES);
}
public String toChars(String format) {
- return date.toString(format);
+ TimeMeta tm = toTimeMeta();
+ return DateTimeFormat.to_char(tm, format);
}
@Override
@@ -228,8 +228,8 @@ public class DateDatum extends Datum {
@Override
public Datum equalsTo(Datum datum) {
- if (datum.type() == TajoDataTypes.Type.TIME) {
- return DatumFactory.createBool(date.equals(((DateDatum) datum).date));
+ if (datum.type() == Type.DATE) {
+ return DatumFactory.createBool(equals(datum));
} else if (datum.isNull()) {
return datum;
} else {
@@ -240,7 +240,17 @@ public class DateDatum extends Datum {
@Override
public int compareTo(Datum datum) {
if (datum.type() == TajoDataTypes.Type.DATE) {
- return date.compareTo(((DateDatum)datum).date);
+ DateDatum another = (DateDatum) datum;
+ int compareResult = (year < another.year) ? -1 : ((year == another.year) ? 0 : 1);
+ if (compareResult != 0) {
+ return compareResult;
+ }
+ compareResult = (monthOfYear < another.monthOfYear) ? -1 : ((monthOfYear == another.monthOfYear) ? 0 : 1);
+ if (compareResult != 0) {
+ return compareResult;
+ }
+
+ return (dayOfMonth < another.dayOfMonth) ? -1 : ((dayOfMonth == another.dayOfMonth) ? 0 : 1);
} else if (datum instanceof NullDatum || datum.isNull()) {
return -1;
} else {
@@ -251,7 +261,7 @@ public class DateDatum extends Datum {
public boolean equals(Object obj) {
if (obj instanceof DateDatum) {
DateDatum another = (DateDatum) obj;
- return date.isEqual(another.date);
+ return year == another.year && monthOfYear == another.monthOfYear && dayOfMonth == another.dayOfMonth;
} else {
return false;
}
@@ -259,6 +269,11 @@ public class DateDatum extends Datum {
@Override
public int hashCode() {
- return date.hashCode();
+ int total = 157;
+ total = 23 * total + year;
+ total = 23 * total + monthOfYear;
+ total = 23 * total + dayOfMonth;
+
+ return total;
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java b/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
index 5ce36e5..7d6ef18 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/DatumFactory.java
@@ -23,9 +23,9 @@ import org.apache.tajo.common.TajoDataTypes.DataType;
import org.apache.tajo.common.TajoDataTypes.Type;
import org.apache.tajo.exception.InvalidCastException;
import org.apache.tajo.util.Bytes;
-import org.joda.time.DateTime;
-import org.joda.time.LocalDate;
-import org.joda.time.LocalTime;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.io.IOException;
@@ -96,7 +96,7 @@ public class DatumFactory {
case TIME:
return createTime(value);
case TIMESTAMP:
- return createTimeStamp(value);
+ return createTimestamp(value);
case INTERVAL:
return createInterval(value);
case BLOB:
@@ -128,11 +128,11 @@ public class DatumFactory {
case TEXT:
return createText(bytes);
case DATE:
- return new DateDatum(bytes);
+ return new DateDatum(Bytes.toInt(bytes));
case TIME:
- return new TimeDatum(bytes);
+ return new TimeDatum(Bytes.toLong(bytes));
case TIMESTAMP:
- return new TimestampDatum(bytes);
+ return new TimestampDatum(Bytes.toLong(bytes));
case BIT:
return createBit(bytes[0]);
case BLOB:
@@ -170,7 +170,7 @@ public class DatumFactory {
case INT8:
return new Int8Datum(val);
case TIMESTAMP:
- return createTimeStampFromMillis(val);
+ return new TimestampDatum(val);
case TIME:
return createTime(val);
default:
@@ -266,8 +266,13 @@ public class DatumFactory {
return new DateDatum(instance);
}
+ public static DateDatum createDate(int year, int month, int day) {
+ return new DateDatum(DateTimeUtil.date2j(year, month, day));
+ }
+
public static DateDatum createDate(String dateStr) {
- return new DateDatum(LocalDate.parse(dateStr));
+ TimeMeta tm = DateTimeUtil.decodeDateTime(dateStr);
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
}
public static TimeDatum createTime(long instance) {
@@ -275,19 +280,20 @@ public class DatumFactory {
}
public static TimeDatum createTime(String dateStr) {
- return new TimeDatum(LocalTime.parse(dateStr));
+ TimeMeta tm = DateTimeUtil.decodeDateTime(dateStr);
+ return new TimeDatum(DateTimeUtil.toTime(tm));
}
- public static TimestampDatum createTimeStamp(int unixTime) {
- return new TimestampDatum(unixTime);
+ public static TimestampDatum createTimestmpDatumWithJavaMillis(long millis) {
+ return new TimestampDatum(DateTimeUtil.javaTimeToJulianTime(millis));
}
- public static TimestampDatum createTimeStampFromMillis(long millis) {
- return new TimestampDatum(new DateTime(millis));
+ public static TimestampDatum createTimestmpDatumWithUnixTime(int unixTime) {
+ return createTimestmpDatumWithJavaMillis(unixTime * 1000L);
}
- public static TimestampDatum createTimeStamp(String timeStamp) {
- return new TimestampDatum(timeStamp);
+ public static TimestampDatum createTimestamp(String datetimeStr) {
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(datetimeStr));
}
public static IntervalDatum createInterval(String intervalStr) {
@@ -301,7 +307,7 @@ public class DatumFactory {
case INT8:
return new DateDatum(datum.asInt4());
case TEXT:
- return new DateDatum(datum.asChars());
+ return createDate(datum.asChars());
case DATE:
return (DateDatum) datum;
default:
@@ -314,7 +320,9 @@ public class DatumFactory {
case INT8:
return new TimeDatum(datum.asInt8());
case TEXT:
- return new TimeDatum(datum.asChars());
+ TimeMeta tm = DateTimeFormat.parseDateTime(datum.asChars(), "HH24:MI:SS.MS");
+ DateTimeUtil.toUTCTimezone(tm);
+ return new TimeDatum(DateTimeUtil.toTime(tm));
case TIME:
return (TimeDatum) datum;
default:
@@ -325,7 +333,11 @@ public class DatumFactory {
public static TimestampDatum createTimestamp(Datum datum) {
switch (datum.type()) {
case TEXT:
- return new TimestampDatum(datum.asChars());
+ long timestamp = DateTimeUtil.toJulianTimestamp(datum.asChars());
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.toJulianTimeMeta(timestamp, tm);
+ DateTimeUtil.toUTCTimezone(tm);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
case TIMESTAMP:
return (TimestampDatum) datum;
default:
@@ -375,7 +387,24 @@ public class DatumFactory {
case FLOAT8:
return DatumFactory.createFloat8(operandDatum.asFloat8());
case TEXT:
- return DatumFactory.createText(operandDatum.asTextBytes());
+ switch (operandDatum.type()) {
+ case TIMESTAMP: {
+ TimestampDatum timestampDatum = (TimestampDatum)operandDatum;
+ TimeMeta tm = timestampDatum.toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm);
+ TimestampDatum convertedTimestampDatum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ return DatumFactory.createText(convertedTimestampDatum.asTextBytes());
+ }
+ case TIME: {
+ TimeDatum timeDatum = (TimeDatum)operandDatum;
+ TimeMeta tm = timeDatum.toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm);
+ TimeDatum convertedTimeDatum = new TimeDatum(DateTimeUtil.toTime(tm));
+ return DatumFactory.createText(convertedTimeDatum.asTextBytes());
+ }
+ default:
+ return DatumFactory.createText(operandDatum.asTextBytes());
+ }
case DATE:
return DatumFactory.createDate(operandDatum);
case TIME:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/Float4Datum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/Float4Datum.java b/tajo-common/src/main/java/org/apache/tajo/datum/Float4Datum.java
index 35dca00..c269a40 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/Float4Datum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/Float4Datum.java
@@ -23,6 +23,8 @@ import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidCastException;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.NumberUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.nio.ByteBuffer;
@@ -211,7 +213,10 @@ public class Float4Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val + datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().plusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
@@ -233,7 +238,10 @@ public class Float4Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val - datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().minusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(0 - asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/Float8Datum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/Float8Datum.java b/tajo-common/src/main/java/org/apache/tajo/datum/Float8Datum.java
index b7d8cf4..b4cfc67 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/Float8Datum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/Float8Datum.java
@@ -23,6 +23,8 @@ import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.Bytes;
import org.apache.tajo.util.NumberUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.nio.ByteBuffer;
@@ -200,7 +202,10 @@ public class Float8Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val + datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().plusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
@@ -222,7 +227,10 @@ public class Float8Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val - datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().minusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(0 - asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/Int2Datum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/Int2Datum.java b/tajo-common/src/main/java/org/apache/tajo/datum/Int2Datum.java
index 731ccdc..7770b94 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/Int2Datum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/Int2Datum.java
@@ -22,6 +22,8 @@ import com.google.gson.annotations.Expose;
import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.NumberUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.nio.ByteBuffer;
@@ -202,7 +204,10 @@ public class Int2Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val + datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().plusDays(asInt2()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(asInt2());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
@@ -224,7 +229,10 @@ public class Int2Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val - datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().minusDays(asInt2()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(0 - asInt2());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/Int4Datum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/Int4Datum.java b/tajo-common/src/main/java/org/apache/tajo/datum/Int4Datum.java
index 5b60e9e..7f7ac13 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/Int4Datum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/Int4Datum.java
@@ -22,6 +22,8 @@ import com.google.gson.annotations.Expose;
import org.apache.tajo.common.TajoDataTypes.Type;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.NumberUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.nio.ByteBuffer;
@@ -206,7 +208,10 @@ public class Int4Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val + datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().plusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
@@ -228,7 +233,10 @@ public class Int4Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val - datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().minusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(0 - asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/Int8Datum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/Int8Datum.java b/tajo-common/src/main/java/org/apache/tajo/datum/Int8Datum.java
index 0367f9c..3b1e67d 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/Int8Datum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/Int8Datum.java
@@ -23,6 +23,8 @@ import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidCastException;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.NumberUtil;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.nio.ByteBuffer;
@@ -214,7 +216,10 @@ public class Int8Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val + datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().plusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
@@ -236,7 +241,10 @@ public class Int8Datum extends NumericDatum {
case FLOAT8:
return DatumFactory.createFloat8(val - datum.asFloat8());
case DATE:
- return new DateDatum(((DateDatum)datum).getDate().minusDays(asInt4()));
+ DateDatum dateDatum = (DateDatum)datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusDays(0 - asInt4());
+ return new DateDatum(DateTimeUtil.date2j(tm.years, tm.monthOfYear, tm.dayOfMonth));
case NULL_TYPE:
return datum;
default:
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java
index b6904a9..5ad9635 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/IntervalDatum.java
@@ -21,9 +21,8 @@ package org.apache.tajo.datum;
import com.google.common.base.Objects;
import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidOperationException;
-import org.joda.time.DateTime;
-import org.joda.time.LocalDate;
-import org.joda.time.LocalTime;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
import java.text.DecimalFormat;
import java.util.HashMap;
@@ -231,28 +230,32 @@ public class IntervalDatum extends Datum {
case INTERVAL:
IntervalDatum other = (IntervalDatum) datum;
return new IntervalDatum(months + other.months, millieconds + other.millieconds);
- case DATE:
- LocalDate date = ((DateDatum)datum).getDate();
- LocalDate localDate;
- if (months > 0) {
- localDate = date.plusMonths(months);
- } else {
- localDate = date;
+ case DATE: {
+ DateDatum dateDatum = (DateDatum) datum;
+ TimeMeta tm = dateDatum.toTimeMeta();
+ tm.plusMillis(getMilliSeconds());
+ if (getMonths() > 0) {
+ tm.plusMonths(getMonths());
}
- return new TimestampDatum(localDate.toDateTimeAtStartOfDay().getMillis() + millieconds);
- case TIME:
- LocalTime localTime = ((TimeDatum)datum).getTime();
- localTime = localTime.plusMillis((int) millieconds);
- return new TimeDatum(localTime);
- case TIMESTAMP:
- DateTime dateTime = ((TimestampDatum) datum).getDateTime();
+ DateTimeUtil.toUTCTimezone(tm);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ }
+ case TIME: {
+ TimeMeta tm = ((TimeDatum) datum).toTimeMeta();
+ tm.plusMillis(millieconds);
+ return new TimeDatum(DateTimeUtil.toTime(tm));
+ }
+ case TIMESTAMP: {
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.toJulianTimeMeta(((TimestampDatum) datum).asInt8(), tm);
if (months > 0) {
- dateTime = dateTime.plusMonths(months);
+ tm.plusMonths(months);
}
if (millieconds > 0) {
- dateTime = dateTime.plusMillis((int) millieconds);
+ tm.plusMillis(millieconds);
}
- return new TimestampDatum(dateTime);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ }
default:
throw new InvalidOperationException(datum.type());
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
index 70f62c2..a7b9d1e 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/TimeDatum.java
@@ -22,65 +22,47 @@ import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidCastException;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.Bytes;
-import org.joda.time.DateTime;
-import org.joda.time.LocalTime;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
+import org.apache.tajo.util.datetime.DateTimeConstants.DateStyle;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
+
+import java.util.TimeZone;
public class TimeDatum extends Datum {
public static final int SIZE = 8;
- /** ISO 8601/SQL standard format - ex) 07:37:16-08 */
- public static final String DEFAULT_FORMAT_STRING = "HH:mm:ss";
- private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormat.forPattern(DEFAULT_FORMAT_STRING);
- private final LocalTime time;
-
- public TimeDatum(long value) {
- super(TajoDataTypes.Type.TIME);
- time = new LocalTime(value);
- }
-
- public TimeDatum(int hour, int minute, int second) {
- super(TajoDataTypes.Type.TIME);
- time = new LocalTime(hour, minute, second);
- }
-
- public TimeDatum(int hour, int minute, int second, int millis) {
- super(TajoDataTypes.Type.TIME);
- time = new LocalTime(hour, minute, second, millis);
- }
+ private final long time;
- public TimeDatum(String timeStr) {
- super(TajoDataTypes.Type.TIME);
- time = LocalTime.parse(timeStr, DEFAULT_FORMATTER);
- }
-
- public TimeDatum(LocalTime time) {
+ public TimeDatum(long time) {
super(TajoDataTypes.Type.TIME);
this.time = time;
}
- public TimeDatum(byte [] bytes) {
- this(Bytes.toLong(bytes));
+ public TimeMeta toTimeMeta() {
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.date2j(time, tm);
+
+ return tm;
}
public int getHourOfDay() {
- return time.getHourOfDay();
+ TimeMeta tm = toTimeMeta();
+ return tm.hours;
}
public int getMinuteOfHour() {
- return time.getMinuteOfHour();
+ TimeMeta tm = toTimeMeta();
+ return tm.minutes;
}
public int getSecondOfMinute() {
- return time.getSecondOfMinute();
- }
-
- public int getMillisOfDay() {
- return time.getMillisOfDay();
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.date2j(time, tm);
+ return tm.secs;
}
public int getMillisOfSecond() {
- return time.getMillisOfSecond();
+ TimeMeta tm = toTimeMeta();
+ return tm.fsecs / 1000;
}
public String toString() {
@@ -94,7 +76,7 @@ public class TimeDatum extends Datum {
@Override
public long asInt8() {
- return time.toDateTimeToday().getMillis();
+ return time;
}
@Override
@@ -109,11 +91,21 @@ public class TimeDatum extends Datum {
@Override
public String asChars() {
- return time.toString(DEFAULT_FORMATTER);
+ TimeMeta tm = toTimeMeta();
+ return DateTimeUtil.encodeTime(tm, DateStyle.ISO_DATES);
}
- public String toChars(String format) {
- return time.toString(format);
+ public String asChars(TimeZone timeZone, boolean includeTimeZone) {
+ TimeMeta tm = toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm, timeZone);
+ if (includeTimeZone) {
+ tm.timeZone = timeZone.getRawOffset() / 1000;
+ }
+ return DateTimeUtil.encodeTime(tm, DateStyle.ISO_DATES);
+ }
+
+ public String toString(TimeZone timeZone, boolean includeTimeZone) {
+ return asChars(timeZone, includeTimeZone);
}
@Override
@@ -128,12 +120,23 @@ public class TimeDatum extends Datum {
public Datum plus(Datum datum) {
switch(datum.type()) {
- case INTERVAL:
+ case INTERVAL: {
IntervalDatum interval = ((IntervalDatum)datum);
- return new TimeDatum(time.plusMillis((int)interval.getMilliSeconds()));
- case DATE:
- DateTime dateTime = DateDatum.createDateTime(((DateDatum)datum).getDate(), time, true);
- return new TimestampDatum(dateTime);
+ TimeMeta tm = toTimeMeta();
+ tm.plusMillis(interval.getMilliSeconds());
+ return new TimeDatum(DateTimeUtil.toTime(tm));
+ }
+ case DATE: {
+ TimeMeta tm = toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm); //TimeDatum is UTC
+
+ DateDatum dateDatum = (DateDatum) datum;
+ TimeMeta dateTm = dateDatum.toTimeMeta();
+ dateTm.plusTime(DateTimeUtil.toTime(tm));
+
+ DateTimeUtil.toUTCTimezone(dateTm);
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(dateTm));
+ }
default:
throw new InvalidOperationException(datum.type());
}
@@ -141,12 +144,17 @@ public class TimeDatum extends Datum {
public Datum minus(Datum datum) {
switch(datum.type()) {
- case INTERVAL:
+ case INTERVAL: {
IntervalDatum interval = ((IntervalDatum)datum);
- return new TimeDatum(time.minusMillis((int)interval.getMilliSeconds()));
+ TimeMeta tm = toTimeMeta();
+ tm.plusMillis(0 - interval.getMilliSeconds());
+ return new TimeDatum(DateTimeUtil.toTime(tm));
+ }
case TIME:
- return new IntervalDatum(
- time.toDateTimeToday().getMillis() - ((TimeDatum)datum).getTime().toDateTimeToday().getMillis() );
+ TimeMeta tm1 = toTimeMeta();
+ TimeMeta tm2 = ((TimeDatum)datum).toTimeMeta();
+
+ return new IntervalDatum((DateTimeUtil.toTime(tm1) - DateTimeUtil.toTime(tm2))/1000);
default:
throw new InvalidOperationException(datum.type());
}
@@ -155,7 +163,7 @@ public class TimeDatum extends Datum {
@Override
public Datum equalsTo(Datum datum) {
if (datum.type() == TajoDataTypes.Type.TIME) {
- return DatumFactory.createBool(time.equals(((TimeDatum) datum).time));
+ return DatumFactory.createBool(time == (((TimeDatum) datum).time));
} else if (datum.isNull()) {
return datum;
} else {
@@ -166,7 +174,8 @@ public class TimeDatum extends Datum {
@Override
public int compareTo(Datum datum) {
if (datum.type() == TajoDataTypes.Type.TIME) {
- return time.compareTo(((TimeDatum)datum).time);
+ TimeDatum another = (TimeDatum)datum;
+ return (time < another.time) ? -1 : ((time == another.time) ? 0 : 1);
} else if (datum instanceof NullDatum || datum.isNull()) {
return -1;
} else {
@@ -177,7 +186,7 @@ public class TimeDatum extends Datum {
public boolean equals(Object obj) {
if (obj instanceof TimeDatum) {
TimeDatum another = (TimeDatum) obj;
- return time.isEqual(another.time);
+ return time == another.time;
} else {
return false;
}
@@ -185,10 +194,7 @@ public class TimeDatum extends Datum {
@Override
public int hashCode() {
- return time.hashCode();
+ return (int)(time ^ (time >>> 32));
}
- public LocalTime getTime() {
- return time;
- }
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java b/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
index 879428b..d3cc5f4 100644
--- a/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
+++ b/tajo-common/src/main/java/org/apache/tajo/datum/TimestampDatum.java
@@ -19,187 +19,138 @@
package org.apache.tajo.datum;
import com.google.common.base.Objects;
-import org.apache.commons.lang.StringUtils;
import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.exception.InvalidOperationException;
import org.apache.tajo.util.Bytes;
-import org.joda.time.DateTime;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
+import org.apache.tajo.util.datetime.DateTimeConstants.DateStyle;
+import org.apache.tajo.util.datetime.DateTimeFormat;
+import org.apache.tajo.util.datetime.DateTimeUtil;
+import org.apache.tajo.util.datetime.TimeMeta;
+
+import java.util.TimeZone;
public class TimestampDatum extends Datum {
public static final int SIZE = 8;
- /** ISO 8601/SQL standard format - ex) 1997-12-17 07:37:16-08 */
- public static final String DEFAULT_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
- public static final String FRACTION_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss.SSS";
- private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormat.forPattern(DEFAULT_FORMAT_STRING);
- private static final DateTimeFormatter FRACTION_FORMATTER = DateTimeFormat.forPattern(FRACTION_FORMAT_STRING);
- private final DateTime dateTime;
-
- public TimestampDatum(int timestamp) {
- super(TajoDataTypes.Type.TIMESTAMP);
- dateTime = new DateTime((long)timestamp * 1000);
- }
+ private long timestamp;
+
+ /**
+ *
+ * @param timestamp UTC based
+ */
public TimestampDatum(long timestamp) {
super(TajoDataTypes.Type.TIMESTAMP);
- dateTime = new DateTime(timestamp);
+ this.timestamp = timestamp;
}
- public TimestampDatum(DateTime dateTime) {
- super(TajoDataTypes.Type.TIMESTAMP);
- this.dateTime = dateTime;
+ /**
+ * It's the same value to asInt8().
+ * @return The Timestamp
+ */
+ public long getTimestamp() {
+ return timestamp;
}
- TimestampDatum(byte [] bytes) {
- super(TajoDataTypes.Type.TIMESTAMP);
- this.dateTime = new DateTime(Bytes.toLong(bytes));
+ public int getEpoch() {
+ return DateTimeUtil.julianTimeToEpoch(timestamp);
}
- public TimestampDatum(String datetime) {
- super(TajoDataTypes.Type.TIMESTAMP);
-
- DateTime tmpDateTime = null;
- try {
- tmpDateTime = DateTime.parse(datetime, DEFAULT_FORMATTER);
- } catch (IllegalArgumentException e) {
- tmpDateTime = DateTime.parse(datetime, FRACTION_FORMATTER);
- }
- this.dateTime = tmpDateTime;
- }
-
- public int getUnixTime() {
- return (int) (dateTime.getMillis() / 1000);
+ public long getJavaTimestamp() {
+ return DateTimeUtil.julianTimeToJavaTime(timestamp);
}
- public long getMillis() {
- return dateTime.getMillis();
- }
-
- public DateTime getDateTime() {
- return dateTime;
- }
public int getCenturyOfEra() {
- return dateTime.getCenturyOfEra();
- }
-
- public int getEra() {
- return dateTime.getEra();
+ TimeMeta tm = toTimeMeta();
+ return tm.getCenturyOfEra();
}
public int getYear() {
- return dateTime.getYear();
+ TimeMeta tm = toTimeMeta();
+ return tm.years;
}
public int getMonthOfYear() {
- return dateTime.getMonthOfYear();
+ TimeMeta tm = toTimeMeta();
+ return tm.monthOfYear;
+ }
+
+ public int getDayOfYear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getDayOfYear();
}
public int getDayOfWeek() {
- return dateTime.getDayOfWeek();
+ TimeMeta tm = toTimeMeta();
+ return tm.getDayOfYear();
}
- public int getDayOfYear() {
- return dateTime.getDayOfYear();
+ public int getWeekOfYear() {
+ TimeMeta tm = toTimeMeta();
+ return tm.getWeekOfYear();
}
public int getDayOfMonth() {
- return dateTime.getDayOfMonth();
+ TimeMeta tm = toTimeMeta();
+ return tm.dayOfMonth;
}
public int getHourOfDay() {
- return dateTime.getHourOfDay();
+ TimeMeta tm = toTimeMeta();
+ return tm.hours;
}
public int getMinuteOfHour() {
- return dateTime.getMinuteOfHour();
- }
-
- public int getSecondOfDay() {
- return dateTime.getSecondOfDay();
+ TimeMeta tm = toTimeMeta();
+ return tm.minutes;
}
public int getSecondOfMinute() {
- return dateTime.getSecondOfMinute();
+ TimeMeta tm = toTimeMeta();
+ return tm.secs;
}
public int getMillisOfSecond() {
- return dateTime.getMillisOfSecond();
+ TimeMeta tm = toTimeMeta();
+ return tm.fsecs / 1000;
}
- public int getWeekyear() {
- return dateTime.getWeekyear();
+ public int getUnixTime() {
+ return (int)(DateTimeUtil.julianTimeToJavaTime(timestamp) / 1000);
}
- public int getWeekOfWeekyear() {
- return dateTime.getWeekOfWeekyear();
+ public String toString() {
+ return asChars();
}
- @Override
- public Datum plus(Datum datum) {
- if (datum.type() == TajoDataTypes.Type.INTERVAL) {
- IntervalDatum interval = (IntervalDatum)datum;
-
- DateTime plusDateTime = null;
- if (interval.getMonths() > 0) {
- plusDateTime = dateTime.plusMonths(interval.getMonths());
- } else {
- plusDateTime = dateTime;
- }
- if (interval.getMilliSeconds() > 0) {
- plusDateTime = plusDateTime.plusMillis((int) interval.getMilliSeconds());
- }
- return new TimestampDatum(plusDateTime);
-
- } else {
- throw new InvalidOperationException(datum.type());
+ public String asChars(TimeZone timeZone, boolean includeTimeZone) {
+ TimeMeta tm = toTimeMeta();
+ DateTimeUtil.toUserTimezone(tm, timeZone);
+ if (includeTimeZone) {
+ tm.timeZone = timeZone.getRawOffset() / 1000;
}
+ return DateTimeUtil.encodeDateTime(tm, DateStyle.ISO_DATES);
}
- @Override
- public Datum minus(Datum datum) {
- switch(datum.type()) {
- case INTERVAL:
- IntervalDatum interval = (IntervalDatum)datum;
-
- DateTime minusDateTime = null;
- if (interval.getMonths() > 0) {
- minusDateTime = dateTime.minusMonths(interval.getMonths());
- } else {
- minusDateTime = dateTime;
- }
- if (interval.getMilliSeconds() > 0) {
- minusDateTime = minusDateTime.minusMillis((int)interval.getMilliSeconds());
- }
- return new TimestampDatum(minusDateTime);
- case TIMESTAMP:
- return new IntervalDatum(dateTime.getMillis() - ((TimestampDatum)datum).dateTime.getMillis());
- default:
- throw new InvalidOperationException(datum.type());
- }
+ public String toString(TimeZone timeZone, boolean includeTimeZone) {
+ return asChars(timeZone, includeTimeZone);
}
@Override
public long asInt8() {
- return dateTime.getMillis();
- }
-
- public String toString() {
- return asChars();
+ return timestamp;
}
@Override
public String asChars() {
- if (getMillisOfSecond() > 0) {
- return StringUtils.stripEnd(dateTime.toString(FRACTION_FORMATTER), "0");
- } else {
- return dateTime.toString(DEFAULT_FORMATTER);
- }
+ TimeMeta tm = toTimeMeta();
+ return DateTimeUtil.encodeDateTime(tm, DateStyle.ISO_DATES);
}
- public String toChars(DateTimeFormatter format) {
- return dateTime.toString(format);
+ public String toChars(String format) {
+ TimeMeta tm = toTimeMeta();
+
+ return DateTimeFormat.to_char(tm, format);
}
@Override
@@ -209,43 +160,95 @@ public class TimestampDatum extends Datum {
@Override
public byte [] asByteArray() {
- return Bytes.toBytes(dateTime.getMillis());
+ return Bytes.toBytes(timestamp);
}
@Override
public Datum equalsTo(Datum datum) {
if (datum.type() == TajoDataTypes.Type.TIME) {
- return DatumFactory.createBool(dateTime.equals(((TimestampDatum) datum).dateTime));
+ return timestamp == datum.asInt8() ? BooleanDatum.TRUE : BooleanDatum.FALSE;
} else if (datum.isNull()) {
return datum;
} else {
- throw new InvalidOperationException();
+ throw new RuntimeException();
}
}
@Override
public int compareTo(Datum datum) {
if (datum.type() == TajoDataTypes.Type.TIMESTAMP) {
- return dateTime.compareTo(((TimestampDatum)datum).dateTime);
- } else if (datum instanceof NullDatum || datum.isNull()) {
+ TimestampDatum another = (TimestampDatum) datum;
+ return (timestamp < another.timestamp) ? -1 : ((timestamp > another.timestamp) ? 1 : 0);
+ } else if (datum.isNull()) {
return -1;
} else {
- throw new InvalidOperationException();
+ throw new RuntimeException();
}
}
- @Override
public boolean equals(Object obj) {
if (obj instanceof TimestampDatum) {
TimestampDatum another = (TimestampDatum) obj;
- return dateTime.isEqual(another.dateTime);
+ return timestamp == another.timestamp;
} else {
return false;
}
}
@Override
+ public Datum plus(Datum datum) {
+ if (datum.type() == TajoDataTypes.Type.INTERVAL) {
+ IntervalDatum interval = (IntervalDatum)datum;
+
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.toJulianTimeMeta(timestamp, tm);
+
+ if (interval.getMonths() > 0) {
+ tm.plusMonths(interval.getMonths());
+ }
+ if (interval.getMilliSeconds() > 0) {
+ tm.plusMillis(interval.getMilliSeconds());
+ }
+
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+
+ } else {
+ throw new InvalidOperationException(datum.type());
+ }
+ }
+
+ @Override
+ public Datum minus(Datum datum) {
+ switch(datum.type()) {
+ case INTERVAL:
+ IntervalDatum interval = (IntervalDatum)datum;
+
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.toJulianTimeMeta(timestamp, tm);
+
+ if (interval.getMonths() > 0) {
+ tm.plusMonths(0 - interval.getMonths());
+ }
+ if (interval.getMilliSeconds() > 0) {
+ tm.plusMillis(0 - interval.getMilliSeconds());
+ }
+ return new TimestampDatum(DateTimeUtil.toJulianTimestamp(tm));
+ case TIMESTAMP:
+ return new IntervalDatum((timestamp - ((TimestampDatum)datum).timestamp) / 1000);
+ default:
+ throw new InvalidOperationException(datum.type());
+ }
+ }
+
+ @Override
public int hashCode(){
- return Objects.hashCode(dateTime);
+ return Objects.hashCode(timestamp);
+ }
+
+ public TimeMeta toTimeMeta() {
+ TimeMeta tm = new TimeMeta();
+ DateTimeUtil.toJulianTimeMeta(timestamp, tm);
+
+ return tm;
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/exception/ValueOutOfRangeException.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/exception/ValueOutOfRangeException.java b/tajo-common/src/main/java/org/apache/tajo/exception/ValueOutOfRangeException.java
new file mode 100644
index 0000000..4cf6b56
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/exception/ValueOutOfRangeException.java
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.exception;
+
+public class ValueOutOfRangeException extends RuntimeException {
+ public ValueOutOfRangeException(String message) {
+ super(message);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/json/DatumAdapter.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/json/DatumAdapter.java b/tajo-common/src/main/java/org/apache/tajo/json/DatumAdapter.java
index 2a162a9..9e88acb 100644
--- a/tajo-common/src/main/java/org/apache/tajo/json/DatumAdapter.java
+++ b/tajo-common/src/main/java/org/apache/tajo/json/DatumAdapter.java
@@ -21,7 +21,6 @@ package org.apache.tajo.json;
import com.google.gson.*;
import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.datum.*;
-import org.joda.time.DateTime;
import java.lang.reflect.Type;
@@ -39,7 +38,7 @@ public class DatumAdapter implements GsonSerDerAdapter<Datum> {
case TIME:
return new TimeDatum(jsonObject.get("value").getAsLong());
case TIMESTAMP:
- return new TimestampDatum(new DateTime(jsonObject.get("value").getAsLong()));
+ return new TimestampDatum(jsonObject.get("value").getAsLong());
case INTERVAL:
String[] values = jsonObject.get("value").getAsString().split(",");
@@ -62,7 +61,7 @@ public class DatumAdapter implements GsonSerDerAdapter<Datum> {
jsonObj.addProperty("value", src.asInt8());
break;
case TIMESTAMP:
- jsonObj.addProperty("value", ((TimestampDatum)src).getMillis());
+ jsonObj.addProperty("value", src.asInt8());
break;
case INTERVAL:
IntervalDatum interval = (IntervalDatum)src;
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java b/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java
deleted file mode 100644
index aa1be8b..0000000
--- a/tajo-common/src/main/java/org/apache/tajo/util/TimeStampUtil.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tajo.util;
-
-
-import org.apache.tajo.datum.Int8Datum;
-import org.joda.time.DateTime;
-import org.joda.time.DateTimeFieldType;
-import org.joda.time.DateTimeZone;
-
-public class TimeStampUtil {
-
- public static long getDay(DateTime dateTime) {
- return convertToMicroSeconds(dateTime.withTimeAtStartOfDay());
- }
-
- public static long getHour(DateTime dateTime) {
- return convertToMicroSeconds(dateTime.withTime(dateTime.get(DateTimeFieldType.hourOfDay()), 0, 0, 0));
- }
-
- public static long getMinute(DateTime dateTime) {
- return convertToMicroSeconds(dateTime.withTime(dateTime.get(DateTimeFieldType.hourOfDay()),
- dateTime.get(DateTimeFieldType.minuteOfHour()), 0, 0));
- }
-
- public static long getSecond(DateTime dateTime) {
- return convertToMicroSeconds(dateTime.withTime(dateTime.get(DateTimeFieldType.hourOfDay()),
- dateTime.get(DateTimeFieldType.minuteOfHour()), dateTime.get(DateTimeFieldType.secondOfMinute()), 0));
- }
-
- public static long getMonth(DateTime dateTime) {
- return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDate(dateTime.getYear(),
- dateTime.getMonthOfYear(),1));
- }
-
- public static long getDayOfWeek(DateTime dateTime,int week) {
- return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDayOfWeek(week));
- }
-
- public static long getYear (DateTime dateTime) {
- return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDate(dateTime.getYear(), 1, 1));
- }
-
- public static DateTime getUTCDateTime(Int8Datum int8Datum){
- return new DateTime(int8Datum.asInt8()/1000, DateTimeZone.UTC);
- }
-
- public static long convertToMicroSeconds(DateTime dateTime) {
- return dateTime.getMillis() * 1000;
- }
-}
[5/6] TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
Posted by ji...@apache.org.
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeConstants.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeConstants.java b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeConstants.java
new file mode 100644
index 0000000..353a500
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeConstants.java
@@ -0,0 +1,631 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.util.datetime;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class DateTimeConstants {
+ public enum DateStyle {
+ XSO_DATES,
+ ISO_DATES,
+ SQL_DATES
+ };
+
+ public static final int[][] DAY_OF_MONTH = {
+ {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
+ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}
+ };
+
+ /** assumes leap year every four years */
+ public static final double DAYS_PER_YEAR = 365.25;
+ public static final int MONTHS_PER_YEAR = 12;
+
+
+ // DAYS_PER_MONTH is very imprecise. The more accurate value is
+ // 365.2425/12 = 30.436875, or '30 days_full 10:29:06'. Right now we only
+ // return an integral number of days_full, but someday perhaps we should
+ // also return a 'time' value to be used as well. ISO 8601 suggests
+ // 0 days_full.
+
+ /** assumes exactly 30 days_full per month */
+ public static final int DAYS_PER_MONTH = 30;
+ /** assume no daylight savings time changes */
+ public static final int HOURS_PER_DAY = 24;
+
+ // This doesn't adjust for uneven daylight savings time intervals or leap
+ // seconds, and it crudely estimates leap years. A more accurate value
+ // for days_full per years is 365.2422.
+
+ /** avoid floating-point computation */
+ public static final int SECS_PER_YEAR = 36525 * 864;
+ public static final int SECS_PER_DAY = 86400;
+ public static final int SECS_PER_HOUR = 3600;
+ public static final int SECS_PER_MINUTE = 60;
+ public static final int MINS_PER_HOUR = 60;
+
+ public static final long MSECS_PER_DAY = 86400000L;
+ public static final long MSECS_PER_SEC = 1000L;
+
+ public static final long USECS_PER_DAY = 86400000000L;
+ public static final long USECS_PER_HOUR = 3600000000L;
+ public static final long USECS_PER_MINUTE = 60000000L;
+ public static final long USECS_PER_SEC = 1000000L;
+
+ public static final int JULIAN_MINYEAR = -4713;
+ public static final int JULIAN_MINMONTH = 11;
+ public static final int JULIAN_MINDAY = 24;
+ public static final int JULIAN_MAXYEAR = 5874898;
+
+ /** == DateTimeUtil.toJulianDate(JULIAN_MAXYEAR, 1, 1) */
+ public static final int JULIAN_MAX = 2147483494;
+
+ // Julian-date equivalents of Day 0 in Unix and Postgres reckoning
+ /** == DateTimeUtil.toJulianDate(1970, 1, 1) */
+ public static final int UNIX_EPOCH_JDATE = 2440588;
+ /** == DateTimeUtil.toJulianDate(2000, 1, 1) */
+ public static final int POSTGRES_EPOCH_JDATE = 2451545;
+ /** == (POSTGRES_EPOCH_JDATE * SECS_PER_DAY) - (UNIX_EPOCH_JDATE * SECS_PER_DAY); */
+ public static final long SECS_DIFFERENCE_BETWEEN_JULIAN_AND_UNIXTIME = 946684800;
+
+ public static final int MAX_TZDISP_HOUR = 15; /* maximum allowed hour part */
+ public static final int TZDISP_LIMIT = ((MAX_TZDISP_HOUR + 1) * SECS_PER_HOUR);
+
+ public static final int INTERVAL_FULL_RANGE = 0x7FFF;
+
+ public static final String DAGO = "ago";
+ public static final String DCURRENT = "current";
+ public static final String EPOCH = "epoch";
+ public static final String INVALID = "invalid";
+ public static final String EARLY = "-infinity";
+ public static final String LATE = "infinity";
+ public static final String NOW = "now";
+ public static final String TODAY = "today";
+ public static final String TOMORROW = "tomorrow";
+ public static final String YESTERDAY = "yesterday";
+ public static final String ZULU = "zulu";
+
+ public static final String DMICROSEC = "usecond";
+ public static final String DMILLISEC = "msecond";
+ public static final String DSECOND = "second";
+ public static final String DMINUTE = "minute";
+ public static final String DHOUR = "hour";
+ public static final String DDAY = "day";
+ public static final String DWEEK = "week";
+ public static final String DMONTH = "month";
+ public static final String DQUARTER = "quarter";
+ public static final String DYEAR = "year";
+ public static final String DDECADE = "decade";
+ public static final String DCENTURY = "century";
+ public static final String DMILLENNIUM = "millennium";
+ public static final String DA_D = "ad";
+ public static final String DB_C = "bc";
+ public static final String DTIMEZONE = "timezone";
+
+ public static final int DATEORDER_YMD = 0;
+ public static final int DATEORDER_DMY = 1;
+ public static final int DATEORDER_MDY = 2;
+
+ public static enum TokenField {
+ DECIMAL(0),
+
+ AM(0), PM(1), HR24(2),
+
+ AD(0), BC(1),
+
+ DTK_NUMBER(0),
+ DTK_STRING(1),
+
+ DTK_DATE(2),
+ DTK_TIME(3),
+ DTK_TZ(4),
+ DTK_AGO(5),
+
+ DTK_SPECIAL(6),
+ DTK_INVALID(7),
+ DTK_CURRENT(8),
+ DTK_EARLY(9),
+ DTK_LATE(10),
+ DTK_EPOCH(11),
+ DTK_NOW(12),
+ DTK_YESTERDAY(13),
+ DTK_TODAY(14),
+ DTK_TOMORROW(15),
+ DTK_ZULU(16),
+
+ DTK_DELTA(17),
+ DTK_SECOND(18),
+ DTK_MINUTE(19),
+ DTK_HOUR(20),
+ DTK_DAY(21),
+ DTK_WEEK(22),
+ DTK_MONTH(23),
+ DTK_QUARTER(24),
+ DTK_YEAR(25),
+ DTK_DECADE(26),
+ DTK_CENTURY(27),
+ DTK_MILLENNIUM(28),
+ DTK_MILLISEC(29),
+ DTK_MICROSEC(30),
+ DTK_JULIAN(31),
+
+ DTK_DOW(32),
+ DTK_DOY(33),
+ DTK_TZ_HOUR(34),
+ DTK_TZ_MINUTE(35),
+ DTK_ISOYEAR(36),
+ DTK_ISODOW(37),
+
+ RESERV(0),
+ MONTH(1),
+ YEAR(2),
+ DAY(3),
+ JULIAN(4),
+ TZ(5),
+ DTZ(6),
+ DTZMOD(7),
+ IGNORE_DTF(8),
+ AMPM(9),
+ HOUR(10),
+ MINUTE(11),
+ SECOND(12),
+ MILLISECOND(13),
+ MICROSECOND(14),
+ DOY(15),
+ DOW(16),
+ UNITS(17),
+ ADBC(18),
+ /* these are only for relative dates */
+ AGO(19),
+ ABS_BEFORE(20),
+ ABS_AFTER(21),
+ /* generic fields to help with parsing */
+ ISODATE(22),
+ ISOTIME(23),
+ /* these are only for parsing intervals */
+ WEEK(24),
+ DECADE(25),
+ CENTURY(26),
+ MILLENNIUM(27),
+ /* reserved for unrecognized string values */
+ UNKNOWN_FIELD(28);
+
+ int value;
+ TokenField(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return value;
+ }
+ }
+
+ static Object datetktbl[][] = {
+ /* text, token, lexval */
+ {"-infinity", TokenField.RESERV, TokenField.DTK_EARLY}, /* "-infinity" reserved for "early time" */
+ {"acsst", TokenField.DTZ, 42}, /* Cent. Australia */
+ {"acst", TokenField.DTZ, -16}, /* Atlantic/Porto Acre */
+ {"act", TokenField.TZ, -20}, /* Atlantic/Porto Acre */
+ {DA_D, TokenField.ADBC, TokenField.AD}, /* "ad" for years >= 0 */
+ {"adt", TokenField.DTZ, -12}, /* Atlantic Daylight Time */
+ {"aesst", TokenField.DTZ, 44}, /* E. Australia */
+ {"aest", TokenField.TZ, 40}, /* Australia Eastern Std Time */
+ {"aft", TokenField.TZ, 18}, /* Kabul */
+ {"ahst", TokenField.TZ, -40}, /* Alaska-Hawaii Std Time */
+ {"akdt", TokenField.DTZ, -32}, /* Alaska Daylight Time */
+ {"akst", TokenField.DTZ, -36}, /* Alaska Standard Time */
+ {"allballs", TokenField.RESERV, TokenField.DTK_ZULU}, /* 00:00:00 */
+ {"almst", TokenField.TZ, 28}, /* Almaty Savings Time */
+ {"almt", TokenField.TZ, 24}, /* Almaty Time */
+ {"am", TokenField.AMPM, TokenField.AM},
+ {"amst", TokenField.DTZ, 20}, /* Armenia Summer Time (Yerevan) */
+ {"amt", TokenField.TZ, 16}, /* Armenia Time (Yerevan) */
+ {"anast", TokenField.DTZ, 52}, /* Anadyr Summer Time (Russia) */
+ {"anat", TokenField.TZ, 48}, /* Anadyr Time (Russia) */
+ {"apr", TokenField.MONTH, 4},
+ {"april", TokenField.MONTH, 4},
+ {"art", TokenField.TZ, -12}, /* Argentina Time */
+ {"ast", TokenField.TZ, -16}, /* Atlantic Std Time (Canada) */
+ {"at", TokenField.IGNORE_DTF, 0}, /* "at" (throwaway) */
+ {"aug", TokenField.MONTH, 8},
+ {"august", TokenField.MONTH, 8},
+ {"awsst", TokenField.DTZ, 36}, /* W. Australia */
+ {"awst", TokenField.TZ, 32}, /* W. Australia */
+ {"awt", TokenField.DTZ, -12},
+ {"azost", TokenField.DTZ, 0}, /* Azores Summer Time */
+ {"azot", TokenField.TZ, -4}, /* Azores Time */
+ {"azst", TokenField.DTZ, 20}, /* Azerbaijan Summer Time */
+ {"azt", TokenField.TZ, 16}, /* Azerbaijan Time */
+ {DB_C, TokenField.ADBC, TokenField.BC}, /* "bc" for years < 0 */
+ {"bdst", TokenField.TZ, 8}, /* British Double Summer Time */
+ {"bdt", TokenField.TZ, 24}, /* Dacca */
+ {"bnt", TokenField.TZ, 32}, /* Brunei Darussalam Time */
+ {"bort", TokenField.TZ, 32}, /* Borneo Time (Indonesia) */
+ {"bot", TokenField.TZ, -16}, /* Bolivia Time */
+ {"bra", TokenField.TZ, -12}, /* Brazil Time */
+ {"bst", TokenField.DTZ, 4}, /* British Summer Time */
+ {"bt", TokenField.TZ, 12}, /* Baghdad Time */
+ {"btt", TokenField.TZ, 24}, /* Bhutan Time */
+ {"cadt", TokenField.DTZ, 42}, /* Central Australian DST */
+ {"cast", TokenField.TZ, 38}, /* Central Australian ST */
+ {"cat", TokenField.TZ, -40}, /* Central Alaska Time */
+ {"cct", TokenField.TZ, 32}, /* China Coast Time */
+ {"cdt", TokenField.DTZ, -20}, /* Central Daylight Time */
+ {"cest", TokenField.DTZ, 8}, /* Central European Dayl.Time */
+ {"cet", TokenField.TZ, 4}, /* Central European Time */
+ {"cetdst", TokenField.DTZ, 8}, /* Central European Dayl.Time */
+ {"chadt", TokenField.DTZ, 55}, /* Chatham Island Daylight Time (13:45) */
+ {"chast", TokenField.TZ, 51}, /* Chatham Island Time (12:45) */
+ {"ckt", TokenField.TZ, 48}, /* Cook Islands Time */
+ {"clst", TokenField.DTZ, -12}, /* Chile Summer Time */
+ {"clt", TokenField.TZ, -16}, /* Chile Time */
+ {"cot", TokenField.TZ, -20}, /* Columbia Time */
+ {"cst", TokenField.TZ, -24}, /* Central Standard Time */
+ {DCURRENT, TokenField.RESERV, TokenField.DTK_CURRENT}, /* "current" is always now */
+ {"cvt", TokenField.TZ, 28}, /* Christmas Island Time (Indian Ocean) */
+ {"cxt", TokenField.TZ, 28}, /* Christmas Island Time (Indian Ocean) */
+ {"d", TokenField.UNITS, TokenField.DTK_DAY}, /* "day of month" for ISO input */
+ {"davt", TokenField.TZ, 28}, /* Davis Time (Antarctica) */
+ {"ddut", TokenField.TZ, 40}, /* Dumont-d'Urville Time (Antarctica) */
+ {"dec", TokenField.MONTH, 12},
+ {"december", TokenField.MONTH, 12},
+ {"dnt", TokenField.TZ, 4}, /* Dansk Normal Tid */
+ {"dow", TokenField.RESERV, TokenField.DTK_DOW}, /* day of week */
+ {"doy", TokenField.RESERV, TokenField.DTK_DOY}, /* day of year */
+ {"dst", TokenField.DTZMOD, 6},
+ {"easst", TokenField.DTZ, -20}, /* Easter Island Summer Time */
+ {"east", TokenField.TZ, -24}, /* Easter Island Time */
+ {"eat", TokenField.TZ, 12}, /* East Africa Time */
+ {"edt", TokenField.DTZ, -16}, /* Eastern Daylight Time */
+ {"eest", TokenField.DTZ, 12}, /* Eastern Europe Summer Time */
+ {"eet", TokenField.TZ, 8}, /* East. Europe, USSR Zone 1 */
+ {"eetdst", TokenField.DTZ, 12}, /* Eastern Europe Daylight Time */
+ {"egst", TokenField.DTZ, 0}, /* East Greenland Summer Time */
+ {"egt", TokenField.TZ, -4}, /* East Greenland Time */
+ {EPOCH, TokenField.RESERV, TokenField.DTK_EPOCH}, /* "epoch" reserved for system epoch time */
+ {"est", TokenField.TZ, -20}, /* Eastern Standard Time */
+ {"feb", TokenField.MONTH, 2},
+ {"february", TokenField.MONTH, 2},
+ {"fjst", TokenField.DTZ, -52}, /* Fiji Summer Time (13 hour offset!) */
+ {"fjt", TokenField.TZ, -48}, /* Fiji Time */
+ {"fkst", TokenField.DTZ, -12}, /* Falkland Islands Summer Time */
+ {"fkt", TokenField.TZ, -8}, /* Falkland Islands Time */
+ {"fri", TokenField.DOW, 5},
+ {"friday", TokenField.DOW, 5},
+ {"fst", TokenField.TZ, 4}, /* French Summer Time */
+ {"fwt", TokenField.DTZ, 8}, /* French Winter Time */
+ {"galt", TokenField.TZ, -24}, /* Galapagos Time */
+ {"gamt", TokenField.TZ, -36}, /* Gambier Time */
+ {"gest", TokenField.DTZ, 20}, /* Georgia Summer Time */
+ {"get", TokenField.TZ, 16}, /* Georgia Time */
+ {"gft", TokenField.TZ, -12}, /* French Guiana Time */
+ {"gilt", TokenField.TZ, 48}, /* Gilbert Islands Time */
+ {"gmt", TokenField.TZ, 0}, /* Greenwish Mean Time */
+ {"gst", TokenField.TZ, 40}, /* Guam Std Time, USSR Zone 9 */
+ {"gyt", TokenField.TZ, -16}, /* Guyana Time */
+ {"h", TokenField.UNITS, TokenField.DTK_HOUR}, /* "hour" */
+ {"hdt", TokenField.DTZ, -36}, /* Hawaii/Alaska Daylight Time */
+ {"hkt", TokenField.TZ, 32}, /* Hong Kong Time */
+ {"hst", TokenField.TZ, -40}, /* Hawaii Std Time */
+ {"ict", TokenField.TZ, 28}, /* Indochina Time */
+ {"idle", TokenField.TZ, 48}, /* Intl. Date Line, East */
+ {"idlw", TokenField.TZ, -48}, /* Intl. Date Line, West */
+ {LATE, TokenField.RESERV, TokenField.DTK_LATE}, /* "infinity" reserved for "late time" */
+ {INVALID, TokenField.RESERV, TokenField.DTK_INVALID}, /* "invalid" reserved for bad time */
+ {"iot", TokenField.TZ, 20}, /* Indian Chagos Time */
+ {"irkst", TokenField.DTZ, 36}, /* Irkutsk Summer Time */
+ {"irkt", TokenField.TZ, 32}, /* Irkutsk Time */
+ {"irt", TokenField.TZ, 14}, /* Iran Time */
+ {"isodow", TokenField.RESERV, TokenField.DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
+ {"ist", TokenField.TZ, 8}, /* Israel */
+ {"it", TokenField.TZ, 14}, /* Iran Time */
+ {"j", TokenField.UNITS, TokenField.DTK_JULIAN},
+ {"jan", TokenField.MONTH, 1},
+ {"january", TokenField.MONTH, 1},
+ {"javt", TokenField.TZ, 28}, /* Java Time (07:00? see JT) */
+ {"jayt", TokenField.TZ, 36}, /* Jayapura Time (Indonesia) */
+ {"jd", TokenField.UNITS, TokenField.DTK_JULIAN},
+ {"jst", TokenField.TZ, 36}, /* Japan Std Time,USSR Zone 8 */
+ {"jt", TokenField.TZ, 30}, /* Java Time (07:30? see JAVT) */
+ {"jul", TokenField.MONTH, 7},
+ {"julian", TokenField.UNITS, TokenField.DTK_JULIAN},
+ {"july", TokenField.MONTH, 7},
+ {"jun", TokenField.MONTH, 6},
+ {"june", TokenField.MONTH, 6},
+ {"kdt", TokenField.DTZ, 40}, /* Korea Daylight Time */
+ {"kgst", TokenField.DTZ, 24}, /* Kyrgyzstan Summer Time */
+ {"kgt", TokenField.TZ, 20}, /* Kyrgyzstan Time */
+ {"kost", TokenField.TZ, 48}, /* Kosrae Time */
+ {"krast", TokenField.DTZ, 28}, /* Krasnoyarsk Summer Time */
+ {"krat", TokenField.TZ, 32}, /* Krasnoyarsk Standard Time */
+ {"kst", TokenField.TZ, 36}, /* Korea Standard Time */
+ {"lhdt", TokenField.DTZ, 44}, /* Lord Howe Daylight Time, Australia */
+ {"lhst", TokenField.TZ, 42}, /* Lord Howe Standard Time, Australia */
+ {"ligt", TokenField.TZ, 40}, /* From Melbourne, Australia */
+ {"lint", TokenField.TZ, 56}, /* Line Islands Time (Kiribati; +14 hours!) */
+ {"lkt", TokenField.TZ, 24}, /* Lanka Time */
+ {"m", TokenField.UNITS, TokenField.DTK_MONTH}, /* "month" for ISO input */
+ {"magst", TokenField.DTZ, 48}, /* Magadan Summer Time */
+ {"magt", TokenField.TZ, 44}, /* Magadan Time */
+ {"mar", TokenField.MONTH, 3},
+ {"march", TokenField.MONTH, 3},
+ {"mart", TokenField.TZ, -38}, /* Marquesas Time */
+ {"mawt", TokenField.TZ, 24}, /* Mawson, Antarctica */
+ {"may", TokenField.MONTH, 5},
+ {"mdt", TokenField.DTZ, -24}, /* Mountain Daylight Time */
+ {"mest", TokenField.DTZ, 8}, /* Middle Europe Summer Time */
+ {"met", TokenField.TZ, 4}, /* Middle Europe Time */
+ {"metdst", TokenField.DTZ, 8}, /* Middle Europe Daylight Time */
+ {"mewt", TokenField.TZ, 4}, /* Middle Europe Winter Time */
+ {"mez", TokenField.TZ, 4}, /* Middle Europe Zone */
+ {"mht", TokenField.TZ, 48}, /* Kwajalein */
+ {"mm", TokenField.UNITS, TokenField.DTK_MINUTE}, /* "minute" for ISO input */
+ {"mmt", TokenField.TZ, 26}, /* Myannar Time */
+ {"mon", TokenField.DOW, 1},
+ {"monday", TokenField.DOW, 1},
+ {"mpt", TokenField.TZ, 40}, /* North Mariana Islands Time */
+ {"msd", TokenField.DTZ, 16}, /* Moscow Summer Time */
+ {"msk", TokenField.TZ, 12}, /* Moscow Time */
+ {"mst", TokenField.TZ, -28}, /* Mountain Standard Time */
+ {"mt", TokenField.TZ, 34}, /* Moluccas Time */
+ {"mut", TokenField.TZ, 16}, /* Mauritius Island Time */
+ {"mvt", TokenField.TZ, 20}, /* Maldives Island Time */
+ {"myt", TokenField.TZ, 32}, /* Malaysia Time */
+ {"nct", TokenField.TZ, 44}, /* New Caledonia Time */
+ {"ndt", TokenField.DTZ, -10}, /* Nfld. Daylight Time */
+ {"nft", TokenField.TZ, -14}, /* Newfoundland Standard Time */
+ {"nor", TokenField.TZ, 4}, /* Norway Standard Time */
+ {"nov", TokenField.MONTH, 11},
+ {"november", TokenField.MONTH, 11},
+ {"novst", TokenField.DTZ, 28}, /* Novosibirsk Summer Time */
+ {"novt", TokenField.TZ, 24}, /* Novosibirsk Standard Time */
+ {NOW, TokenField.RESERV, TokenField.DTK_NOW}, /* current transaction time */
+ {"npt", TokenField.TZ, 23}, /* Nepal Standard Time (GMT-5:45) */
+ {"nst", TokenField.TZ, -14}, /* Nfld. Standard Time */
+ {"nt", TokenField.TZ, -44}, /* Nome Time */
+ {"nut", TokenField.TZ, -44}, /* Niue Time */
+ {"nzdt", TokenField.DTZ, 52}, /* New Zealand Daylight Time */
+ {"nzst", TokenField.TZ, 48}, /* New Zealand Standard Time */
+ {"nzt", TokenField.TZ, 48}, /* New Zealand Time */
+ {"oct", TokenField.MONTH, 10},
+ {"october", TokenField.MONTH, 10},
+ {"omsst", TokenField.DTZ, 28}, /* Omsk Summer Time */
+ {"omst", TokenField.TZ, 24}, /* Omsk Time */
+ {"on", TokenField.IGNORE_DTF, 0}, /* "on" (throwaway) */
+ {"pdt", TokenField.DTZ, -28}, /* Pacific Daylight Time */
+ {"pet", TokenField.TZ, -20}, /* Peru Time */
+ {"petst", TokenField.DTZ, 52}, /* Petropavlovsk-Kamchatski Summer Time */
+ {"pett", TokenField.TZ, 48}, /* Petropavlovsk-Kamchatski Time */
+ {"pgt", TokenField.TZ, 40}, /* Papua New Guinea Time */
+ {"phot", TokenField.TZ, 52}, /* Phoenix Islands (Kiribati) Time */
+ {"pht", TokenField.TZ, 32}, /* Philippine Time */
+ {"pkt", TokenField.TZ, 20}, /* Pakistan Time */
+ {"pm", TokenField.AMPM, TokenField.PM},
+ {"pmdt", TokenField.DTZ, -8}, /* Pierre & Miquelon Daylight Time */
+ {"pont", TokenField.TZ, 44}, /* Ponape Time (Micronesia) */
+ {"pst", TokenField.TZ, -32}, /* Pacific Standard Time */
+ {"pwt", TokenField.TZ, 36}, /* Palau Time */
+ {"pyst", TokenField.DTZ, -12}, /* Paraguay Summer Time */
+ {"pyt", TokenField.TZ, -16}, /* Paraguay Time */
+ {"ret", TokenField.DTZ, 16}, /* Reunion Island Time */
+ {"s", TokenField.UNITS, TokenField.DTK_SECOND}, /* "seconds" for ISO input */
+ {"sadt", TokenField.DTZ, 42}, /* S. Australian Dayl. Time */
+ {"sast", TokenField.TZ, 38}, /* South Australian Std Time */
+ {"sat", TokenField.DOW, 6},
+ {"saturday", TokenField.DOW, 6},
+ {"sct", TokenField.DTZ, 16}, /* Mahe Island Time */
+ {"sep", TokenField.MONTH, 9},
+ {"sept", TokenField.MONTH, 9},
+ {"september", TokenField.MONTH, 9},
+ {"set", TokenField.TZ, -4}, /* Seychelles Time ?? */
+ {"sst", TokenField.DTZ, 8}, /* Swedish Summer Time */
+ {"sun", TokenField.DOW, 0},
+ {"sunday", TokenField.DOW, 0},
+ {"swt", TokenField.TZ, 4}, /* Swedish Winter Time */
+ {"t", TokenField.ISOTIME, TokenField.DTK_TIME}, /* Filler for ISO time fields */
+ {"tft", TokenField.TZ, 20}, /* Kerguelen Time */
+ {"that", TokenField.TZ, -40}, /* Tahiti Time */
+ {"thu", TokenField.DOW, 4},
+ {"thur", TokenField.DOW, 4},
+ {"thurs", TokenField.DOW, 4},
+ {"thursday", TokenField.DOW, 4},
+ {"tjt", TokenField.TZ, 20}, /* Tajikistan Time */
+ {"tkt", TokenField.TZ, -40}, /* Tokelau Time */
+ {"tmt", TokenField.TZ, 20}, /* Turkmenistan Time */
+ {TODAY, TokenField.RESERV, TokenField.DTK_TODAY}, /* midnight */
+ {TOMORROW, TokenField.RESERV, TokenField.DTK_TOMORROW}, /* tomorrow midnight */
+ {"truk", TokenField.TZ, 40}, /* Truk Time */
+ {"tue", TokenField.DOW, 2},
+ {"tues", TokenField.DOW, 2},
+ {"tuesday", TokenField.DOW, 2},
+ {"tvt", TokenField.TZ, 48}, /* Tuvalu Time */
+ {"ulast", TokenField.DTZ, 36}, /* Ulan Bator Summer Time */
+ {"ulat", TokenField.TZ, 32}, /* Ulan Bator Time */
+ {"undefined", TokenField.RESERV, TokenField.DTK_INVALID}, /* pre-v6.1 invalid time */
+ {"ut", TokenField.TZ, 0},
+ {"utc", TokenField.TZ, 0},
+ {"uyst", TokenField.DTZ, -8}, /* Uruguay Summer Time */
+ {"uyt", TokenField.TZ, -12}, /* Uruguay Time */
+ {"uzst", TokenField.DTZ, 24}, /* Uzbekistan Summer Time */
+ {"uzt", TokenField.TZ, 20}, /* Uzbekistan Time */
+ {"vet", TokenField.TZ, -16}, /* Venezuela Time */
+ {"vlast", TokenField.DTZ, 44}, /* Vladivostok Summer Time */
+ {"vlat", TokenField.TZ, 40}, /* Vladivostok Time */
+ {"vut", TokenField.TZ, 44}, /* Vanuata Time */
+ {"wadt", TokenField.DTZ, 32}, /* West Australian DST */
+ {"wakt", TokenField.TZ, 48}, /* Wake Time */
+ {"wast", TokenField.TZ, 28}, /* West Australian Std Time */
+ {"wat", TokenField.TZ, -4}, /* West Africa Time */
+ {"wdt", TokenField.DTZ, 36}, /* West Australian DST */
+ {"wed", TokenField.DOW, 3},
+ {"wednesday", TokenField.DOW, 3},
+ {"weds", TokenField.DOW, 3},
+ {"west", TokenField.DTZ, 4}, /* Western Europe Summer Time */
+ {"wet", TokenField.TZ, 0}, /* Western Europe */
+ {"wetdst", TokenField.DTZ, 4}, /* Western Europe Daylight Savings Time */
+ {"wft", TokenField.TZ, 48}, /* Wallis and Futuna Time */
+ {"wgst", TokenField.DTZ, -8}, /* West Greenland Summer Time */
+ {"wgt", TokenField.TZ, -12}, /* West Greenland Time */
+ {"wst", TokenField.TZ, 32}, /* West Australian Standard Time */
+ {"y", TokenField.UNITS, TokenField.DTK_YEAR}, /* "year" for ISO input */
+ {"yakst", TokenField.DTZ, 40}, /* Yakutsk Summer Time */
+ {"yakt", TokenField.TZ, 36}, /* Yakutsk Time */
+ {"yapt", TokenField.TZ, 40}, /* Yap Time (Micronesia) */
+ {"ydt", TokenField.DTZ, -32}, /* Yukon Daylight Time */
+ {"yekst", TokenField.DTZ, 24}, /* Yekaterinburg Summer Time */
+ {"yekt", TokenField.TZ, 20}, /* Yekaterinburg Time */
+ {YESTERDAY, TokenField.RESERV, TokenField.DTK_YESTERDAY}, /* yesterday midnight */
+ {"yst", TokenField.TZ, -36}, /* Yukon Standard Time */
+ {"z", TokenField.TZ, 0}, /* time zone tag per ISO-8601 */
+ {"zp4", TokenField.TZ, -16}, /* UTC +4 hours. */
+ {"zp5", TokenField.TZ, -20}, /* UTC +5 hours. */
+ {"zp6", TokenField.TZ, -24}, /* UTC +6 hours. */
+ {ZULU, TokenField.TZ, 0}, /* UTC */
+ };
+
+ static Object[][] deltatktbl = {
+ /* text, token, lexval */
+ {"@", TokenField.IGNORE_DTF, 0}, /* postgres relative prefix */
+ {DAGO, TokenField.AGO, 0}, /* "ago" indicates negative time offset */
+ {"c", TokenField.UNITS, TokenField.DTK_CENTURY}, /* "century" relative */
+ {"cent", TokenField.UNITS, TokenField.DTK_CENTURY}, /* "century" relative */
+ {"centuries", TokenField.UNITS, TokenField.DTK_CENTURY}, /* "centuries" relative */
+ {DCENTURY, TokenField.UNITS, TokenField.DTK_CENTURY}, /* "century" relative */
+ {"d", TokenField.UNITS, TokenField.DTK_DAY}, /* "day" relative */
+ {DDAY, TokenField.UNITS, TokenField.DTK_DAY}, /* "day" relative */
+ {"days_full", TokenField.UNITS, TokenField.DTK_DAY}, /* "days_full" relative */
+ {"dec", TokenField.UNITS, TokenField.DTK_DECADE}, /* "decade" relative */
+ {DDECADE, TokenField.UNITS, TokenField.DTK_DECADE}, /* "decade" relative */
+ {"decades", TokenField.UNITS, TokenField.DTK_DECADE}, /* "decades" relative */
+ {"decs", TokenField.UNITS, TokenField.DTK_DECADE}, /* "decades" relative */
+ {"h", TokenField.UNITS, TokenField.DTK_HOUR}, /* "hour" relative */
+ {DHOUR, TokenField.UNITS, TokenField.DTK_HOUR}, /* "hour" relative */
+ {"hours", TokenField.UNITS, TokenField.DTK_HOUR}, /* "hours" relative */
+ {"hr", TokenField.UNITS, TokenField.DTK_HOUR}, /* "hour" relative */
+ {"hrs", TokenField.UNITS, TokenField.DTK_HOUR}, /* "hours" relative */
+ {INVALID, TokenField.RESERV, TokenField.DTK_INVALID}, /* reserved for invalid time */
+ {"m", TokenField.UNITS, TokenField.DTK_MINUTE}, /* "minute" relative */
+ {"microsecon", TokenField.UNITS, TokenField.DTK_MICROSEC}, /* "microsecond" relative */
+ {"mil", TokenField.UNITS, TokenField.DTK_MILLENNIUM}, /* "millennium" relative */
+ {"millennia", TokenField.UNITS, TokenField.DTK_MILLENNIUM}, /* "millennia" relative */
+ {DMILLENNIUM, TokenField.UNITS, TokenField.DTK_MILLENNIUM}, /* "millennium" relative */
+ {"millisecon", TokenField.UNITS, TokenField.DTK_MILLISEC}, /* relative */
+ {"mils", TokenField.UNITS, TokenField.DTK_MILLENNIUM}, /* "millennia" relative */
+ {"min", TokenField.UNITS, TokenField.DTK_MINUTE}, /* "minute" relative */
+ {"mins", TokenField.UNITS, TokenField.DTK_MINUTE}, /* "minutes" relative */
+ {DMINUTE, TokenField.UNITS, TokenField.DTK_MINUTE}, /* "minute" relative */
+ {"minutes", TokenField.UNITS, TokenField.DTK_MINUTE}, /* "minutes" relative */
+ {"mon", TokenField.UNITS, TokenField.DTK_MONTH}, /* "months_short" relative */
+ {"mons", TokenField.UNITS, TokenField.DTK_MONTH}, /* "months_short" relative */
+ {DMONTH, TokenField.UNITS, TokenField.DTK_MONTH}, /* "month" relative */
+ {"months_short", TokenField.UNITS, TokenField.DTK_MONTH},
+ {"ms", TokenField.UNITS, TokenField.DTK_MILLISEC},
+ {"msec", TokenField.UNITS, TokenField.DTK_MILLISEC},
+ {DMILLISEC, TokenField.UNITS, TokenField.DTK_MILLISEC},
+ {"mseconds", TokenField.UNITS, TokenField.DTK_MILLISEC},
+ {"msecs", TokenField.UNITS, TokenField.DTK_MILLISEC},
+ {"qtr", TokenField.UNITS, TokenField.DTK_QUARTER}, /* "quarter" relative */
+ {DQUARTER, TokenField.UNITS, TokenField.DTK_QUARTER}, /* "quarter" relative */
+ {"s", TokenField.UNITS, TokenField.DTK_SECOND},
+ {"sec", TokenField.UNITS, TokenField.DTK_SECOND},
+ {DSECOND, TokenField.UNITS, TokenField.DTK_SECOND},
+ {"seconds", TokenField.UNITS, TokenField.DTK_SECOND},
+ {"secs", TokenField.UNITS, TokenField.DTK_SECOND},
+ {DTIMEZONE, TokenField.UNITS, TokenField.DTK_TZ}, /* "timezone" time offset */
+ {"timezone_h", TokenField.UNITS, TokenField.DTK_TZ_HOUR}, /* timezone hour units */
+ {"timezone_m", TokenField.UNITS, TokenField.DTK_TZ_MINUTE}, /* timezone minutes units */
+ {"undefined", TokenField.RESERV, TokenField.DTK_INVALID}, /* pre-v6.1 invalid time */
+ {"us", TokenField.UNITS, TokenField.DTK_MICROSEC}, /* "microsecond" relative */
+ {"usec", TokenField.UNITS, TokenField.DTK_MICROSEC}, /* "microsecond" relative */
+ {DMICROSEC, TokenField.UNITS, TokenField.DTK_MICROSEC}, /* "microsecond" relative */
+ {"useconds", TokenField.UNITS, TokenField.DTK_MICROSEC}, /* "microseconds" relative */
+ {"usecs", TokenField.UNITS, TokenField.DTK_MICROSEC}, /* "microseconds" relative */
+ {"w", TokenField.UNITS, TokenField.DTK_WEEK}, /* "week" relative */
+ {DWEEK, TokenField.UNITS, TokenField.DTK_WEEK}, /* "week" relative */
+ {"weeks", TokenField.UNITS, TokenField.DTK_WEEK}, /* "weeks" relative */
+ {"y", TokenField.UNITS, TokenField.DTK_YEAR}, /* "year" relative */
+ {DYEAR, TokenField.UNITS, TokenField.DTK_YEAR}, /* "year" relative */
+ {"years", TokenField.UNITS, TokenField.DTK_YEAR}, /* "years" relative */
+ {"yr", TokenField.UNITS, TokenField.DTK_YEAR}, /* "year" relative */
+ {"yrs", TokenField.UNITS, TokenField.DTK_YEAR}, /* "years" relative */
+ };
+
+ public static class DateToken {
+ String key;
+ TokenField type;
+ int value;
+ TokenField valueType;
+
+ public String getKey() {
+ return key;
+ }
+
+ public TokenField getType() {
+ return type;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public TokenField getValueType() {
+ return valueType;
+ }
+ }
+ public static Map<String, DateToken> dateTokenMap = new HashMap<String, DateToken>();
+
+ static {
+ for (Object[] eachToken: datetktbl) {
+ DateToken dateToken = new DateToken();
+ dateToken.key = eachToken[0].toString();
+ dateToken.type = (TokenField)eachToken[1];
+ if (eachToken[2] instanceof TokenField) {
+ dateToken.valueType = (TokenField)eachToken[2];
+ dateToken.value = dateToken.valueType.getValue();
+ } else {
+ dateToken.valueType = TokenField.DECIMAL;
+ dateToken.value = ((Integer)eachToken[2]).intValue();
+ }
+ dateTokenMap.put(dateToken.key, dateToken);
+ }
+
+ for (Object[] eachToken: deltatktbl) {
+ DateToken dateToken = new DateToken();
+ dateToken.key = eachToken[0].toString();
+ dateToken.type = (TokenField)eachToken[1];
+ if (eachToken[2] instanceof TokenField) {
+ dateToken.valueType = (TokenField)eachToken[2];
+ dateToken.value = dateToken.valueType.getValue();
+ } else {
+ dateToken.valueType = TokenField.DECIMAL;
+ dateToken.value = ((Integer)eachToken[2]).intValue();
+ }
+ dateTokenMap.put(dateToken.key, dateToken);
+ }
+ }
+
+ public static int INTERVAL_MASK(TokenField t) { return (1 << (t.getValue())); }
+ public static int DTK_M(TokenField t) { return (0x01 << (t.getValue())); }
+
+ public static final int DTK_ALL_SECS_M = (DTK_M(TokenField.SECOND) |
+ DTK_M(TokenField.MILLISECOND) |
+ DTK_M(TokenField.MICROSECOND));
+ public static final int DTK_DATE_M = (DTK_M(TokenField.YEAR) | DTK_M(TokenField.MONTH) | DTK_M(TokenField.DAY));
+ public static final int DTK_TIME_M = (DTK_M(TokenField.HOUR) | DTK_M(TokenField.MINUTE) | DTK_M(TokenField.SECOND));
+}
[3/6] TAJO-825: Datetime type refactoring. (Hyoungjun Kim via jihoon)
Posted by ji...@apache.org.
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java
new file mode 100644
index 0000000..187e25d
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/util/datetime/DateTimeUtil.java
@@ -0,0 +1,2102 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.util.datetime;
+
+import org.apache.tajo.conf.TajoConf;
+import org.apache.tajo.datum.Int8Datum;
+import org.apache.tajo.exception.ValueOutOfRangeException;
+import org.apache.tajo.util.datetime.DateTimeConstants.DateStyle;
+import org.apache.tajo.util.datetime.DateTimeConstants.DateToken;
+import org.apache.tajo.util.datetime.DateTimeConstants.TokenField;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.util.TimeZone;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * This Class is originated from j2date in datetime.c of PostgreSQL.
+ */
+public class DateTimeUtil {
+
+ private static int MAX_FRACTION_LENGTH = 6;
+
+ /** maximum possible number of fields in a date * string */
+ private static int MAXDATEFIELDS = 25;
+
+ public static boolean isJulianCalendar(int year, int month, int day) {
+ return year <= 1752 && month <= 9 && day < 14;
+ }
+
+ public static int getCenturyOfEra(int year) {
+ if (year > 0) {
+ return (year - 1) / 100 + 1;
+ } else if (year < 0) {
+ //600BC to 501BC -> -6
+ int pYear = -year;
+ return -((pYear - 1) / 100 + 1);
+ } else {
+ return 0;
+ }
+ }
+
+ public static boolean isLeapYear(int year) {
+ return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
+ }
+
+ public static int getDaysInYearMonth(int year, int month) {
+ if (isLeapYear(year)) {
+ return DateTimeConstants.DAY_OF_MONTH[1][month - 1];
+ } else {
+ return DateTimeConstants.DAY_OF_MONTH[0][month - 1];
+ }
+ }
+
+ /**
+ * Julian date support.
+ *
+ * isValidJulianDate checks the minimum date exactly, but is a bit sloppy
+ * about the maximum, since it's far enough out to not be especially
+ * interesting.
+ * @param years
+ * @param months
+ * @param days
+ * @return
+ */
+ public static boolean isValidJulianDate(int years, int months, int days) {
+ return years > DateTimeConstants.JULIAN_MINYEAR || years == DateTimeConstants.JULIAN_MINYEAR &&
+ months > DateTimeConstants.JULIAN_MINMONTH || months == DateTimeConstants.JULIAN_MINMONTH &&
+ days >= DateTimeConstants.JULIAN_MINDAY && years < DateTimeConstants.JULIAN_MAXYEAR;
+ }
+
+ /**
+ * Calendar time to Julian date conversions.
+ * Julian date is commonly used in astronomical applications,
+ * since it is numerically accurate and computationally simple.
+ * The algorithms here will accurately convert between Julian day
+ * and calendar date for all non-negative Julian days_full
+ * (i.e. from Nov 24, -4713 on).
+ *
+ * These routines will be used by other date/time packages
+ * - thomas 97/02/25
+ *
+ * Rewritten to eliminate overflow problems. This now allows the
+ * routines to work correctly for all Julian day counts from
+ * 0 to 2147483647 (Nov 24, -4713 to Jun 3, 5874898) assuming
+ * a 32-bit integer. Longer types should also work to the limits
+ * of their precision.
+ * @param year
+ * @param month
+ * @param day
+ * @return
+ */
+ public static int date2j(int year, int month, int day) {
+ int julian;
+ int century;
+
+ if (month > 2) {
+ month += 1;
+ year += 4800;
+ } else {
+ month += 13;
+ year += 4799;
+ }
+
+ century = year / 100;
+ julian = (year * 365) - 32167;
+ julian += (((year / 4) - century) + (century / 4));
+ julian += ((7834 * month) / 256) + day;
+
+ return julian;
+ }
+
+ /**
+ * Set TimeMeta's date fields.
+ * @param julianDate
+ * @param tm
+ */
+ public static void j2date(int julianDate, TimeMeta tm) {
+ long julian;
+ long quad;
+ long extra;
+ long y;
+
+ julian = julianDate;
+ julian += 32044;
+ quad = julian / 146097;
+ extra = (julian - quad * 146097) * 4 + 3;
+ julian += 60 + quad * 3 + extra / 146097;
+ quad = julian / 1461;
+ julian -= quad * 1461;
+ y = julian * 4 / 1461;
+ julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
+ + 123;
+ y += quad * 4;
+
+
+ tm.years = (int)(y - 4800);
+ quad = julian * 2141 / 65536;
+ tm.dayOfMonth = (int)(julian - 7834 * quad / 256);
+ tm.monthOfYear = (int) ((quad + 10) % DateTimeConstants.MONTHS_PER_YEAR + 1);
+ }
+
+ /**
+ * This method is originated from j2date in datetime.c of PostgreSQL.
+ *
+ * julianToDay - convert Julian date to day-of-week (0..6 == Sun..Sat)
+ *
+ * Note: various places use the locution julianToDay(date - 1) to produce a
+ * result according to the convention 0..6 = Mon..Sun. This is a bit of
+ * a crock, but will work as long as the computation here is just a modulo.
+ * @param julianDate
+ * @return
+ */
+ public static int j2day(int julianDate) {
+ long day;
+
+ day = julianDate;
+
+ day += 1;
+ day %= 7;
+
+ return (int) day;
+ }
+
+ /**
+ * This method is originated from date2isoweek in timestamp.c of PostgreSQL.
+ * Returns ISO week number of year.
+ * @param year
+ * @param mon
+ * @param mday
+ * @return
+ */
+ public static int date2isoweek(int year, int mon, int mday) {
+ double result;
+ int day0;
+ int day4;
+ int dayn;
+
+ /* current day */
+ dayn = date2j(year, mon, mday);
+
+ /* fourth day of current year */
+ day4 = date2j(year, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ day0 = j2day(day4 - 1);
+
+ /*
+ * We need the first week containing a Thursday, otherwise this day falls
+ * into the previous year for purposes of counting weeks
+ */
+ if (dayn < day4 - day0) {
+ day4 = date2j(year - 1, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ day0 = j2day(day4 - 1);
+ }
+
+ result = (dayn - (day4 - day0)) / 7 + 1;
+
+ /*
+ * Sometimes the last few days_full in a year will fall into the first week of
+ * the next year, so check for this.
+ */
+ if (result >= 52) {
+ day4 = date2j(year + 1, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ day0 = j2day(day4 - 1);
+
+ if (dayn >= day4 - day0) {
+ result = (dayn - (day4 - day0)) / 7 + 1;
+ }
+ }
+
+ return (int) result;
+ }
+
+ /**
+ * date2isoyear()
+ *
+ * Returns ISO 8601 year number.
+ * @param year
+ * @param mon
+ * @param mday
+ * @return
+ */
+ public static int date2isoyear(int year, int mon, int mday) {
+ /* current day */
+ int dayn = date2j(year, mon, mday);
+
+ /* fourth day of current year */
+ int day4 = date2j(year, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ int day0 = j2day(day4 - 1);
+
+ /*
+ * We need the first week containing a Thursday, otherwise this day falls
+ * into the previous year for purposes of counting weeks
+ */
+ if (dayn < day4 - day0) {
+ day4 = date2j(year - 1, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ day0 = j2day(day4 - 1);
+
+ year--;
+ }
+
+ double result = (dayn - (day4 - day0)) / 7 + 1;
+
+ /*
+ * Sometimes the last few days in a year will fall into the first week of
+ * the next year, so check for this.
+ */
+ if (result >= 52) {
+ day4 = date2j(year + 1, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ day0 = j2day(day4 - 1);
+
+ if (dayn >= day4 - day0) {
+ year++;
+ }
+ }
+
+ return year;
+ }
+
+ /**
+ * Converts julian timestamp to epoch.
+ * @param timestamp
+ * @return
+ */
+ public static int julianTimeToEpoch(long timestamp) {
+ long totalSecs = timestamp / DateTimeConstants.USECS_PER_SEC;
+ return (int)(totalSecs + DateTimeConstants.SECS_DIFFERENCE_BETWEEN_JULIAN_AND_UNIXTIME);
+ }
+
+ /**
+ * Converts julian timestamp to java timestamp.
+ * @param timestamp
+ * @return
+ */
+ public static long julianTimeToJavaTime(long timestamp) {
+ double totalSecs = (double)timestamp / (double)DateTimeConstants.MSECS_PER_SEC;
+ return (long)(Math.round(totalSecs + DateTimeConstants.SECS_DIFFERENCE_BETWEEN_JULIAN_AND_UNIXTIME * 1000.0));
+ }
+
+ /**
+ * Converts java timestamp to julian timestamp.
+ * @param javaTimestamp
+ * @return
+ */
+ public static long javaTimeToJulianTime(long javaTimestamp) {
+ double totalSecs = javaTimestamp / 1000.0;
+ return (long)((totalSecs -
+ DateTimeConstants.SECS_DIFFERENCE_BETWEEN_JULIAN_AND_UNIXTIME) * DateTimeConstants.USECS_PER_SEC);
+ }
+
+ /**
+ * Calculate the time value(hour, minute, sec, fsec)
+ * If tm.TomeZone is set, the result value is adjusted.
+ * @param tm
+ * @return
+ */
+ public static long toTime(TimeMeta tm) {
+ if (tm.timeZone != 0 && tm.timeZone != Integer.MAX_VALUE) {
+ int timeZoneSecs = tm.timeZone;
+ tm.timeZone = Integer.MAX_VALUE;
+ tm.plusMillis(0 - timeZoneSecs * 1000);
+ }
+ return toTime(tm.hours, tm.minutes, tm.secs, tm.fsecs);
+ }
+
+ /**
+ * Calculate the time value(hour, minute, sec, fsec)
+ * @param hour
+ * @param min
+ * @param sec
+ * @param fsec
+ * @return
+ */
+ public static long toTime(int hour, int min, int sec, int fsec) {
+ return (((((hour * DateTimeConstants.MINS_PER_HOUR) + min) *
+ DateTimeConstants.SECS_PER_MINUTE) + sec) *
+ DateTimeConstants.USECS_PER_SEC) + fsec;
+ }
+
+ /**
+ * Calculate julian timestamp.
+ * @param years
+ * @param months
+ * @param days
+ * @param hours
+ * @param minutes
+ * @param seconds
+ * @param fsec
+ * @return
+ */
+ public static long toJulianTimestamp(
+ int years, int months, int days, int hours, int minutes, int seconds, int fsec) {
+ /* Julian day routines are not correct for negative Julian days_full */
+ if (!isValidJulianDate(years, months, days)) {
+ throw new ValueOutOfRangeException("Out of Range Julian days_full");
+ }
+
+ long numJulianDays = date2j(years, months, days) - DateTimeConstants.POSTGRES_EPOCH_JDATE;
+
+ return toJulianTimestamp(numJulianDays, hours, minutes, seconds, fsec);
+ }
+
+ /**
+ * Calculate julian timestamp.
+ * @param numJulianDays
+ * @param hours
+ * @param minutes
+ * @param seconds
+ * @param fsec
+ * @return
+ */
+ private static long toJulianTimestamp(long numJulianDays, int hours, int minutes, int seconds, int fsec) {
+ long time = toTime(hours, minutes, seconds, fsec);
+
+ long timestamp = numJulianDays * DateTimeConstants.USECS_PER_DAY + time;
+ /* check for major overflow */
+ if ((timestamp - time) / DateTimeConstants.USECS_PER_DAY != numJulianDays) {
+ throw new RuntimeException("Out of Range of Time");
+ }
+ /* check for just-barely overflow (okay except time-of-day wraps) */
+ /* caution: we want to allow 1999-12-31 24:00:00 */
+ if ((timestamp < 0 && numJulianDays > 0) || (timestamp > 0 && numJulianDays < -1)) {
+ throw new RuntimeException("Out of Range of Date");
+ }
+
+ return timestamp;
+ }
+
+ /**
+ * Calculate julian timestamp.
+ * If tm.TomeZone is set, the result value is adjusted.
+ * @param tm
+ * @return
+ */
+ public static long toJulianTimestamp(TimeMeta tm) {
+ if (tm.timeZone != 0 && tm.timeZone != Integer.MAX_VALUE) {
+ int timeZoneSecs = tm.timeZone;
+ tm.timeZone = Integer.MAX_VALUE;
+ tm.plusMillis(0 - timeZoneSecs * 1000);
+ }
+ if (tm.dayOfYear > 0) {
+ return toJulianTimestamp(date2j(tm.years, 1, 1) + tm.dayOfYear - 1, tm.hours, tm.minutes, tm.secs, tm.fsecs);
+ } else {
+ return toJulianTimestamp(tm.years, tm.monthOfYear, tm.dayOfMonth, tm.hours, tm.minutes, tm.secs, tm.fsecs);
+ }
+ }
+
+ /**
+ * Set TimeMeta's field value using given julian timestamp.
+ * Note that year is _not_ 1900-based, but is an explicit full value.
+ * Also, month is one-based, _not_ zero-based.
+ * Returns:
+ * 0 on success
+ * -1 on out of range
+ *
+ * If attimezone is NULL, the global timezone (including possibly brute forced
+ * timezone) will be used.
+ */
+ public static void toJulianTimeMeta(long julianTimestamp, TimeMeta tm) {
+ long date;
+ long time;
+
+ // TODO - If timezone is set, timestamp value should be adjusted here.
+ time = julianTimestamp;
+
+ // TMODULO
+ date = time / DateTimeConstants.USECS_PER_DAY;
+ if (date != 0) {
+ time -= date * DateTimeConstants.USECS_PER_DAY;
+ }
+ if (time < 0) {
+ time += DateTimeConstants.USECS_PER_DAY;
+ date -= 1;
+ }
+
+ /* add offset to go from J2000 back to standard Julian date */
+ date += DateTimeConstants.POSTGRES_EPOCH_JDATE;
+
+ /* Julian day routine does not work for negative Julian days_full */
+ if (date < 0 || date > Integer.MAX_VALUE) {
+ throw new RuntimeException("Timestamp Out Of Scope");
+ }
+
+ j2date((int) date, tm);
+ date2j(time, tm);
+ }
+
+ /**
+ * This method is originated from dt2time in timestamp.c of PostgreSQL.
+ *
+ * @param julianDate
+ * @return hour, min, sec, fsec
+ */
+ public static void date2j(long julianDate, TimeMeta tm) {
+ long time = julianDate;
+
+ tm.hours = (int) (time / DateTimeConstants.USECS_PER_HOUR);
+ time -= tm.hours * DateTimeConstants.USECS_PER_HOUR;
+ tm.minutes = (int) (time / DateTimeConstants.USECS_PER_MINUTE);
+ time -= tm.minutes * DateTimeConstants.USECS_PER_MINUTE;
+ tm.secs = (int) (time / DateTimeConstants.USECS_PER_SEC);
+ tm.fsecs = (int) (time - (tm.secs * DateTimeConstants.USECS_PER_SEC));
+ }
+
+ /**
+ * Decode date string which includes delimiters.
+ *
+ * This method is originated from DecodeDate() in datetime.c of PostgreSQL.
+ * @param str The date string like '2013-12-25'.
+ * @param fmask
+ * @param tmaskValue
+ * @param is2digits
+ * @param tm
+ */
+ private static void decodeDate(String str, int fmask, AtomicInteger tmaskValue, AtomicBoolean is2digits, TimeMeta tm) {
+
+ int idx = 0;
+ int nf = 0;
+ TokenField type = null;
+ int val = 0;
+
+ AtomicInteger dmask = new AtomicInteger(0);
+ int tmask = tmaskValue.get();
+ boolean haveTextMonth = false;
+
+ int length = str.length();
+ char[] dateStr = str.toCharArray();
+ String[] fields = new String[MAXDATEFIELDS];
+
+ while(idx < length && nf < MAXDATEFIELDS) {
+
+ /* skip field separators */
+ while (idx < length && !Character.isLetterOrDigit(dateStr[idx])) {
+ idx++;
+ }
+
+ if (idx == length) {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ int fieldStartIdx = idx;
+ int fieldLength = idx;
+ if (Character.isDigit(dateStr[idx])) {
+ while (idx < length && Character.isDigit(dateStr[idx])) {
+ idx++;
+ }
+ fieldLength = idx;
+ } else if (Character.isLetterOrDigit(dateStr[idx])) {
+ while (idx < length && Character.isLetterOrDigit(dateStr[idx])) {
+ idx++;
+ }
+ fieldLength = idx;
+ }
+
+ fields[nf] = str.substring(fieldStartIdx, fieldLength);
+ nf++;
+ }
+
+ /* look first for text fields, since that will be unambiguous month */
+ for (int i = 0; i < nf; i++) {
+ if (Character.isLetter(fields[i].charAt(0))) {
+ DateToken dateToken = DateTimeConstants.dateTokenMap.get(fields[i].toLowerCase());
+ type = dateToken.getType();
+
+ if (type == TokenField.IGNORE_DTF) {
+ continue;
+ }
+
+ dmask.set(DateTimeConstants.DTK_M(type));
+ switch (type) {
+ case MONTH:
+ tm.monthOfYear = type.getValue();
+ haveTextMonth = true;
+ break;
+
+ default:
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+ if ((fmask & dmask.get()) != 0) {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ fmask |= dmask.get();
+ tmask |= dmask.get();
+
+ /* mark this field as being completed */
+ fields[i] = null;
+ }
+ }
+
+ /* now pick up remaining numeric fields */
+ for (int i = 0; i < nf; i++) {
+ if (fields[i] == null) {
+ continue;
+ }
+
+ length = fields[i].length();
+ if (length <= 0) {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ decodeNumber(length, fields[i], haveTextMonth, fmask, dmask, tm, new AtomicLong(0), is2digits);
+
+ if ( (fmask & dmask.get()) != 0 ) {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+ fmask |= dmask.get();
+ tmask |= dmask.get();
+ }
+
+ tmaskValue.set(tmask);
+
+ if ((fmask & ~(DateTimeConstants.DTK_M(TokenField.DOY) | DateTimeConstants.DTK_M(TokenField.TZ))) != DateTimeConstants.DTK_DATE_M) {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+ }
+
+ /**
+ * Decode time string which includes delimiters.
+ * Return 0 if okay, a DTERR code if not.
+ *
+ * Only check the lower limit on hours, since this same code can be
+ * used to represent time spans.
+ * @param str
+ * @param fmask
+ * @param range
+ * @param tmask
+ * @param tm
+ * @param fsec
+ */
+ private static void decodeTime(String str, int fmask, int range,
+ AtomicInteger tmask, TimeMeta tm, AtomicLong fsec) {
+ StringBuilder cp = new StringBuilder();
+
+ tmask.set(DateTimeConstants.DTK_TIME_M);
+
+ tm.hours = strtoi(str, 0, cp);
+ if (cp.charAt(0) != ':') {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ tm.minutes = strtoi(cp.toString(), 1, cp);
+
+ if (cp.length() == 0) {
+ tm.secs = 0;
+ fsec.set(0);
+ /* If it's a MINUTE TO SECOND interval, take 2 fields as being mm:ss */
+ if (range == (DateTimeConstants.INTERVAL_MASK(TokenField.MINUTE) | DateTimeConstants.INTERVAL_MASK(TokenField.SECOND))) {
+ tm.secs = tm.minutes;
+ tm.minutes = tm.hours;
+ tm.hours = 0;
+ }
+ } else if (cp.charAt(0) == '.') {
+ /* always assume mm:ss.sss is MINUTE TO SECOND */
+ ParseFractionalSecond(cp, fsec);
+ tm.secs = tm.minutes;
+ tm.minutes = tm.hours;
+ tm.hours = 0;
+ }
+ else if (cp.charAt(0) == ':') {
+ tm.secs = strtoi(cp.toString(), 1, cp);
+ if (cp.length() == 0){
+ fsec.set(0);
+ } else if (cp.charAt(0) == '.') {
+ ParseFractionalSecond(cp, fsec);
+ } else{
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+ } else {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ /* do a sanity check */
+ if (tm.hours < 0 || tm.minutes < 0 || tm.minutes > DateTimeConstants.MINS_PER_HOUR - 1 ||
+ tm.secs < 0 || tm.secs > DateTimeConstants.SECS_PER_MINUTE ||
+ fsec.get() < 0 ||
+ fsec.get() > DateTimeConstants.USECS_PER_SEC) {
+ throw new IllegalArgumentException("BAD Format: FIELD_OVERFLOW: " + str);
+ }
+ }
+
+ /**
+ * Parse datetime string to julian time.
+ * The result is the UTC time basis.
+ * @param str
+ * @return
+ */
+ public static long toJulianTimestamp(String str) {
+ TimeMeta tm = decodeDateTime(str, MAXDATEFIELDS);
+ return toJulianTimestamp(tm);
+ }
+
+ public static TimeMeta decodeDateTime(String str) {
+ return decodeDateTime(str, MAXDATEFIELDS);
+ }
+
+ /**
+ * Break string into tokens based on a date/time context.
+ *
+ * This method is originated form ParseDateTime() in datetime.c of PostgreSQL.
+ *
+ * @param str The input string
+ * @param maxFields
+ */
+ public static TimeMeta decodeDateTime(String str, int maxFields) {
+ int idx = 0;
+ int nf = 0;
+ int length = str.length();
+ char [] timeStr = str.toCharArray();
+ String [] fields = new String[maxFields];
+ TokenField[] fieldTypes = new TokenField[maxFields];
+
+ while (idx < length) {
+
+ /* Ignore spaces between fields */
+ if (Character.isSpaceChar(timeStr[idx])) {
+ idx++;
+ continue;
+ }
+
+ /* Record start of current field */
+ if (nf >= maxFields) {
+ throw new IllegalArgumentException("Too many fields");
+ }
+
+ int startIdx = idx;
+
+ //January 8, 1999
+ /* leading digit? then date or time */
+ if (Character.isDigit(timeStr[idx])) {
+ idx++;
+ while (idx < length && Character.isDigit(timeStr[idx])) {
+ idx++;
+ }
+
+ if (idx < length && timeStr[idx] == ':') {
+ fieldTypes[nf] = TokenField.DTK_TIME;
+
+ while (idx <length && (Character.isDigit(timeStr[idx]) || timeStr[idx] == ':' || timeStr[idx] == '.')) {
+ idx++;
+ }
+ }
+
+ /* date field? allow embedded text month */
+ else if (idx < length && (timeStr[idx] == '-' || timeStr[idx] == '/' || timeStr[idx] == '.')) {
+
+ /* save delimiting character to use later */
+ char delim = timeStr[idx];
+ idx++;
+
+ /* second field is all digits? then no embedded text month */
+ if (Character.isDigit(timeStr[idx])) {
+ fieldTypes[nf] = delim == '.' ? TokenField.DTK_NUMBER : TokenField.DTK_DATE;
+
+ while (idx < length && Character.isDigit(timeStr[idx])) {
+ idx++;
+ }
+
+ /*
+ * insist that the delimiters match to get a three-field
+ * date.
+ */
+ if (idx < length && timeStr[idx] == delim) {
+ fieldTypes[nf] = TokenField.DTK_DATE;
+ idx++;
+ while (idx < length && (Character.isDigit(timeStr[idx]) || timeStr[idx] == delim)) {
+ idx++;
+ }
+ }
+ } else {
+ fieldTypes[nf] = TokenField.DTK_DATE;
+ while (idx < length && Character.isLetterOrDigit(timeStr[idx]) || timeStr[idx] == delim) {
+ idx++;
+ }
+ }
+ } else {
+ /*
+ * otherwise, number only and will determine year, month, day, or
+ * concatenated fields later...
+ */
+ fieldTypes[nf] = TokenField.DTK_NUMBER;
+ }
+ }
+
+ /* Leading decimal point? Then fractional seconds... */
+ else if (timeStr[idx] == '.') {
+ idx++;
+ while (idx < length && Character.isDigit(timeStr[idx])) {
+ idx++;
+ continue;
+ }
+ fieldTypes[nf] = TokenField.DTK_NUMBER;
+ }
+
+ // text? then date string, month, day of week, special, or timezone
+ else if (Character.isLetter(timeStr[idx])) {
+ boolean isDate;
+ idx++;
+ while (idx < length && Character.isLetter(timeStr[idx])) {
+ idx++;
+ }
+
+ // Dates can have embedded '-', '/', or '.' separators. It could
+ // also be a timezone name containing embedded '/', '+', '-', '_',
+ // or ':' (but '_' or ':' can't be the first punctuation). If the
+ // next character is a digit or '+', we need to check whether what
+ // we have so far is a recognized non-timezone keyword --- if so,
+ // don't believe that this is the start of a timezone.
+
+ isDate = false;
+ if (idx < length && (timeStr[idx] == '-' || timeStr[idx] == '/' || timeStr[idx] == '.')) {
+ isDate = true;
+ } else if (idx < length && (timeStr[idx] == '+' || Character.isDigit(timeStr[idx]))) {
+ // The original ParseDateTime handles this case. But, we currently omit this case.
+ throw new IllegalArgumentException("Cannot parse this datetime field " + str.substring(startIdx, idx));
+ }
+
+ if (isDate) {
+ fieldTypes[nf] = TokenField.DTK_DATE;
+
+ do {
+ idx++;
+ } while (idx <length && (timeStr[idx] == '+' || timeStr[idx] == '-' || timeStr[idx] == '/' ||
+ timeStr[idx] == '_' || timeStr[idx] == '.' || timeStr[idx] == ':' ||
+ Character.isLetterOrDigit(timeStr[idx])));
+ } else {
+ fieldTypes[nf] = TokenField.DTK_STRING;
+ }
+ }
+
+ // sign? then special or numeric timezone
+ else if (timeStr[idx] == '+' || timeStr[idx] == '-') {
+ idx++;
+
+ // soak up leading whitespace
+ while (idx < length && Character.isSpaceChar(timeStr[idx])) {
+ idx++;
+ }
+
+ // numeric timezone?
+ // note that "DTK_TZ" could also be a signed float or yyyy-mm */
+ if (idx < length && Character.isDigit(timeStr[idx])) {
+ fieldTypes[nf] = TokenField.DTK_TZ;
+ idx++;
+
+ while (idx < length && (Character.isDigit(timeStr[idx]) || timeStr[idx] == ':' || timeStr[idx] == '.' ||
+ timeStr[idx] == '-')) {
+ idx++;
+ }
+ }
+ /* special? */
+ else if (idx < length && Character.isLetter(timeStr[idx])) {
+ fieldTypes[nf] = TokenField.DTK_SPECIAL;
+ idx++;
+
+ while (idx < length && Character.isLetter(timeStr[idx])) {
+ idx++;
+ }
+ } else {
+ throw new IllegalArgumentException("BAD Format: " + str.substring(startIdx, idx));
+ }
+ }
+ /* ignore other punctuation but use as delimiter */
+ else if (isPunctuation(timeStr[idx])) {
+ idx++;
+ continue;
+ } else { // otherwise, something is not right...
+ throw new IllegalArgumentException("BAD datetime format: " + str.substring(startIdx, idx));
+ }
+
+ fields[nf] = str.substring(startIdx, idx);
+ nf++;
+ }
+ return decodeDateTime(fields, fieldTypes, nf);
+ }
+
+ /**
+ * Fetch a fractional-second value with suitable error checking
+ * @param cp
+ * @param fsec
+ */
+ public static void ParseFractionalSecond(StringBuilder cp, AtomicLong fsec) {
+ /* Caller should always pass the start of the fraction part */
+ double frac = strtod(cp.toString(), 1, cp);
+ fsec.set(Math.round(frac * 1000000));
+ }
+
+ /**
+ * Interpret string as a numeric timezone.
+ *
+ * Return 0 if okay (and set *tzp), a DTERR code if not okay.
+ *
+ * NB: this must *not* ereport on failure; see commands/variable.c.
+ * @param str
+ * @param tz
+ */
+ public static void decodeTimezone(String str, AtomicInteger tz) {
+ int min = 0;
+ int sec = 0;
+ StringBuilder sb = new StringBuilder();
+
+ int strIndex = 0;
+ /* leading character must be "+" or "-" */
+ if (str.charAt(strIndex) != '+' && str.charAt(strIndex) != '-') {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+ int hr = strtoi(str, 1, sb);
+
+ /* explicit delimiter? */
+ if (sb.length() > 0 && sb.charAt(0) == ':') {
+ min = strtoi(sb.toString(), 1, sb);
+ if (sb.charAt(0) == ':') {
+ sec = strtoi(sb.toString(), 1, sb);
+ }
+ }
+ /* otherwise, might have run things together... */
+ else if (sb.length() == 0 && str.length() > 3) {
+ min = hr % 100;
+ hr = hr / 100;
+ /* we could, but don't, support a run-together hhmmss format */
+ } else {
+ min = 0;
+ }
+ /* Range-check the values; see notes in datatype/timestamp.h */
+ if (hr < 0 || hr > DateTimeConstants.MAX_TZDISP_HOUR) {
+ throw new IllegalArgumentException("BAD Format: TZDISP_OVERFLOW: " + str);
+ }
+ if (min < 0 || min >= DateTimeConstants.MINS_PER_HOUR) {
+ throw new IllegalArgumentException("BAD Format: TZDISP_OVERFLOW: " + str);
+ }
+ if (sec < 0 || sec >= DateTimeConstants.SECS_PER_MINUTE) {
+ throw new IllegalArgumentException("BAD Format: TZDISP_OVERFLOW: " + str);
+ }
+
+ int tzValue = (hr * DateTimeConstants.MINS_PER_HOUR + min) * DateTimeConstants.SECS_PER_MINUTE + sec;
+ if (str.charAt(strIndex) == '-') {
+ tzValue = -tzValue;
+ }
+ tz.set(tzValue);
+ }
+
+ /**
+ * Interpret plain numeric field as a date value in context.
+ * @param flen
+ * @param str
+ * @param haveTextMonth
+ * @param fmask
+ * @param tmaskValue
+ * @param tm
+ * @param fsec
+ * @param is2digits
+ */
+ private static void decodeNumber(int flen, String str, boolean haveTextMonth, int fmask,
+ AtomicInteger tmaskValue, TimeMeta tm, AtomicLong fsec, AtomicBoolean is2digits) {
+ int val;
+ StringBuilder cp = new StringBuilder();
+
+ int tmask = 0;
+ tmaskValue.set(tmask);
+
+ val = strtoi(str, 0, cp);
+ if (cp.toString().equals(str)) {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ if (cp.length() > 0 && cp.charAt(0) == '.') {
+ /*
+ * More than two digits before decimal point? Then could be a date or
+ * a run-together time: 2001.360 20011225 040506.789
+ */
+ if (cp.length() - str.length() > 2) {
+ decodeNumberField(flen, str,
+ (fmask | DateTimeConstants.DTK_DATE_M),
+ tmaskValue, tm,
+ fsec, is2digits);
+ return;
+ }
+ ParseFractionalSecond(cp, fsec);
+ }
+
+ // Special case for day of year
+ if (flen == 3 && (fmask & DateTimeConstants.DTK_DATE_M) == DateTimeConstants.DTK_M(TokenField.YEAR) &&
+ val >= 1 && val <= 366) {
+ tmaskValue.set((DateTimeConstants.DTK_M(TokenField.DOY) |
+ DateTimeConstants.DTK_M(TokenField.MONTH) |
+ DateTimeConstants.DTK_M(TokenField.DAY)));
+ tm.dayOfYear = val;
+ // tm_mon and tm_mday can't actually be set yet ...
+ return;
+ }
+
+ /* Switch based on what we have so far */
+ int checkValue = fmask & DateTimeConstants.DTK_DATE_M;
+ if (checkValue == 0) {
+ /*
+ * Nothing so far; make a decision about what we think the input
+ * is. There used to be lots of heuristics here, but the
+ * consensus now is to be paranoid. It *must* be either
+ * YYYY-MM-DD (with a more-than-two-digit year field), or the
+ * field order defined by DateOrder.
+ */
+ if (flen >= 3 || TajoConf.getDateOrder() == DateTimeConstants.DATEORDER_YMD) {
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.YEAR));
+ tm.years = val;
+ } else if (TajoConf.getDateOrder() == DateTimeConstants.DATEORDER_DMY) {
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ tm.dayOfMonth = val;
+ } else {
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.MONTH));
+ tm.monthOfYear = val;
+ }
+ } else if (checkValue == (DateTimeConstants.DTK_M(TokenField.YEAR))) {
+ /* Must be at second field of YY-MM-DD */
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.MONTH));
+ tm.monthOfYear = val;
+ } else if (checkValue == (DateTimeConstants.DTK_M(TokenField.MONTH))) {
+ if (haveTextMonth) {
+ /*
+ * We are at the first numeric field of a date that included a
+ * textual month name. We want to support the variants
+ * MON-DD-YYYY, DD-MON-YYYY, and YYYY-MON-DD as unambiguous
+ * inputs. We will also accept MON-DD-YY or DD-MON-YY in
+ * either DMY or MDY modes, as well as YY-MON-DD in YMD mode.
+ */
+ if (flen >= 3 || TajoConf.getDateOrder() == DateTimeConstants.DATEORDER_YMD) {
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.YEAR));
+ tm.years = val;
+ } else {
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ tm.dayOfMonth = val;
+ }
+ } else {
+ /* Must be at second field of MM-DD-YY */
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ tm.dayOfMonth = val;
+ }
+ } else if (checkValue == (DateTimeConstants.DTK_M(TokenField.YEAR) | DateTimeConstants.DTK_M(TokenField.MONTH))) {
+ if (haveTextMonth) {
+ /* Need to accept DD-MON-YYYY even in YMD mode */
+ if (flen >= 3 && is2digits.get()) {
+ /* Guess that first numeric field is day was wrong */
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.DAY)); /* YEAR is already set */
+ tm.dayOfMonth = tm.years;
+ tm.years = val;
+ is2digits.set(false);
+ } else {
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ tm.dayOfMonth = val;
+ }
+ } else {
+ /* Must be at third field of YY-MM-DD */
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ tm.dayOfMonth = val;
+ }
+ } else if (checkValue == DateTimeConstants.DTK_M(TokenField.DAY)) {
+ /* Must be at second field of DD-MM-YY */
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.MONTH));
+ tm.monthOfYear = val;
+ } else if (checkValue == (DateTimeConstants.DTK_M(TokenField.MONTH) | DateTimeConstants.DTK_M(TokenField.DAY))) {
+ /* Must be at third field of DD-MM-YY or MM-DD-YY */
+ tmaskValue.set(DateTimeConstants.DTK_M(TokenField.YEAR));
+ tm.years = val;
+ } else if (checkValue == (DateTimeConstants.DTK_M(TokenField.YEAR) | DateTimeConstants.DTK_M(TokenField.MONTH) | DateTimeConstants.DTK_M(TokenField.DAY))) {
+ /* we have all the date, so it must be a time field */
+ decodeNumberField(flen, str, fmask,
+ tmaskValue, tm,
+ fsec, is2digits);
+ return;
+
+ } else {
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ /*
+ * When processing a year field, mark it for adjustment if it's only one
+ * or two digits.
+ */
+ if (tmaskValue.get() == DateTimeConstants.DTK_M(TokenField.YEAR)) {
+ is2digits.set(flen <= 2);
+ }
+ }
+
+ /**
+ * Interpret numeric string as a concatenated date or time field.
+ *
+ * Use the context of previously decoded fields to help with
+ * the interpretation.
+ * @param len
+ * @param str
+ * @param fmask
+ * @param tmaskValue
+ * @param tm
+ * @param fsec
+ * @param is2digits
+ * @return
+ */
+ static TokenField decodeNumberField(int len, String str, int fmask,
+ AtomicInteger tmaskValue, TimeMeta tm, AtomicLong fsec, AtomicBoolean is2digits) {
+ /*
+ * Have a decimal point? Then this is a date or something with a seconds
+ * field...
+ */
+ int index = str.indexOf('.');
+
+ if (index >= 0) {
+ String cp = str.substring(index + 1);
+ /*
+ * Can we use ParseFractionalSecond here? Not clear whether trailing
+ * junk should be rejected ...
+ */
+ double frac = strtod(cp, 0, null);
+ fsec.set(Math.round(frac * 1000000));
+ /* Now truncate off the fraction for further processing */
+ len = str.length();
+ }
+ /* No decimal point and no complete date yet? */
+ else if ((fmask & DateTimeConstants.DTK_DATE_M) != DateTimeConstants.DTK_DATE_M) {
+ /* yyyymmdd? */
+ if (len == 8) {
+ tmaskValue.set(DateTimeConstants.DTK_DATE_M);
+
+ tm.dayOfMonth = Integer.parseInt(str.substring(6));
+ tm.monthOfYear = Integer.parseInt(str.substring(4, 6));
+ tm.years = Integer.parseInt(str.substring(0, 4));
+
+ return TokenField.DTK_DATE;
+ }
+ /* yymmdd? */
+ else if (len == 6) {
+ tmaskValue.set(DateTimeConstants.DTK_DATE_M);
+ tm.dayOfMonth = Integer.parseInt(str.substring(4));
+ tm.monthOfYear = Integer.parseInt(str.substring(2, 4));
+ tm.years = Integer.parseInt(str.substring(0, 2));
+ is2digits.set(true);
+
+ return TokenField.DTK_DATE;
+ }
+ }
+
+ /* not all time fields are specified? */
+ if ((fmask & DateTimeConstants.DTK_TIME_M) != DateTimeConstants.DTK_TIME_M) {
+ /* hhmmss */
+ if (len == 6) {
+ tmaskValue.set(DateTimeConstants.DTK_TIME_M);
+ tm.secs = Integer.parseInt(str.substring(4));
+ tm.minutes = Integer.parseInt(str.substring(2, 4));
+ tm.hours = Integer.parseInt(str.substring(0, 2));
+
+ return TokenField.DTK_TIME;
+ }
+ /* hhmm? */
+ else if (len == 4) {
+ tmaskValue.set(DateTimeConstants.DTK_TIME_M);
+ tm.secs = 0;
+ tm.minutes = Integer.parseInt(str.substring(2, 4));
+ tm.hours = Integer.parseInt(str.substring(0, 2));
+
+ return TokenField.DTK_TIME;
+ }
+ }
+
+ throw new IllegalArgumentException("BAD Format: " + str);
+ }
+
+ private static TimeMeta decodeDateTime(String[] fields, TokenField[] fieldTypes, int nf) {
+ int fmask = 0;
+ AtomicInteger tmask = new AtomicInteger(0);
+ int type;
+
+ /* "prefix type" for ISO y2001m02d04 format */
+ TokenField ptype = null;
+
+ boolean haveTextMonth = false;
+ boolean isjulian = false;
+ AtomicBoolean is2digits = new AtomicBoolean(false);
+ boolean bc = false;
+
+ int tzp = Integer.MAX_VALUE;
+ String namedTimeZone = null;
+
+ StringBuilder sb = new StringBuilder();
+ // We'll insist on at least all of the date fields, but initialize the
+ // remaining fields in case they are not set later...
+ TokenField dtype = TokenField.DTK_DATE;
+ TokenField mer = null;
+
+ TimeMeta tm = new TimeMeta();
+ TimeMeta cur_tm = new TimeMeta();
+
+ AtomicLong fsec = new AtomicLong();
+ AtomicInteger tz = new AtomicInteger(Integer.MAX_VALUE);
+
+ // don't know daylight savings time status apriori */
+ tm.isDST = false;
+
+ for (int i = 0; i < nf; i++) {
+ if (fieldTypes[i] == null) {
+ continue;
+ }
+ switch (fieldTypes[i]) {
+ case DTK_DATE:
+ /***
+ * Integral julian day with attached time zone?
+ * All other forms with JD will be separated into
+ * distinct fields, so we handle just this case here.
+ ***/
+ if (ptype == TokenField.DTK_JULIAN) {
+ int val;
+
+ if (tzp == Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ val = strtoi(fields[i], 0, sb);
+
+ date2j(val, tm);
+ isjulian = true;
+
+ /* Get the time zone from the end of the string */
+ decodeTimezone(sb.toString(), tz);
+
+ tmask.set(DateTimeConstants.DTK_DATE_M | DateTimeConstants.DTK_TIME_M | DateTimeConstants.DTK_M(TokenField.TZ));
+ ptype = null;
+ break;
+ }
+ /***
+ * Already have a date? Then this might be a time zone name
+ * with embedded punctuation (e.g. "America/New_York") or a
+ * run-together time with trailing time zone (e.g. hhmmss-zz).
+ * - thomas 2001-12-25
+ *
+ * We consider it a time zone if we already have month & day.
+ * This is to allow the form "mmm dd hhmmss tz year", which
+ * we've historically accepted.
+ ***/
+ else if (ptype != null ||
+ ((fmask & (DateTimeConstants.DTK_M(TokenField.MONTH) | DateTimeConstants.DTK_M(TokenField.DAY))) ==
+ (DateTimeConstants.DTK_M(TokenField.MONTH) | DateTimeConstants.DTK_M(TokenField.DAY))))
+ {
+ /* No time zone accepted? Then quit... */
+ if (tzp == Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ if (Character.isDigit(fields[i].charAt(0)) || ptype != null) {
+ if (ptype != null) {
+ /* Sanity check; should not fail this test */
+ if (ptype != TokenField.DTK_TIME) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ ptype = null;
+ }
+
+ /*
+ * Starts with a digit but we already have a time
+ * field? Then we are in trouble with a date and time
+ * already...
+ */
+ if ((fmask & DateTimeConstants.DTK_TIME_M) == DateTimeConstants.DTK_TIME_M) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ int index = fields[i].indexOf("-");
+ if (index < 0) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ /* Get the time zone from the end of the string */
+ decodeTimezone(fields[i].substring(index + 1), tz);
+
+ /*
+ * Then read the rest of the field as a concatenated
+ * time
+ */
+ decodeNumberField(fields[i].length(), fields[i],
+ fmask,
+ tmask, tm,
+ fsec, is2digits);
+
+ /*
+ * modify tmask after returning from
+ * DecodeNumberField()
+ */
+ tmask.set(tmask.get() | DateTimeConstants.DTK_M(TokenField.TZ));
+ }
+ else {
+ namedTimeZone = pg_tzset(fields[i]);
+ if (namedTimeZone == null) {
+ /*
+ * We should return an error code instead of
+ * ereport'ing directly, but then there is no way
+ * to report the bad time zone name.
+ */
+ throw new IllegalArgumentException("BAD Format: time zone \"%s\" not recognized: " + fields[i]);
+ }
+ /* we'll apply the zone setting below */
+ tmask.set(DateTimeConstants.DTK_M(TokenField.TZ));
+ }
+ } else {
+ decodeDate(fields[i], fmask, tmask, is2digits, tm);
+ }
+ break;
+
+ case DTK_TIME:
+ decodeTime(fields[i], (fmask | DateTimeConstants.DTK_DATE_M),
+ DateTimeConstants.INTERVAL_FULL_RANGE,
+ tmask, tm, fsec);
+ break;
+
+ case DTK_TZ: {
+ decodeTimezone(fields[i], tz);
+ tmask.set(DateTimeConstants.DTK_M(TokenField.TZ));
+ break;
+ }
+
+ case DTK_NUMBER:
+
+ /*
+ * Was this an "ISO date" with embedded field labels? An
+ * example is "y2001m02d04" - thomas 2001-02-04
+ */
+ if (ptype != null) {
+ int val = strtoi(fields[i], 0, sb);
+
+ /*
+ * only a few kinds are allowed to have an embedded
+ * decimal
+ */
+ if (sb.length() == 0) {
+ continue;
+ }
+ if (sb.charAt(0) == '.') {
+ switch (ptype) {
+ case DTK_JULIAN:
+ case DTK_TIME:
+ case DTK_SECOND:
+ break;
+ default:
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ } else {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ switch (ptype) {
+ case DTK_YEAR:
+ tm.years = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.YEAR));
+ break;
+
+ case DTK_MONTH:
+
+ /*
+ * already have a month and hour? then assume
+ * minutes
+ */
+ if ((fmask & DateTimeConstants.DTK_M(TokenField.MONTH)) != 0 &&
+ (fmask & DateTimeConstants.DTK_M(TokenField.HOUR)) != 0) {
+ tm.minutes = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.MINUTE));
+ }
+ else {
+ tm.monthOfYear = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.MONTH));
+ }
+ break;
+
+ case DTK_DAY:
+ tm.dayOfMonth = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ break;
+
+ case DTK_HOUR:
+ tm.hours = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.HOUR));
+ break;
+
+ case DTK_MINUTE:
+ tm.minutes = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.MINUTE));
+ break;
+
+ case DTK_SECOND:
+ tm.secs = val;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.SECOND));
+ if (sb.charAt(0) == '.') {
+ ParseFractionalSecond(sb, fsec);
+ tmask.set(DateTimeConstants.DTK_ALL_SECS_M);
+ }
+ break;
+ case DTK_TZ:
+ tmask.set(DateTimeConstants.DTK_M(TokenField.TZ));
+ decodeTimezone(fields[i], tz);
+ break;
+
+ case DTK_JULIAN:
+ /* previous field was a label for "julian date" */
+ if (val < 0) {
+ throw new IllegalArgumentException("BAD Format: FIELD_OVERFLOW: " + fields[i]);
+ }
+ tmask.set(DateTimeConstants.DTK_DATE_M);
+ date2j(val, tm);
+ isjulian = true;
+
+ /* fractional Julian Day? */
+ if (sb.charAt(0) == '.') {
+ double time = strtod(sb.toString(), 0, sb);
+
+ time *= DateTimeConstants.USECS_PER_DAY;
+ date2j((long)time, tm);
+ tmask.set(tmask.get() | DateTimeConstants.DTK_TIME_M);
+ }
+ break;
+
+ case DTK_TIME:
+ /* previous field was "t" for ISO time */
+ decodeNumberField(fields[i].length(), fields[i],
+ (fmask | DateTimeConstants.DTK_DATE_M),
+ tmask, tm,
+ fsec, is2digits);
+ if (tmask.get() != DateTimeConstants.DTK_TIME_M) {
+ throw new IllegalArgumentException("BAD Format: FIELD_OVERFLOW: " + fields[i]);
+ }
+ break;
+
+ default:
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ ptype = null;
+ dtype = TokenField.DTK_DATE;
+ } else {
+ int flen = fields[i].length();
+ int index = fields[i].indexOf(".");
+ String cp = null;
+ if (index > 0) {
+ cp = fields[i].substring(index + 1);
+ }
+
+ /* Embedded decimal and no date yet? */
+ if (cp != null && ((fmask & DateTimeConstants.DTK_DATE_M) == 0 )) {
+ decodeDate(fields[i], fmask,
+ tmask, is2digits, tm);
+ }
+ /* embedded decimal and several digits before? */
+ else if (cp != null && flen - cp.length() > 2) {
+ /*
+ * Interpret as a concatenated date or time Set the
+ * type field to allow decoding other fields later.
+ * Example: 20011223 or 040506
+ */
+ decodeNumberField(flen, fields[i], fmask,
+ tmask, tm,
+ fsec, is2digits);
+ }
+ else if (flen > 4) {
+ decodeNumberField(flen, fields[i], fmask,
+ tmask, tm,
+ fsec, is2digits);
+ }
+ /* otherwise it is a single date/time field... */
+ else {
+ decodeNumber(flen, fields[i],
+ haveTextMonth, fmask,
+ tmask, tm,
+ fsec, is2digits);
+ }
+ }
+ break;
+ case DTK_STRING:
+ case DTK_SPECIAL:
+ DateToken dateToken = DateTimeConstants.dateTokenMap.get(fields[i].toLowerCase());
+ if (dateToken == null) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ tmask.set(DateTimeConstants.DTK_M(dateToken.getType()));
+ switch (dateToken.getType()) {
+ case RESERV:
+ switch(dateToken.getValueType()) {
+ case DTK_CURRENT:
+ throw new IllegalArgumentException("BAD Format: date/time value \"current\" is no longer supported" + fields[i]);
+
+ case DTK_NOW:
+ tmask.set(DateTimeConstants.DTK_DATE_M | DateTimeConstants.DTK_TIME_M | DateTimeConstants.DTK_M(TokenField.TZ));
+ dtype = TokenField.DTK_DATE;
+ date2j(javaTimeToJulianTime(System.currentTimeMillis()), tm);
+ break;
+
+ case DTK_YESTERDAY:
+ tmask.set(DateTimeConstants.DTK_DATE_M);
+ dtype = TokenField.DTK_DATE;
+ date2j(javaTimeToJulianTime(System.currentTimeMillis()), tm);
+ tm.plusDays(-1);
+ break;
+
+ case DTK_TODAY:
+ tmask.set(DateTimeConstants.DTK_DATE_M);
+ dtype = TokenField.DTK_DATE;
+ date2j(javaTimeToJulianTime(System.currentTimeMillis()), cur_tm);
+ tm.years = cur_tm.years;
+ tm.monthOfYear = cur_tm.monthOfYear;
+ tm.dayOfMonth = cur_tm.dayOfMonth;
+ break;
+
+ case DTK_TOMORROW:
+ tmask.set(DateTimeConstants.DTK_DATE_M);
+ dtype = TokenField.DTK_DATE;
+ date2j(javaTimeToJulianTime(System.currentTimeMillis()), tm);
+ tm.plusDays(1);
+ break;
+
+ case DTK_ZULU:
+ tmask.set(DateTimeConstants.DTK_TIME_M | DateTimeConstants.DTK_M(TokenField.TZ));
+ dtype = TokenField.DTK_DATE;
+ tm.hours = 0;
+ tm.minutes = 0;
+ tm.secs = 0;
+ break;
+
+ default:
+ dtype = dateToken.getValueType();
+ }
+ break;
+
+ case MONTH:
+ /*
+ * already have a (numeric) month? then see if we can
+ * substitute...
+ */
+ if ((fmask & DateTimeConstants.DTK_M(TokenField.MONTH)) != 0 && !haveTextMonth &&
+ (fmask & DateTimeConstants.DTK_M(TokenField.DAY)) == 0 &&
+ tm.monthOfYear >= 1 && tm.monthOfYear <= 31) {
+ tm.dayOfMonth = tm.monthOfYear;
+ tmask.set(DateTimeConstants.DTK_M(TokenField.DAY));
+ }
+ haveTextMonth = true;
+ tm.monthOfYear = dateToken.getValue();
+ break;
+
+ case DTZMOD:
+
+ /*
+ * daylight savings time modifier (solves "MET DST"
+ * syntax)
+ */
+ tmask.set(tmask.get() | DateTimeConstants.DTK_M(TokenField.DTZ));
+ tm.isDST = true;
+ if (tzp == Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ tzp += dateToken.getValue() * DateTimeConstants.MINS_PER_HOUR;
+ break;
+
+ case DTZ:
+
+ /*
+ * set mask for TZ here _or_ check for DTZ later when
+ * getting default timezone
+ */
+ tmask.set(tmask.get() | DateTimeConstants.DTK_M(TokenField.TZ));
+ tm.isDST = true;
+ if (tzp == Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ tzp = dateToken.getValue() * DateTimeConstants.MINS_PER_HOUR;
+ break;
+
+ case TZ:
+ tm.isDST = false;
+ if (tzp == Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ tzp = dateToken.getValue() * DateTimeConstants.MINS_PER_HOUR;
+ break;
+
+ case IGNORE_DTF:
+ break;
+
+ case AMPM:
+ mer = dateToken.getValueType();
+ break;
+
+ case ADBC:
+ bc = (dateToken.getValueType() == TokenField.BC);
+ break;
+
+ case DOW:
+ tm.dayOfWeek = dateToken.getValue();
+ break;
+
+ case UNITS:
+ tmask.set(0);
+ ptype = dateToken.getValueType();
+ break;
+
+ case ISOTIME:
+
+ /*
+ * This is a filler field "t" indicating that the next
+ * field is time. Try to verify that this is sensible.
+ */
+ tmask.set(0);
+
+ /* No preceding date? Then quit... */
+ if ((fmask & DateTimeConstants.DTK_DATE_M) != DateTimeConstants.DTK_DATE_M) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ /***
+ * We will need one of the following fields:
+ * DTK_NUMBER should be hhmmss.fff
+ * DTK_TIME should be hh:mm:ss.fff
+ * DTK_DATE should be hhmmss-zz
+ ***/
+ if (i >= nf - 1 ||
+ (fieldTypes[i + 1] != TokenField.DTK_NUMBER &&
+ fieldTypes[i + 1] != TokenField.DTK_TIME &&
+ fieldTypes[i + 1] != TokenField.DTK_DATE)) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+
+ ptype = dateToken.getValueType();
+ break;
+
+ case UNKNOWN_FIELD:
+
+ /*
+ * Before giving up and declaring error, check to see
+ * if it is an all-alpha timezone name.
+ */
+ namedTimeZone = pg_tzset(fields[i]);
+ if (namedTimeZone == null) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ /* we'll apply the zone setting below */
+ tmask.set(DateTimeConstants.DTK_M(TokenField.TZ));
+ break;
+
+ default:
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ break;
+ }
+ if ((tmask.get() & fmask) != 0) {
+ throw new IllegalArgumentException("BAD Format: " + fields[i]);
+ }
+ fmask |= tmask.get();
+ } /* end loop over fields */
+
+ tm.fsecs = fsec.intValue();
+ tm.timeZone = tz.get();
+ /* do final checking/adjustment of Y/M/D fields */
+ validateDate(fmask, isjulian, is2digits.get(), bc, tm);
+
+ /* handle AM/PM */
+ if (mer != null && mer != TokenField.HR24 && tm.hours > DateTimeConstants.HOURS_PER_DAY / 2) {
+ throw new IllegalArgumentException("BAD Format: overflow hour: " + tm.hours);
+ }
+ if (mer != null && mer == TokenField.AM && tm.hours == DateTimeConstants.HOURS_PER_DAY / 2) {
+ tm.hours = 0;
+ } else if (mer != null && mer == TokenField.PM && tm.hours != DateTimeConstants.HOURS_PER_DAY / 2) {
+ tm.hours += DateTimeConstants.HOURS_PER_DAY / 2;
+ }
+ /* do additional checking for full date specs... */
+ if (dtype == TokenField.DTK_DATE) {
+ if ((fmask & DateTimeConstants.DTK_DATE_M) != DateTimeConstants.DTK_DATE_M) {
+ if ((fmask & DateTimeConstants.DTK_TIME_M) == DateTimeConstants.DTK_TIME_M) {
+ return tm;
+ }
+ throw new IllegalArgumentException("BAD Format: " + tm);
+ }
+
+ /*
+ * If we had a full timezone spec, compute the offset (we could not do
+ * it before, because we need the date to resolve DST status).
+ */
+ if (namedTimeZone != null) {
+ /* daylight savings time modifier disallowed with full TZ */
+ if ( (fmask & DateTimeConstants.DTK_M(TokenField.DTZMOD)) != 0 ) {
+ throw new IllegalArgumentException("BAD Format: " + tm);
+ }
+ }
+ }
+
+ return tm;
+ }
+
+ private static String pg_tzset(String str) {
+ //TODO implements logic
+ return null;
+ }
+
+ /**
+ * Check valid year/month/day values, handle BC and DOY cases
+ * Return 0 if okay, a DTERR code if not.
+ * @param fmask
+ * @param isjulian
+ * @param is2digits
+ * @param bc
+ * @param tm
+ * @return
+ */
+ private static int validateDate(int fmask, boolean isjulian, boolean is2digits, boolean bc, TimeMeta tm) {
+ if ( (fmask & DateTimeConstants.DTK_M(TokenField.YEAR)) != 0 ) {
+ if (isjulian) {
+ /* tm_year is correct and should not be touched */
+ } else if (bc) {
+ /* there is no year zero in AD/BC notation */
+ if (tm.years <= 0) {
+ throw new IllegalArgumentException("BAD Format: year overflow:" + tm.years);
+ }
+ /* internally, we represent 1 BC as year zero, 2 BC as -1, etc */
+ tm.years = -(tm.years - 1);
+ }
+ else if (is2digits) {
+ /* process 1 or 2-digit input as 1970-2069 AD, allow '0' and '00' */
+ if (tm.years < 0) { /* just paranoia */
+ throw new IllegalArgumentException("BAD Format: year overflow:" + tm.years);
+ }
+ if (tm.years < 70) {
+ tm.years += 2000;
+ } else if (tm.years < 100) {
+ tm.years += 1900;
+ }
+ }
+ else {
+ /* there is no year zero in AD/BC notation */
+ if (tm.years <= 0) {
+ throw new IllegalArgumentException("BAD Format: year overflow:" + tm.years);
+ }
+ }
+ }
+
+ /* now that we have correct year, decode DOY */
+ if ( (fmask & DateTimeConstants.DTK_M(TokenField.DOY)) != 0 ) {
+ j2date(date2j(tm.years, 1, 1) + tm.dayOfYear - 1, tm);
+ }
+
+ /* check for valid month */
+ if ( (fmask & DateTimeConstants.DTK_M(TokenField.MONTH)) != 0 ) {
+ if (tm.monthOfYear < 1 || tm.monthOfYear > DateTimeConstants.MONTHS_PER_YEAR) {
+ throw new IllegalArgumentException("BAD Format: month overflow:" + tm.monthOfYear);
+ }
+ }
+
+ /* minimal check for valid day */
+ if ( (fmask & DateTimeConstants.DTK_M(TokenField.DAY)) != 0 ) {
+ if (tm.dayOfMonth < 1 || tm.dayOfMonth > 31) {
+ throw new IllegalArgumentException("BAD Format: day overflow:" + tm.dayOfMonth);
+ }
+ }
+
+ if ((fmask & DateTimeConstants.DTK_DATE_M) == DateTimeConstants.DTK_DATE_M) {
+ /*
+ * Check for valid day of month, now that we know for sure the month
+ * and year. Note we don't use MD_FIELD_OVERFLOW here, since it seems
+ * unlikely that "Feb 29" is a YMD-order error.
+ */
+ boolean leapYear = isLeapYear(tm.years);
+ if (tm.dayOfMonth > DateTimeConstants.DAY_OF_MONTH[leapYear ? 1: 0][tm.monthOfYear - 1])
+ throw new IllegalArgumentException("BAD Format: day overflow:" + tm.dayOfMonth);
+ }
+
+ return 0;
+ }
+
+ public static int strtoi(String str, int startIndex, StringBuilder sb) {
+ sb.setLength(0);
+ char[] chars = str.toCharArray();
+
+ int index = startIndex;
+ for (; index < chars.length; index++) {
+ if (!Character.isDigit(chars[index])) {
+ break;
+ }
+ }
+
+ int val = index == startIndex ? 0 : Integer.parseInt(str.substring(startIndex, index));
+ sb.append(chars, index, chars.length - index);
+
+ return val;
+ }
+
+ public static long strtol(String str, int startIndex, StringBuilder sb) {
+ sb.setLength(0);
+ char[] chars = str.toCharArray();
+
+ int index = startIndex;
+ for (; index < chars.length; index++) {
+ if (!Character.isDigit(chars[index])) {
+ break;
+ }
+ }
+
+ long val = index == startIndex ? 0 : Long.parseLong(str.substring(startIndex, index));
+ sb.append(chars, index, chars.length - index);
+
+ return val;
+ }
+
+ public static double strtod(String str, int strIndex, StringBuilder sb) {
+ if (sb != null) {
+ sb.setLength(0);
+ }
+ char[] chars = str.toCharArray();
+
+ int index = strIndex;
+ for (; index < chars.length; index++) {
+ if (!Character.isDigit(chars[index])) {
+ break;
+ }
+ }
+
+ double val = Double.parseDouble(str.substring(0, index));
+ if (sb != null) {
+ sb.append(chars, index, chars.length - index);
+ }
+ return val;
+ }
+
+ /**
+ * Check whether it is a punctuation character or not.
+ * @param c The character to be checked
+ * @return True if it is a punctuation character. Otherwise, false.
+ */
+ public static boolean isPunctuation(char c) {
+ return ((c >= '!' && c <= '/') ||
+ (c >= ':' && c <= '@') ||
+ (c >= '[' && c <= '`') ||
+ (c >= '{' && c <= '~'));
+ }
+
+ public static String toString(TimeMeta tm) {
+ return encodeDateTime(tm, DateStyle.ISO_DATES);
+ }
+
+ /**
+ * Encode date and time interpreted as local time.
+ *
+ * tm and fsec are the value to encode, print_tz determines whether to include
+ * a time zone (the difference between timestamp and timestamptz types), tz is
+ * the numeric time zone offset, tzn is the textual time zone, which if
+ * specified will be used instead of tz by some styles, style is the date
+ * style, str is where to write the output.
+ *
+ * Supported date styles:
+ * Postgres - day mon hh:mm:ss yyyy tz
+ * SQL - mm/dd/yyyy hh:mm:ss.ss tz
+ * ISO - yyyy-mm-dd hh:mm:ss+/-tz
+ * German - dd.mm.yyyy hh:mm:ss tz
+ * XSD - yyyy-mm-ddThh:mm:ss.ss+/-tz
+ *
+ * This method is originated from EncodeDateTime of datetime.c of PostgreSQL.
+ * @param tm
+ * @param style
+ * @return
+ */
+ public static String encodeDateTime(TimeMeta tm, DateStyle style) {
+
+ StringBuilder sb = new StringBuilder();
+ switch (style) {
+
+ case ISO_DATES:
+ case XSO_DATES:
+ if (style == DateTimeConstants.DateStyle.ISO_DATES) {
+ sb.append(String.format("%04d-%02d-%02d %02d:%02d:",
+ (tm.years > 0) ? tm.years : -(tm.years - 1),
+ tm.monthOfYear, tm.dayOfMonth, tm.hours, tm.minutes));
+ } else {
+ sb.append(String.format("%04d-%02d-%02dT%02d:%02d:",
+ (tm.years > 0) ? tm.years : -(tm.years - 1),
+ tm.monthOfYear, tm.dayOfMonth, tm.hours, tm.minutes));
+ }
+
+ appendSecondsToEncodeOutput(sb, tm.secs, tm.fsecs, 6, true);
+ if (tm.timeZone != 0 && tm.timeZone != Integer.MAX_VALUE) {
+ sb.append(getTimeZoneDisplayTime(tm.timeZone));
+ }
+ if (tm.years <= 0) {
+ sb.append(" BC");
+ }
+ break;
+
+ case SQL_DATES:
+ // Compatible with Oracle/Ingres date formats
+
+ }
+
+ return sb.toString();
+ }
+
+ public static String encodeDate(TimeMeta tm, DateStyle style) {
+ StringBuilder sb = new StringBuilder();
+ switch (style) {
+ case ISO_DATES:
+ case XSO_DATES:
+ case SQL_DATES:
+ // Compatible with Oracle/Ingres date formats
+ default:
+ sb.append(String.format("%04d-%02d-%02d",
+ (tm.years > 0) ? tm.years : -(tm.years - 1),
+ tm.monthOfYear, tm.dayOfMonth));
+ }
+
+ return sb.toString();
+ }
+
+ public static String encodeTime(TimeMeta tm, DateStyle style) {
+ StringBuilder sb = new StringBuilder();
+ switch (style) {
+
+ case ISO_DATES:
+ case XSO_DATES:
+ case SQL_DATES:
+ // Compatible with Oracle/Ingres date formats
+ default :
+ sb.append(String.format("%02d:%02d:", tm.hours, tm.minutes));
+ appendSecondsToEncodeOutput(sb, tm.secs, tm.fsecs, 6, true);
+ if (tm.timeZone != 0 && tm.timeZone != Integer.MAX_VALUE) {
+ sb.append(getTimeZoneDisplayTime(tm.timeZone));
+ }
+ break;
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Append sections and fractional seconds (if any) at *cp.
+ * precision is the max number of fraction digits, fillzeros says to
+ * pad to two integral-seconds digits.
+ * Note that any sign is stripped from the input seconds values.
+ *
+ * This method is originated form AppendSeconds in datetime.c of PostgreSQL.
+ */
+ public static void appendSecondsToEncodeOutput(
+ StringBuilder sb, int sec, int fsec, int precision, boolean fillzeros) {
+ if (fsec == 0) {
+ if (fillzeros)
+ sb.append(String.format("%02d", Math.abs(sec)));
+ else
+ sb.append(String.format("%d", Math.abs(sec)));
+ } else {
+ if (fillzeros) {
+ sb.append(String.format("%02d", Math.abs(sec)));
+ } else {
+ sb.append(String.format("%d", Math.abs(sec)));
+ }
+
+ if (precision > MAX_FRACTION_LENGTH) {
+ precision = MAX_FRACTION_LENGTH;
+ }
+
+ if (precision > 0) {
+ char[] fracChars = String.valueOf(fsec).toCharArray();
+ char[] resultChars = new char[MAX_FRACTION_LENGTH];
+
+ int numFillZero = MAX_FRACTION_LENGTH - fracChars.length;
+ for (int i = 0, fracIdx = 0; i < MAX_FRACTION_LENGTH; i++) {
+ if (i < numFillZero) {
+ resultChars[i] = '0';
+ } else {
+ resultChars[i] = fracChars[fracIdx];
+ fracIdx++;
+ }
+ }
+ sb.append(".").append(resultChars, 0, precision);
+ }
+ trimTrailingZeros(sb);
+ }
+ }
+
+ /**
+ * ... resulting from printing numbers with full precision.
+ *
+ * Before Postgres 8.4, this always left at least 2 fractional digits,
+ * but conversations on the lists suggest this isn't desired
+ * since showing '0.10' is misleading with values of precision(1).
+ *
+ * This method is originated form AppendSeconds in datetime.c of PostgreSQL.
+ * @param sb
+ */
+ public static void trimTrailingZeros(StringBuilder sb) {
+ int len = sb.length();
+ while (len > 1 && sb.charAt(len - 1) == '0' && sb.charAt(len - 2) != '.') {
+ len--;
+ sb.setLength(len);
+ }
+ }
+
+ /**
+ * Return the Julian day which corresponds to the first day (Monday) of the given ISO 8601 year and week.
+ * Julian days_full are used to convert between ISO week dates and Gregorian dates.
+ *
+ * This method is originated form AppendSeconds in timestamp.c of PostgreSQL.
+ * @param year
+ * @param week
+ * @return
+ */
+ public static int isoweek2j(int year, int week) {
+ /* fourth day of current year */
+ int day4 = date2j(year, 1, 4);
+
+ /* day0 == offset to first day of week (Monday) */
+ int day0 = j2day(day4 - 1);
+
+ return ((week - 1) * 7) + (day4 - day0);
+ }
+
+ /**
+ * Convert ISO week of year number to date.
+ * The year field must be specified with the ISO year!
+ * karel 2000/08/07
+ *
+ * This method is originated form AppendSeconds in timestamp.c of PostgreSQL.
+ * @param woy
+ * @param tm
+ */
+ public static void isoweek2date(int woy, TimeMeta tm) {
+ j2date(isoweek2j(tm.years, woy), tm);
+ }
+
+ /**
+ * Convert an ISO 8601 week date (ISO year, ISO week) into a Gregorian date.
+ * Gregorian day of week sent so weekday strings can be supplied.
+ * Populates year, mon, and mday with the correct Gregorian values.
+ * year must be passed in as the ISO year.
+ *
+ * This method is originated form AppendSeconds in timestamp.c of PostgreSQL.
+ * @param isoweek
+ * @param wday
+ * @param tm
+ */
+ public static void isoweekdate2date(int isoweek, int wday, TimeMeta tm) {
+ int jday;
+ jday = isoweek2j(tm.years, isoweek);
+ /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
+ if (wday > 1) {
+ jday += wday - 2;
+ } else {
+ jday += 6;
+ }
+ j2date(jday, tm);
+ }
+
+ /**
+ * Returns the ISO 8601 day-of-year, given a Gregorian year, month and day.
+ * Possible return values are 1 through 371 (364 in non-leap years).
+ * @param year
+ * @param mon
+ * @param mday
+ * @return
+ */
+ public static int date2isoyearday(int year, int mon, int mday) {
+ return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
+ }
+
+ public static void toUserTimezone(TimeMeta tm) {
+ toUserTimezone(tm, TajoConf.getCurrentTimeZone());
+ }
+
+ public static void toUserTimezone(TimeMeta tm, TimeZone timeZone) {
+ tm.plusMillis(timeZone.getRawOffset());
+ }
+
+ public static void toUTCTimezone(TimeMeta tm) {
+ TimeZone timeZone = TajoConf.getCurrentTimeZone();
+ tm.plusMillis(0 - timeZone.getRawOffset());
+ }
+
+ public static String getTimeZoneDisplayTime(TimeZone timeZone) {
+ return getTimeZoneDisplayTime(timeZone.getRawOffset() / 1000);
+ }
+
+ public static String getTimeZoneDisplayTime(int totalSecs) {
+ if (totalSecs == 0) {
+ return "";
+ }
+ int minutes = Math.abs(totalSecs) / DateTimeConstants.SECS_PER_MINUTE;
+ int hours = minutes / DateTimeConstants.MINS_PER_HOUR;
+ minutes = minutes - hours * DateTimeConstants.MINS_PER_HOUR;
+
+ StringBuilder sb = new StringBuilder();
+ String prefix = "";
+
+ sb.append(totalSecs > 0 ? "+" : "-").append(String.format("%02d", hours));
+
+ if (minutes > 0) {
+ sb.append(":").append(String.format("%02d", minutes));
+ prefix = ":";
+ }
+
+ return sb.toString();
+ }
+
+ public static long getDay(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay());
+ }
+
+ public static long getHour(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTime(dateTime.get(org.joda.time.DateTimeFieldType.hourOfDay()), 0, 0, 0));
+ }
+
+ public static long getMinute(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTime(dateTime.get(org.joda.time.DateTimeFieldType.hourOfDay()),
+ dateTime.get(org.joda.time.DateTimeFieldType.minuteOfHour()), 0, 0));
+ }
+
+ public static long getSecond(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTime(dateTime.get(org.joda.time.DateTimeFieldType.hourOfDay()),
+ dateTime.get(org.joda.time.DateTimeFieldType.minuteOfHour()), dateTime.get(org.joda.time.DateTimeFieldType.secondOfMinute()), 0));
+ }
+
+ public static long getMonth(DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDate(dateTime.getYear(),
+ dateTime.getMonthOfYear(),1));
+ }
+
+ public static long getDayOfWeek(DateTime dateTime,int week) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDayOfWeek(week));
+ }
+
+ public static long getYear (DateTime dateTime) {
+ return convertToMicroSeconds(dateTime.withTimeAtStartOfDay().withDate(dateTime.getYear(), 1, 1));
+ }
+
+ public static DateTime getUTCDateTime(Int8Datum int8Datum){
+ return new DateTime(int8Datum.asInt8()/1000, DateTimeZone.UTC);
+ }
+
+ public static long convertToMicroSeconds(DateTime dateTime) {
+ return dateTime.getMillis() * 1000;
+ }
+}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java b/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
new file mode 100644
index 0000000..25fe64a
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/util/datetime/TimeMeta.java
@@ -0,0 +1,154 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tajo.util.datetime;
+
+import org.apache.tajo.util.datetime.DateTimeConstants.DateStyle;
+
+public class TimeMeta {
+ public int fsecs; // 1/1,000,000 secs
+ public int secs;
+ public int minutes;
+ public int hours;
+ public int dayOfMonth;
+ public int dayOfYear; //used for only DateTimeFormat
+ public int monthOfYear; // origin 0, not 1
+ public int years; // relative to 1900
+ public boolean isDST; // daylight savings time
+ public int dayOfWeek;
+ public int timeZone = Integer.MAX_VALUE; //sec, used for only Text -> Timestamp
+
+ @Override
+ public String toString() {
+ return DateTimeUtil.encodeDateTime(this, DateStyle.ISO_DATES);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof TimeMeta)) {
+ return false;
+ }
+ TimeMeta other = (TimeMeta)o;
+
+ return fsecs == other.fsecs &&
+ secs == other.secs &&
+ minutes == other.minutes &&
+ hours == other.hours &&
+ dayOfMonth == other.dayOfMonth &&
+ dayOfYear == other.dayOfYear &&
+ monthOfYear == other.monthOfYear &&
+ years == other.years &&
+ isDST == other.isDST
+ ;
+ }
+
+ public void plusMonths(int months) {
+ if (months == 0) {
+ return;
+ }
+ int thisYear = years;
+ int thisMonth = monthOfYear;
+
+ int yearToUse;
+ // Initially, monthToUse is zero-based
+ int monthToUse = thisMonth - 1 + months;
+ if (monthToUse >= 0) {
+ yearToUse = thisYear + (monthToUse / DateTimeConstants.MONTHS_PER_YEAR);
+ monthToUse = (monthToUse % DateTimeConstants.MONTHS_PER_YEAR) + 1;
+ } else {
+ yearToUse = thisYear + (monthToUse / DateTimeConstants.MONTHS_PER_YEAR) - 1;
+ monthToUse = Math.abs(monthToUse);
+ int remMonthToUse = monthToUse % DateTimeConstants.MONTHS_PER_YEAR;
+ // Take care of the boundary condition
+ if (remMonthToUse == 0) {
+ remMonthToUse = DateTimeConstants.MONTHS_PER_YEAR;
+ }
+ monthToUse = DateTimeConstants.MONTHS_PER_YEAR - remMonthToUse + 1;
+ // Take care MONTHS_PER_YEAR the boundary condition
+ if (monthToUse == 1) {
+ yearToUse += 1;
+ }
+ }
+ // End of do not refactor.
+ // ----------------------------------------------------------
+
+ //
+ // Quietly force DOM to nearest sane value.
+ //
+ int dayToUse = dayOfMonth;
+ int maxDay = DateTimeUtil.getDaysInYearMonth(yearToUse, monthToUse);
+ if (dayToUse > maxDay) {
+ dayToUse = maxDay;
+ }
+
+ this.years = yearToUse;
+ this.monthOfYear = monthToUse;
+ this.dayOfMonth = dayToUse;
+ }
+
+ public void plusDays(int days) {
+ long timestamp = DateTimeUtil.toJulianTimestamp(this);
+ timestamp += days * DateTimeConstants.USECS_PER_DAY;
+
+ DateTimeUtil.toJulianTimeMeta(timestamp, this);
+ }
+
+ public void plusMillis(long millis) {
+ plusTime(millis * 1000);
+ }
+
+ public void plusTime(long time) {
+ long timestamp = DateTimeUtil.toJulianTimestamp(this);
+ timestamp += time;
+ DateTimeUtil.toJulianTimeMeta(timestamp, this);
+ }
+
+ public int getCenturyOfEra() {
+ return DateTimeUtil.getCenturyOfEra(years);
+ }
+
+ public int getDayOfYear() {
+ int dayOfYear = 0;
+ for (int i = 0; i < monthOfYear - 1; i++) {
+ dayOfYear += DateTimeUtil.getDaysInYearMonth(years, i + 1);
+ }
+
+ return dayOfYear + dayOfMonth;
+ }
+
+ public int getWeekOfYear() {
+ return DateTimeUtil.date2isoweek(years, monthOfYear, dayOfMonth);
+ }
+
+ public int getWeekyear() {
+ return DateTimeUtil.date2isoyear(years, monthOfYear, dayOfMonth);
+ }
+
+ public int getISODayOfWeek() {
+ int dow = getDayOfWeek();
+ if (dow == 0) { //Sunday
+ return 7;
+ } else {
+ return dow;
+ }
+ }
+
+ public int getDayOfWeek() {
+ return (DateTimeUtil.date2j(years, monthOfYear, dayOfMonth) + 1) % 7;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
index 2123dc5..f2ad261 100644
--- a/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestDateDatum.java
@@ -68,13 +68,6 @@ public class TestDateDatum {
assertEquals(d, copy);
}
- @Test
- public final void testAsByteArray() {
- DateDatum d = DatumFactory.createDate(DATE);
- DateDatum copy = new DateDatum(d.asByteArray());
- assertEquals(d.asInt8(), copy.asInt8());
- }
-
@Test
public final void testSize() {
Datum d = DatumFactory.createDate(DATE);
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/datum/TestIntervalDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestIntervalDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestIntervalDatum.java
index 3bce64c..511b356 100644
--- a/tajo-common/src/test/java/org/apache/tajo/datum/TestIntervalDatum.java
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestIntervalDatum.java
@@ -19,9 +19,9 @@
package org.apache.tajo.datum;
import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.conf.TajoConf;
import org.apache.tajo.exception.InvalidOperationException;
-import org.joda.time.DateTime;
-import org.joda.time.LocalTime;
+import org.apache.tajo.util.datetime.DateTimeUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@@ -64,27 +64,35 @@ public class TestIntervalDatum {
// http://www.postgresql.org/docs/8.2/static/functions-datetime.html
// date '2001-09-28' + integer '7' ==> date '2001-10-05'
- Datum datum = new DateDatum(2001, 9, 28);
+ Datum datum = DatumFactory.createDate(2001, 9, 28);
Datum[] datums = new Datum[]{new Int2Datum((short) 7), new Int4Datum(7), new Int8Datum(7),
new Float4Datum(7.0f), new Float8Datum(7.0f)};
for (int i = 0; i < datums.length; i++) {
Datum result = datum.plus(datums[i]);
assertEquals(TajoDataTypes.Type.DATE, result.type());
- assertEquals(new DateDatum(2001, 10, 5), result);
+ assertEquals("date '2001-09-28' + " + datums[i].asChars() + "(" + i + " th test)", "2001-10-05", result.asChars());
}
+ //TimestampDatum and TimeDatum should be TimeZone when convert to string
// date '2001-09-28' + interval '1 hour' ==> timestamp '2001-09-28 01:00:00'
- datum = new DateDatum(2001, 9, 28);
+ datum = DatumFactory.createDate(2001, 9, 28);
Datum result = datum.plus(new IntervalDatum(60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 28, 1, 0, 0, 0)), result);
+ assertEquals("2001-09-28 01:00:00", ((TimestampDatum)result).asChars(TajoConf.getCurrentTimeZone(), false));
+
+ // interval '1 hour' + date '2001-09-28' ==> timestamp '2001-09-28 01:00:00'
+ datum = new IntervalDatum(60 * 60 * 1000);
+ result = datum.plus(DatumFactory.createDate(2001, 9, 28));
+ assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
+ assertEquals("2001-09-28 01:00:00", ((TimestampDatum)result).asChars(TajoConf.getCurrentTimeZone(), false));
// date '2001-09-28' + time '03:00' ==> timestamp '2001-09-28 03:00:00'
- datum = new DateDatum(2001, 9, 28);
- result = datum.plus(new TimeDatum(new LocalTime(3, 0)));
+ datum = DatumFactory.createDate(2001, 9, 28);
+ TimeDatum time = new TimeDatum(DateTimeUtil.toTime(3, 0, 0, 0));
+ result = datum.plus(time);
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 28, 3, 0, 0, 0)), result);
+ assertEquals("2001-09-28 03:00:00", result.asChars());
// interval '1 day' + interval '1 hour' interval '1 day 01:00:00'
datum = new IntervalDatum(IntervalDatum.DAY_MILLIS);
@@ -93,63 +101,65 @@ public class TestIntervalDatum {
assertEquals("1 day 01:00:00", result.asChars());
// timestamp '2001-09-28 01:00' + interval '23 hours' ==> timestamp '2001-09-29 00:00:00'
- datum = new TimestampDatum(new DateTime(2001, 9, 28, 1, 0, 0, 0));
+ datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(2001, 9, 28, 1, 0, 0, 0));
result = datum.plus(new IntervalDatum(23 * 60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 29, 0, 0, 0, 0)), result);
+ assertEquals("2001-09-29 00:00:00", result.asChars());
// time '01:00' + interval '3 hours' ==> time '04:00:00'
- datum = new TimeDatum(new LocalTime(1, 0, 0, 0));
+ datum = new TimeDatum(DateTimeUtil.toTime(1, 0, 0, 0));
result = datum.plus(new IntervalDatum(3 * 60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIME, result.type());
- assertEquals(new TimeDatum(new LocalTime(4, 0, 0, 0)), result);
+ assertEquals(new TimeDatum(DateTimeUtil.toTime(4, 0, 0, 0)), result);
-// // - interval '23 hours' ==> interval '-23:00:00'
-// // TODO Currently Interval's inverseSign() not supported
+ // - interval '23 hours' ==> interval '-23:00:00'
+ // TODO Currently Interval's inverseSign() not supported
// date '2001-10-01' - date '2001-09-28' ==> integer '3'
- datum = new DateDatum(2001, 10, 01);
- result = datum.minus(new DateDatum(2001, 9, 28));
+ datum = DatumFactory.createDate(2001, 10, 1);
+ result = datum.minus(DatumFactory.createDate(2001, 9, 28));
assertEquals(TajoDataTypes.Type.INT4, result.type());
assertEquals(new Int4Datum(3), result);
// date '2001-10-01' - integer '7' ==> date '2001-09-24'
- datum = new DateDatum(2001, 10, 01);
+ datum = DatumFactory.createDate(2001, 10, 1);
for (int i = 0; i < datums.length; i++) {
Datum result2 = datum.minus(datums[i]);
assertEquals(TajoDataTypes.Type.DATE, result2.type());
- assertEquals(new DateDatum(2001, 9, 24), result2);
+ assertEquals(DatumFactory.createDate(2001, 9, 24), result2);
}
// date '2001-09-28' - interval '1 hour' ==> timestamp '2001-09-27 23:00:00'
- datum = new DateDatum(2001, 9, 28);
+ datum = DatumFactory.createDate(2001, 9, 28);
result = datum.minus(new IntervalDatum(1 * 60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 27, 23, 0, 0, 0)), result);
+ assertEquals("2001-09-27 23:00:00", ((TimestampDatum)result).asChars(TajoConf.getCurrentTimeZone(), false));
// date '2001-09-28' - interval '1 day 1 hour' ==> timestamp '2001-09-26 23:00:00'
- datum = new DateDatum(2001, 9, 28);
+ // In this case all datums are UTC
+ datum = DatumFactory.createDate(2001, 9, 28);
result = datum.minus(new IntervalDatum(IntervalDatum.DAY_MILLIS + 1 * 60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 26, 23, 0, 0, 0)), result);
+ assertEquals("2001-09-26 23:00:00", ((TimestampDatum)result).asChars(TajoConf.getCurrentTimeZone(), false));
// time '05:00' - time '03:00' ==> interval '02:00:00'
- datum = new TimeDatum(new LocalTime(5, 0, 0, 0));
- result = datum.minus(new TimeDatum(new LocalTime(3, 0, 0, 0)));
+ datum = new TimeDatum(DateTimeUtil.toTime(5, 0, 0, 0));
+ result = datum.minus(new TimeDatum(DateTimeUtil.toTime(3, 0, 0, 0)));
assertEquals(TajoDataTypes.Type.INTERVAL, result.type());
assertEquals(new IntervalDatum(2 * 60 * 60 * 1000), result);
// time '05:00' - interval '2 hours' ==> time '03:00:00'
- datum = new TimeDatum(new LocalTime(5, 0, 0, 0));
+ datum = new TimeDatum(DateTimeUtil.toTime(5, 0, 0, 0));
result = datum.minus(new IntervalDatum(2 * 60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIME, result.type());
- assertEquals(new TimeDatum(3, 0, 0, 0), result);
+ assertEquals(new TimeDatum(DateTimeUtil.toTime(3, 0, 0, 0)), result);
// timestamp '2001-09-28 23:00' - interval '23 hours' ==> timestamp '2001-09-28 00:00:00'
- datum = new TimestampDatum(new DateTime(2001, 9, 28, 23, 0, 0, 0));
+ // In this case all datums are UTC
+ datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(2001, 9, 28, 23, 0, 0, 0));
result = datum.minus(new IntervalDatum(23 * 60 * 60 * 1000));
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 28, 0, 0, 0, 0)), result);
+ assertEquals("2001-09-28 00:00:00", result.asChars());
// interval '1 day' - interval '1 hour' ==> interval '1 day -01:00:00'
datum = new IntervalDatum(IntervalDatum.DAY_MILLIS);
@@ -158,8 +168,8 @@ public class TestIntervalDatum {
assertEquals(new IntervalDatum(23 * 60 * 60 * 1000), result);
// timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' ==> interval '1 day 15:00:00'
- datum = new TimestampDatum(new DateTime(2001, 9, 29, 3, 0, 0, 0));
- result = datum.minus(new TimestampDatum(new DateTime(2001, 9, 27, 12, 0, 0, 0)));
+ datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(2001, 9, 29, 3, 0, 0, 0));
+ result = datum.minus(new TimestampDatum(DateTimeUtil.toJulianTimestamp(2001, 9, 27, 12, 0, 0, 0)));
assertEquals(TajoDataTypes.Type.INTERVAL, result.type());
assertEquals(new IntervalDatum(IntervalDatum.DAY_MILLIS + 15 * 60 * 60 * 1000), result);
@@ -191,9 +201,10 @@ public class TestIntervalDatum {
assertEquals(new IntervalDatum(40 * 60 * 1000), result);
// timestamp '2001-08-31 01:00:00' + interval '1 mons' ==> timestamp 2001-09-30 01:00:00
- datum = new TimestampDatum(new DateTime(2001, 8, 31, 1, 0, 0, 0));
+ // In this case all datums are UTC
+ datum = new TimestampDatum(DateTimeUtil.toJulianTimestamp(2001, 8, 31, 1, 0, 0, 0));
result = datum.plus(new IntervalDatum(1, 0));
assertEquals(TajoDataTypes.Type.TIMESTAMP, result.type());
- assertEquals(new TimestampDatum(new DateTime(2001, 9, 30, 1, 0, 0, 0)), result);
+ assertEquals("2001-09-30 01:00:00", result.asChars());
}
}
http://git-wip-us.apache.org/repos/asf/tajo/blob/526dca28/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
index c405d68..ea641ec 100644
--- a/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
+++ b/tajo-common/src/test/java/org/apache/tajo/datum/TestTimeDatum.java
@@ -70,13 +70,6 @@ public class TestTimeDatum {
}
@Test
- public final void testAsByteArray() {
- TimeDatum d = DatumFactory.createTime(TIME);
- TimeDatum copy = new TimeDatum(d.asByteArray());
- assertEquals(d.asInt8(), copy.asInt8());
- }
-
- @Test
public final void testSize() {
Datum d = DatumFactory.createTime(TIME);
assertEquals(TimeDatum.SIZE, d.asByteArray().length);