From db8049dabd78fef6eb962a01622f51ca960b685c Mon Sep 17 00:00:00 2001 From: suhothayan Date: Tue, 22 Sep 2015 15:02:57 +0530 Subject: [PATCH] fixing time function --- .../time/DateFormatFunctionExtension.java | 47 +++++++++---------- .../ExtractAttributesFunctionExtension.java | 2 +- ...ctAttributesFunctionExtensionTestCase.java | 8 ++-- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/DateFormatFunctionExtension.java b/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/DateFormatFunctionExtension.java index 6322b59721..23acf27ffa 100644 --- a/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/DateFormatFunctionExtension.java +++ b/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/DateFormatFunctionExtension.java @@ -39,12 +39,12 @@ * timestampInMilliseconds - date value in milliseconds.(from the epoch) eg: 1415712224000L * dateTargetFormat - Date format which need to be converted to. eg: yyyy/MM/dd HH:mm:ss * Accept Type(s) for dateFormat(dateValue,dateTargetFormat,dateSourceFormat): - * dateValue : STRING - * dateTargetFormat : STRING - * dateSourceFormat : STRING + * dateValue : STRING + * dateTargetFormat : STRING + * dateSourceFormat : STRING * Accept Type(s) for dateFormat(timestampInMilliseconds,dateTargetFormat): - * timestampInMilliseconds : LONG - * dateTargetFormat : STRING + * timestampInMilliseconds : LONG + * dateTargetFormat : STRING * Return Type(s): STRING */ public class DateFormatFunctionExtension extends FunctionExecutor { @@ -56,10 +56,10 @@ public class DateFormatFunctionExtension extends FunctionExecutor { @Override protected void init(ExpressionExecutor[] attributeExpressionExecutors, - ExecutionPlanContext executionPlanContext) { + ExecutionPlanContext executionPlanContext) { - if(attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG && attributeExpressionExecutors - .length == 2){ + if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG && attributeExpressionExecutors + .length == 2) { useDefaultDateFormat = true; sourceDateFormat = TimeExtensionConstants.EXTENSION_TIME_DEFAULT_DATE_FORMAT; } @@ -84,7 +84,7 @@ protected void init(ExpressionExecutor[] attributeExpressionExecutors, " but found " + attributeExpressionExecutors[2].getReturnType().toString()); } } else if (attributeExpressionExecutors.length == 2) { - if(useDefaultDateFormat){ + if (useDefaultDateFormat) { if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) { throw new ExecutionPlanValidationException("Invalid parameter type found for the first argument of " + "time:dateFormat(dateValue,dateTargetFormat,dateSourceFormat) function, " + "required " @@ -97,7 +97,7 @@ protected void init(ExpressionExecutor[] attributeExpressionExecutors, + Attribute.Type.STRING + " but found " + attributeExpressionExecutors[1].getReturnType().toString()); } - } else{ + } else { if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG) { throw new ExecutionPlanValidationException("Invalid parameter type found for the first argument of " + "time:dateFormat(timestampInMilliseconds,dateTargetFormat) function, " + @@ -139,7 +139,7 @@ protected Object execute(Object[] data) { throw new ExecutionPlanRuntimeException("Invalid input given to time:dateFormat(dateValue," + "dateTargetFormat,dateSourceFormat) function" + ". Second " + "argument cannot be null"); } - if(!useDefaultDateFormat){ + if (!useDefaultDateFormat) { if (data[2] == null) { throw new ExecutionPlanRuntimeException("Invalid input given to time:dateFormat(dateValue," + "dateTargetFormat,dateSourceFormat) function" + ". Third " + "argument cannot be null"); @@ -156,13 +156,13 @@ protected Object execute(Object[] data) { return targetFormat.format(userSpecifiedSourceDate); } catch (ParseException e) { String errorMsg = "Provided format " + sourceDateFormat + " does not match with the timestamp " + - sourceDate +" "+ e.getMessage(); - throw new ExecutionPlanRuntimeException(errorMsg,e); - } catch (ClassCastException e){ - String errorMsg ="Provided Data type cannot be cast to desired format. " + e.getMessage(); - throw new ExecutionPlanRuntimeException(errorMsg,e); + sourceDate + " " + e.getMessage(); + throw new ExecutionPlanRuntimeException(errorMsg, e); + } catch (ClassCastException e) { + String errorMsg = "Provided Data type cannot be cast to desired format. " + e.getMessage(); + throw new ExecutionPlanRuntimeException(errorMsg, e); } - } else if(data.length == 2){ + } else if (data.length == 2) { if (data[0] == null) { throw new ExecutionPlanRuntimeException("Invalid input given to dateFormat(timestampInMilliseconds," + @@ -190,15 +190,10 @@ protected Object execute(Object[] data) { calInstance.setTimeInMillis(dateInMills); userSpecifiedSourceDate = calInstance.getTime(); formattedNewDateValue = targetFormat.format(userSpecifiedSourceDate); - long newFormattedDateInUnix = targetFormat.parse(formattedNewDateValue).getTime(); - return String.valueOf(newFormattedDateInUnix); - } catch (ParseException e) { - String errorMsg = "Provided format " + targetDataFormat + " does not match with the timestamp " + - formattedNewDateValue +" "+ e.getMessage(); - throw new ExecutionPlanRuntimeException(errorMsg,e); - } catch (ClassCastException e){ - String errorMsg ="Provided Data type cannot be cast to desired format. " + e.getMessage(); - throw new ExecutionPlanRuntimeException(errorMsg,e); + return formattedNewDateValue; + } catch (ClassCastException e) { + String errorMsg = "Provided Data type cannot be cast to desired format. " + e.getMessage(); + throw new ExecutionPlanRuntimeException(errorMsg, e); } } else { diff --git a/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtension.java b/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtension.java index c3a4039ced..d5943ee58e 100644 --- a/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtension.java +++ b/modules/siddhi-extensions/time/src/main/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtension.java @@ -183,7 +183,7 @@ protected Object execute(Object[] data) { returnValue = cal.get(Calendar.MINUTE); } else if (unit.equals(TimeExtensionConstants.EXTENSION_TIME_UNIT_HOUR)) { - returnValue = cal.get(Calendar.HOUR); + returnValue = cal.get(Calendar.HOUR_OF_DAY); } else if (unit.equals(TimeExtensionConstants.EXTENSION_TIME_UNIT_DAY)) { returnValue = cal.get(Calendar.DAY_OF_MONTH); diff --git a/modules/siddhi-extensions/time/src/test/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtensionTestCase.java b/modules/siddhi-extensions/time/src/test/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtensionTestCase.java index 79bfcaf73d..fe2ef9fc03 100644 --- a/modules/siddhi-extensions/time/src/test/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtensionTestCase.java +++ b/modules/siddhi-extensions/time/src/test/java/org/wso2/siddhi/extension/time/ExtractAttributesFunctionExtensionTestCase.java @@ -49,8 +49,8 @@ public void extractAttributesFunctionExtension() throws InterruptedException { "define stream inputStream (symbol string,dateValue string,dateFormat string,timestampInMilliseconds long);"; String query = ("@info(name = 'query1') " + "from inputStream " + - "select symbol , time:extract('YEAR',dateValue,dateFormat) as YEAR,time:extract('HOUR',dateValue," + - "dateFormat) as MONTH,time:extract(timestampInMilliseconds,'HOUR') as QUARTER "+ + "select symbol , time:extract('YEAR',dateValue,dateFormat) as YEAR,time:extract('MONTH',dateValue," + + "dateFormat) as MONTH,time:extract(timestampInMilliseconds,'HOUR') as HOUR "+ "insert into outputStream;"); ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(inStreamDefinition + query); @@ -62,7 +62,7 @@ public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { for(int cnt=0;cnt