Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public PhysicalPlan parseSQLToPhysicalPlan(String sqlStr)
public PhysicalPlan parseSQLToPhysicalPlan(String sqlStr, ZoneId zoneId)
throws QueryProcessorException, ArgsErrorException, ProcessorException {
AstNode astNode = parseSQLToAST(sqlStr);
System.out.println("parseASTToOperator");
System.out.println(zoneId);
Operator operator = parseASTToOperator(astNode, zoneId);
operator = logicalOptimize(operator, executor);
PhysicalGenerator physicalGenerator = new PhysicalGenerator(executor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,23 @@ public class DatetimeUtils {

public static long convertDatetimeStrToMillisecond(String str, ZoneId zoneId)
throws LogicalOperatorException {
return convertDatetimeStrToMillisecond(str, toZoneOffset(zoneId));
return convertDatetimeStrToMillisecond(str, toZoneOffset(zoneId), 0);
}

/**
* convert date time string to millisecond.
*/
public static long convertDatetimeStrToMillisecond(String str, ZoneOffset offset)
public static long convertDatetimeStrToMillisecond(String str, ZoneOffset offset, int depth)
throws LogicalOperatorException {
if (str.length() - str.lastIndexOf('+') != 6 && str.length() - str.lastIndexOf('-') != 6) {
return convertDatetimeStrToMillisecond(str + offset, offset);
if (depth >= 2){
throw new DateTimeException(
String.format("Failed to convert %s to millisecond, zone offset is %s, "
+ "please input like 2011-12-03T10:15:30 or 2011-12-03T10:15:30+01:00", str, offset));
}
if (str.indexOf('Z') > 0){
return convertDatetimeStrToMillisecond(str.substring(0, str.indexOf('Z')) + "+00:00", offset, depth);
} else if (str.length() - str.lastIndexOf('+') != 6 && str.length() - str.lastIndexOf('-') != 6) {
return convertDatetimeStrToMillisecond(str + offset, offset, depth + 1);
} else if (str.indexOf('[') > 0 || str.indexOf(']') > 0) {
throw new DateTimeException(
String.format("%s with [time-region] at end is not supported now, "
Expand Down
45 changes: 30 additions & 15 deletions iotdb/src/test/java/org/apache/iotdb/db/sql/DatetimeUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,47 @@ public class DatetimeUtilsTest {
private ZoneId zoneId;
// 1546413207689
// 2019-01-02T15:13:27.689+08:00
private final long timestamp = 1546413207689L;
private long delta;

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test1() throws LogicalOperatorException{
zoneOffset = ZonedDateTime.now().getOffset();
zoneId = ZoneId.systemDefault();
System.out.println(zoneOffset.toString());
if(zoneOffset.toString().equals("Z")){
delta = 8 * 3600000;
zoneOffset = ZoneOffset.of("+00:00");
} else {
delta = (8 - Long.parseLong(zoneOffset.toString().split(":")[0])) * 3600000;
}
System.out.println(delta);
testConvertDatetimeStrToLongWithoutMS(zoneOffset, zoneId, timestamp - 689 + delta);
testConvertDatetimeStrToLongWithMS(zoneOffset, zoneId, timestamp + delta);
}

@After
public void tearDown() throws Exception {
@Test
public void test2() throws LogicalOperatorException{
zoneOffset = ZoneOffset.UTC;
zoneId = ZoneId.of("Etc/UTC");
delta = 8 * 3600000;
testConvertDatetimeStrToLongWithoutMS(zoneOffset, zoneId, timestamp - 689 + delta);
testConvertDatetimeStrToLongWithMS(zoneOffset, zoneId, timestamp + delta);
}

@Test
public void testConvertDatetimeStrToLongWithoutMS() throws LogicalOperatorException {
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",
"2019-01-02 15:13:27" + zoneOffset, "2019/01/02 15:13:27" + zoneOffset,
"2019.01.02 15:13:27" + zoneOffset, "2019-01-02T15:13:27" + zoneOffset,
"2019/01/02T15:13:27" + zoneOffset, "2019.01.02T15:13:27" + zoneOffset,};

long res = 1546413207000L + delta;
for (String str : timeFormatWithoutMs) {
Assert.assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset));
Assert.assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset, 0));
}

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

}

@Test
public void testConvertDatetimeStrToLongWithMS() throws LogicalOperatorException {
public void testConvertDatetimeStrToLongWithMS(ZoneOffset zoneOffset, ZoneId zoneId, long res) throws LogicalOperatorException {
String[] timeFormatWithoutMs = new String[]{"2019-01-02 15:13:27.689",
"2019/01/02 15:13:27.689",
"2019.01.02 15:13:27.689", "2019-01-02T15:13:27.689", "2019/01/02T15:13:27.689",
"2019.01.02T15:13:27.689", "2019-01-02 15:13:27.689" + zoneOffset,
"2019/01/02 15:13:27.689" + zoneOffset, "2019.01.02 15:13:27.689" + zoneOffset,
"2019-01-02T15:13:27.689" + zoneOffset, "2019/01/02T15:13:27.689" + zoneOffset,
"2019.01.02T15:13:27.689" + zoneOffset,};

long res = 1546413207689L + delta;
for (String str : timeFormatWithoutMs) {
assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset));
assertEquals(res, DatetimeUtils.convertDatetimeStrToMillisecond(str, zoneOffset, 0));
}

for (String str : timeFormatWithoutMs) {
Expand All @@ -101,4 +108,12 @@ public void createTest() {
// ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("+08:00"));
// System.out.println(zonedDateTime);
}

public static void main(String[] args){
// System.out.println(DatetimeUtils.toZoneOffset(ZoneId.of("Etc/UTC")));
for(String zoneId : ZoneId.getAvailableZoneIds()){
System.out.println(zoneId + ": " + DatetimeUtils.toZoneOffset(ZoneId.of(zoneId)));
}
// System.out.println(ZoneOffset.of("+00:00"));
}
}