Skip to content

Commit fbb3eff

Browse files
authored
Merge pull request #14 from MyXOF/master
2 parents c7d7c59 + 868758e commit fbb3eff

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

iotdb/src/main/java/org/apache/iotdb/db/qp/QueryProcessor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public PhysicalPlan parseSQLToPhysicalPlan(String sqlStr)
6868
public PhysicalPlan parseSQLToPhysicalPlan(String sqlStr, ZoneId zoneId)
6969
throws QueryProcessorException, ArgsErrorException, ProcessorException {
7070
AstNode astNode = parseSQLToAST(sqlStr);
71-
System.out.println("parseASTToOperator");
72-
System.out.println(zoneId);
7371
Operator operator = parseASTToOperator(astNode, zoneId);
7472
operator = logicalOptimize(operator, executor);
7573
PhysicalGenerator physicalGenerator = new PhysicalGenerator(executor);

iotdb/src/main/java/org/apache/iotdb/db/qp/constant/DatetimeUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,23 @@ public class DatetimeUtils {
177177

178178
public static long convertDatetimeStrToMillisecond(String str, ZoneId zoneId)
179179
throws LogicalOperatorException {
180-
return convertDatetimeStrToMillisecond(str, toZoneOffset(zoneId));
180+
return convertDatetimeStrToMillisecond(str, toZoneOffset(zoneId), 0);
181181
}
182182

183183
/**
184184
* convert date time string to millisecond.
185185
*/
186-
public static long convertDatetimeStrToMillisecond(String str, ZoneOffset offset)
186+
public static long convertDatetimeStrToMillisecond(String str, ZoneOffset offset, int depth)
187187
throws LogicalOperatorException {
188-
if (str.length() - str.lastIndexOf('+') != 6 && str.length() - str.lastIndexOf('-') != 6) {
189-
return convertDatetimeStrToMillisecond(str + offset, offset);
188+
if (depth >= 2){
189+
throw new DateTimeException(
190+
String.format("Failed to convert %s to millisecond, zone offset is %s, "
191+
+ "please input like 2011-12-03T10:15:30 or 2011-12-03T10:15:30+01:00", str, offset));
192+
}
193+
if (str.indexOf('Z') > 0){
194+
return convertDatetimeStrToMillisecond(str.substring(0, str.indexOf('Z')) + "+00:00", offset, depth);
195+
} else if (str.length() - str.lastIndexOf('+') != 6 && str.length() - str.lastIndexOf('-') != 6) {
196+
return convertDatetimeStrToMillisecond(str + offset, offset, depth + 1);
190197
} else if (str.indexOf('[') > 0 || str.indexOf(']') > 0) {
191198
throw new DateTimeException(
192199
String.format("%s with [time-region] at end is not supported now, "

iotdb/src/test/java/org/apache/iotdb/db/sql/DatetimeUtilsTest.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,47 @@ public class DatetimeUtilsTest {
3636
private ZoneId zoneId;
3737
// 1546413207689
3838
// 2019-01-02T15:13:27.689+08:00
39+
private final long timestamp = 1546413207689L;
3940
private long delta;
4041

4142
@Before
4243
public void setUp() throws Exception {
44+
}
45+
46+
@After
47+
public void tearDown() throws Exception {
48+
}
49+
50+
@Test
51+
public void test1() throws LogicalOperatorException{
4352
zoneOffset = ZonedDateTime.now().getOffset();
4453
zoneId = ZoneId.systemDefault();
45-
System.out.println(zoneOffset.toString());
4654
if(zoneOffset.toString().equals("Z")){
4755
delta = 8 * 3600000;
48-
zoneOffset = ZoneOffset.of("+00:00");
4956
} else {
5057
delta = (8 - Long.parseLong(zoneOffset.toString().split(":")[0])) * 3600000;
5158
}
52-
System.out.println(delta);
59+
testConvertDatetimeStrToLongWithoutMS(zoneOffset, zoneId, timestamp - 689 + delta);
60+
testConvertDatetimeStrToLongWithMS(zoneOffset, zoneId, timestamp + delta);
5361
}
5462

55-
@After
56-
public void tearDown() throws Exception {
63+
@Test
64+
public void test2() throws LogicalOperatorException{
65+
zoneOffset = ZoneOffset.UTC;
66+
zoneId = ZoneId.of("Etc/UTC");
67+
delta = 8 * 3600000;
68+
testConvertDatetimeStrToLongWithoutMS(zoneOffset, zoneId, timestamp - 689 + delta);
69+
testConvertDatetimeStrToLongWithMS(zoneOffset, zoneId, timestamp + delta);
5770
}
5871

59-
@Test
60-
public void testConvertDatetimeStrToLongWithoutMS() throws LogicalOperatorException {
72+
public void testConvertDatetimeStrToLongWithoutMS(ZoneOffset zoneOffset, ZoneId zoneId, long res) throws LogicalOperatorException {
6173
String[] timeFormatWithoutMs = new String[]{"2019-01-02 15:13:27", "2019/01/02 15:13:27",
6274
"2019.01.02 15:13:27", "2019-01-02T15:13:27", "2019/01/02T15:13:27", "2019.01.02T15:13:27",
6375
"2019-01-02 15:13:27" + zoneOffset, "2019/01/02 15:13:27" + zoneOffset,
6476
"2019.01.02 15:13:27" + zoneOffset, "2019-01-02T15:13:27" + zoneOffset,
6577
"2019/01/02T15:13:27" + zoneOffset, "2019.01.02T15:13:27" + zoneOffset,};
66-
67-
long res = 1546413207000L + delta;
6878
for (String str : timeFormatWithoutMs) {
69-
Assert.assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset));
79+
Assert.assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset, 0));
7080
}
7181

7282
for (String str : timeFormatWithoutMs) {
@@ -75,19 +85,16 @@ public void testConvertDatetimeStrToLongWithoutMS() throws LogicalOperatorExcept
7585

7686
}
7787

78-
@Test
79-
public void testConvertDatetimeStrToLongWithMS() throws LogicalOperatorException {
88+
public void testConvertDatetimeStrToLongWithMS(ZoneOffset zoneOffset, ZoneId zoneId, long res) throws LogicalOperatorException {
8089
String[] timeFormatWithoutMs = new String[]{"2019-01-02 15:13:27.689",
8190
"2019/01/02 15:13:27.689",
8291
"2019.01.02 15:13:27.689", "2019-01-02T15:13:27.689", "2019/01/02T15:13:27.689",
8392
"2019.01.02T15:13:27.689", "2019-01-02 15:13:27.689" + zoneOffset,
8493
"2019/01/02 15:13:27.689" + zoneOffset, "2019.01.02 15:13:27.689" + zoneOffset,
8594
"2019-01-02T15:13:27.689" + zoneOffset, "2019/01/02T15:13:27.689" + zoneOffset,
8695
"2019.01.02T15:13:27.689" + zoneOffset,};
87-
88-
long res = 1546413207689L + delta;
8996
for (String str : timeFormatWithoutMs) {
90-
assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset));
97+
assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset, 0));
9198
}
9299

93100
for (String str : timeFormatWithoutMs) {
@@ -101,4 +108,12 @@ public void createTest() {
101108
// ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("+08:00"));
102109
// System.out.println(zonedDateTime);
103110
}
111+
112+
public static void main(String[] args){
113+
// System.out.println(DatetimeUtils.toZoneOffset(ZoneId.of("Etc/UTC")));
114+
for(String zoneId : ZoneId.getAvailableZoneIds()){
115+
System.out.println(zoneId + ": " + DatetimeUtils.toZoneOffset(ZoneId.of(zoneId)));
116+
}
117+
// System.out.println(ZoneOffset.of("+00:00"));
118+
}
104119
}

0 commit comments

Comments
 (0)