-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions DT_DATE_NOW and DT_DATE_TODAY (#453)
* Adds functions DT_NOW and DT_TODAY
- Loading branch information
1 parent
0e243b8
commit ac18137
Showing
12 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/ezylang/evalex/functions/datetime/DateTimeNowFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2012-2024 Udo Klimaschewski | ||
Licensed 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 com.ezylang.evalex.functions.datetime; | ||
|
||
import com.ezylang.evalex.Expression; | ||
import com.ezylang.evalex.data.EvaluationValue; | ||
import com.ezylang.evalex.functions.AbstractFunction; | ||
import com.ezylang.evalex.parser.Token; | ||
import java.time.Instant; | ||
|
||
/** | ||
* Produces a new DATE_TIME that represents the current date and time. | ||
* | ||
* <p>It is useful to calculate a value based on the current date and time. For example, if you know | ||
* the start DATE_TIME of a running process, you might use the following expression to find the | ||
* DURATION that represents the process age: | ||
* | ||
* <blockquote> | ||
* | ||
* {@code DT_DATE_NOW() - startDateTime} | ||
* | ||
* </blockquote> | ||
* | ||
* @author oswaldobapvicjr | ||
*/ | ||
public class DateTimeNowFunction extends AbstractFunction { | ||
@Override | ||
public EvaluationValue evaluate( | ||
Expression expression, Token functionToken, EvaluationValue... parameterValues) { | ||
return expression.convertValue(Instant.now()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/ezylang/evalex/functions/datetime/DateTimeTodayFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2012-2024 Udo Klimaschewski | ||
Licensed 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 com.ezylang.evalex.functions.datetime; | ||
|
||
import com.ezylang.evalex.EvaluationException; | ||
import com.ezylang.evalex.Expression; | ||
import com.ezylang.evalex.config.ExpressionConfiguration; | ||
import com.ezylang.evalex.data.EvaluationValue; | ||
import com.ezylang.evalex.functions.AbstractFunction; | ||
import com.ezylang.evalex.functions.FunctionParameter; | ||
import com.ezylang.evalex.parser.Token; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
|
||
/** | ||
* Produces a new DATE_TIME that represents the current date, at midnight (00:00). | ||
* | ||
* <p>It is useful for DATE_TIME comparison, when the current time must not be considered. For | ||
* example, in the expression: | ||
* | ||
* <blockquote> | ||
* | ||
* {@code IF(expiryDate > DT_DATE_TODAY(), "expired", "valid")} | ||
* | ||
* </blockquote> | ||
* | ||
* <p>This function may accept an optional time zone to be applied. If no zone ID is specified, the | ||
* default zone ID defined at the {@link ExpressionConfiguration} will be used. | ||
* | ||
* @author oswaldobapvicjr | ||
*/ | ||
@FunctionParameter(name = "parameters", isVarArg = true) | ||
public class DateTimeTodayFunction extends AbstractFunction { | ||
@Override | ||
public EvaluationValue evaluate( | ||
Expression expression, Token functionToken, EvaluationValue... parameterValues) | ||
throws EvaluationException { | ||
ZoneId zoneId = parseZoneId(expression, functionToken, parameterValues); | ||
Instant today = LocalDate.now().atStartOfDay(zoneId).toInstant(); | ||
return expression.convertValue(today); | ||
} | ||
|
||
private ZoneId parseZoneId( | ||
Expression expression, Token functionToken, EvaluationValue... parameterValues) | ||
throws EvaluationException { | ||
if (parameterValues.length > 0 && !parameterValues[0].isNullValue()) { | ||
return ZoneIdConverter.convert(functionToken, parameterValues[0].getStringValue()); | ||
} | ||
return expression.getConfiguration().getZoneId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters