You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2020/03/03 16:06:50 UTC

[incubator-iotdb] 01/01: Support date format 2020-02-10

This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch new_date_format
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git

commit 9e9e9b1f4156db690a741c7b8ed19e1d9f3d1603
Author: HTHou <hh...@outlook.com>
AuthorDate: Wed Mar 4 00:06:04 2020 +0800

    Support date format 2020-02-10
---
 .../org/apache/iotdb/db/qp/strategy/SqlBase.g4     |  4 ++--
 .../apache/iotdb/db/qp/constant/DatetimeUtils.java |  2 ++
 .../db/sql/DatetimeQueryDataSetUtilsTest.java      | 23 ++++++++++++++++++++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4 b/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
index 55943fc..acf7efc 100644
--- a/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
+++ b/server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
@@ -818,9 +818,9 @@ DURATION
 
 DATETIME
     : INT ('-'|'/') INT ('-'|'/') INT
-      (T | WS)
+      ((T | WS)
       INT ':' INT ':' INT (DOT INT)?
-      (('+' | '-') INT ':' INT)?
+      (('+' | '-') INT ':' INT)?)?
     ;
 /** Allow unicode rule/token names */
 ID : NameChar NameChar*;
diff --git a/server/src/main/java/org/apache/iotdb/db/qp/constant/DatetimeUtils.java b/server/src/main/java/org/apache/iotdb/db/qp/constant/DatetimeUtils.java
index 1111125..ef06ef3 100644
--- a/server/src/main/java/org/apache/iotdb/db/qp/constant/DatetimeUtils.java
+++ b/server/src/main/java/org/apache/iotdb/db/qp/constant/DatetimeUtils.java
@@ -464,6 +464,8 @@ public class DatetimeUtils {
     if (str.contains("Z")) {
       return convertDatetimeStrToLong(str.substring(0, str.indexOf('Z')) + "+00:00", offset,
           depth);
+    } else if (str.length() == 10) {
+      return convertDatetimeStrToLong(str + "T00:00:00", offset, depth);
     } else if (str.length() - str.lastIndexOf('+') != 6
         && str.length() - str.lastIndexOf('-') != 6) {
       return convertDatetimeStrToLong(str + offset, offset, depth + 1);
diff --git a/server/src/test/java/org/apache/iotdb/db/sql/DatetimeQueryDataSetUtilsTest.java b/server/src/test/java/org/apache/iotdb/db/sql/DatetimeQueryDataSetUtilsTest.java
index 84e2f72..c3043e3 100644
--- a/server/src/test/java/org/apache/iotdb/db/sql/DatetimeQueryDataSetUtilsTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/sql/DatetimeQueryDataSetUtilsTest.java
@@ -37,6 +37,8 @@ public class DatetimeQueryDataSetUtilsTest {
   // 1546413207689
   // 2019-01-02T15:13:27.689+08:00
   private final long timestamp = 1546413207689L;
+  // 2019-01-02T00:00:00.000+08:00
+  private final long timestamp1 = 1546358400000L;
   private long delta;
 
   @Before
@@ -69,6 +71,14 @@ public class DatetimeQueryDataSetUtilsTest {
     testConvertDatetimeStrToLongWithMS(zoneOffset, zoneId, timestamp + delta);
   }
 
+  @Test
+  public void test3() throws LogicalOperatorException{
+    zoneOffset = ZoneOffset.UTC;
+    zoneId = ZoneId.of("Etc/UTC");
+    delta = 8 * 3600000;
+    testConvertDateStrToLong(zoneOffset, zoneId, timestamp1 + delta);
+  }
+
   public void testConvertDatetimeStrToLongWithoutMS(ZoneOffset zoneOffset, ZoneId zoneId, long res) throws LogicalOperatorException {
     String[] timeFormatWithoutMs = new String[]{"2019-01-02 15:13:27", "2019/01/02 15:13:27",
         "2019.01.02 15:13:27", "2019-01-02T15:13:27", "2019/01/02T15:13:27", "2019.01.02T15:13:27",
@@ -102,6 +112,19 @@ public class DatetimeQueryDataSetUtilsTest {
     }
   }
 
+  public void testConvertDateStrToLong(ZoneOffset zoneOffset, ZoneId zoneId, long res) throws LogicalOperatorException {
+    String[] timeFormatWithoutMs = new String[]{"2019-01-02",
+        "2019/01/02",
+        "2019.01.02",};
+    for (String str : timeFormatWithoutMs) {
+      assertEquals(res, DatetimeUtils.convertDatetimeStrToLong(str, zoneOffset, 0));
+    }
+
+    for (String str : timeFormatWithoutMs) {
+      assertEquals(res, DatetimeUtils.convertDatetimeStrToLong(str, zoneId));
+    }
+  }
+
   public void createTest() {
     // long timestamp = System.currentTimeMillis();
     // System.out.println(timestamp);