-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- introduces a configuration for the date-time zone id
- refactors EvaluationValue conversion to use a configurable converter - deprecates evaluation value constructor not using a configuration - deprecates EvaluationValue constructor using double value and MathContext
- Loading branch information
1 parent
c25dc23
commit 71d1be9
Showing
68 changed files
with
1,558 additions
and
232 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
layout: default | ||
title: Working with Date-Times and Duration | ||
parent: Concepts | ||
nav_order: 4 | ||
--- | ||
|
||
## Working with Date-Times and Duration | ||
|
||
Since version 3.1.0 of EvalEx, there are two additional data types _DATE_TIME_ and _DURATION_. | ||
|
||
_DATE_TIME_ values are stored internally as _java.time.Instant_ values, _DURATION_ values are stored as | ||
_java.time.Duration_ values. | ||
|
||
A _DATE_TIME_ instant is instantaneous point on the time-line, it holds no information about the time zone. | ||
Time zones come into play, when converting local dates-times to instants and vice versa. | ||
The same instant can have different local date-time values, depending on the destination time zone. | ||
The precision of a _DATE_TIME_ is up to nanoseconds. | ||
|
||
A _DURATION_ is a certain amount of time, like e.g. "3 hours, 15 minutes and 6 seconds". | ||
The smallest amount of a duration is 1 nanosecond. | ||
|
||
### Arithmetic operations with _DATE_TIME_ and _DURATION_. | ||
|
||
The infix plus and minus operators can be used to do calculations with _DATE_TIME_ and _DURATION_ values. | ||
The outcome of the operation depends on the operator types: | ||
|
||
#### Addition | ||
| Left Operand | Right Operand | Result | | ||
|--------------|---------------|------------------------------------------------------------------------| | ||
| _DATE_TIME_ | _DURATION_ | A new _DATE_TIME_ where the duration is added to the date. | | ||
| _DURATION_ | _DURATION_ | A new duration, which is the sum of both durations. | | ||
| _DATE_TIME_ | _NUMBER_ | A new _DATE_TIME_ with the amount of a duration in milliseconds added. | | ||
|
||
All other combinations of _DATE_TIME_ and _DURATION_ with other types will do a string concatenation. | ||
|
||
Example. Adding a duration to a date-time: | ||
```java | ||
Instant start = Instant.parse("2023-12-03T23:15:30.00Z"); | ||
Duration duration = Duration.ofHours(3); | ||
|
||
Expression expression = new Expression("start + duration"); | ||
EvaluationValue result = | ||
expression | ||
.with("start", start) | ||
.and("duration", duration) | ||
.evaluate(); | ||
System.out.println(result); // will print "EvaluationValue(value=2023-12-04T02:15:30Z, dataType=DATE_TIME)" | ||
``` | ||
|
||
#### Subtraction | ||
| Left Operand | Right Operand | Result | | ||
|--------------|---------------|-----------------------------------------------------------------------------| | ||
| _DATE_TIME_ | _DATE_TIME_ | A duration which reflects the difference between the two date-times. | | ||
| _DATE_TIME_ | _DURATION_ | A new _DATE_TIME_ where the duration is subtracted from the date. | | ||
| _DURATION_ | _DURATION_ | A new duration, which is the difference of both durations. | | ||
| _DATE_TIME_ | _NUMBER_ | A new _DATE_TIME_ with the amount of a duration in milliseconds subtracted. | | ||
|
||
All other combinations of _DATE_TIME_ and _DURATION_ with other types will throw an _EvaluationException_. | ||
|
||
Example. Find out the duration between two date-times: | ||
```java | ||
Instant start = Instant.parse("2023-12-05T11:20:00.00Z"); | ||
Instant end = Instant.parse("2023-12-04T23:15:30.00Z"); | ||
|
||
Expression expression = new Expression("start - end"); | ||
EvaluationValue result = expression | ||
.with("start", start) | ||
.and("end", end) | ||
.evaluate(); | ||
System.out.println(result); // will print "EvaluationValue(value=PT12H4M30S, dataType=DURATION)" | ||
``` | ||
|
||
The string representation of a duration is here in SO format, meaning 12 hours, 4 minutes and 30 seconds. | ||
|
||
### Passing other Date-Time Types as variables | ||
|
||
Instead of passing _java.time.Instant_ values for _DATE_TIME_ values, you can pass also the following Java data types. | ||
They will be converted automatically to _java.time.Instant_ values. | ||
|
||
| Input type | Conversion note | | ||
|----------------|---------------------------------------------------------------------------------------------------------------------------| | ||
| ZonedDateTime | Directly converted. | | ||
| OffsetDateTime | Directly converted. | | ||
| LocalDate | Converted using the configured time zone. Defaults to the systems time zone.<br/>The time is set to beginning of the day. | | ||
| LocalDateTime | Converted using the configured time zone. Defaults to the systems time zone. | | ||
| Date | Directly converted. | | ||
| Calendar | Directly converted. | | ||
|
||
### New Date-Time Functions | ||
|
||
In addition to the possibility to add and subtract with _DATE_TIME_ and _DURATION_ values, there are also several new | ||
functions to work with date-times. Most of them allow to create, parse and format date-time and duration values. | ||
|
||
See Chapter [Date Time Functions](../references/functions.html#date-time-Functions) | ||
|
||
### Configuration Changes |
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
84 changes: 84 additions & 0 deletions
84
src/main/java/com/ezylang/evalex/data/DefaultEvaluationValueConverter.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,84 @@ | ||
/* | ||
Copyright 2012-2023 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.data; | ||
|
||
import com.ezylang.evalex.config.ExpressionConfiguration; | ||
import com.ezylang.evalex.data.conversion.*; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* The default implementation of the {@link EvaluationValueConverterIfc}, used in the standard | ||
* configuration. | ||
* | ||
* <table> | ||
* <tr> | ||
* <th>Input type</th><th>Converter used</th> | ||
* </tr> | ||
* <tr><td>BigDecimal</td><td>NumberConverter</td></tr> | ||
* <tr><td>Long, long</td><td>NumberConverter</td></tr> | ||
* <tr><td>Integer, int</td><td>NumberConverter</td></tr> | ||
* <tr><td>Short, short</td><td>NumberConverter</td></tr> | ||
* <tr><td>Byte, byte</td><td>NumberConverter</td></tr> | ||
* <tr><td>Double, double</td><td>NumberConverter *</td></tr> | ||
* <tr><td>Float, float</td><td>NumberConverter *</td></tr> | ||
* <tr><td>CharSequence , String</td><td>StringConverter</td></tr> | ||
* <tr><td>Boolean, boolean</td><td>BooleanConverter</td></tr> | ||
* <tr><td>Instant</td><td>DateTimeConverter</td></tr> | ||
* <tr><td>Date</td><td>DateTimeConverter</td></tr> | ||
* <tr><td>Calendar</td><td>DateTimeConverter</td></tr> | ||
* <tr><td>ZonedDateTime</td><td>DateTimeConverter</td></tr> | ||
* <tr><td>LocalDate</td><td>DateTimeConverter - the configured zone id will be used for conversion</td></tr> | ||
* <tr><td>LocalDateTime</td><td>DateTimeConverter - the configured zone id will be used for conversion</td></tr> | ||
* <tr><td>OffsetDateTime</td><td>DateTimeConverter</td></tr> | ||
* <tr><td>Duration</td><td>DurationConverter</td></tr> | ||
* <tr><td>ASTNode</td><td>ASTNode</td></tr> | ||
* <tr><td>List<?></td><td>ArrayConverter - each entry will be converted</td></tr> | ||
* <tr><td>Map<?,?></td><td>StructureConverter - each entry will be converted.</td></tr> | ||
* </table> | ||
* | ||
* <i>* Be careful with conversion problems when using float or double, which are fractional | ||
* numbers. A (float)0.1 is e.g. converted to 0.10000000149011612</i> | ||
*/ | ||
public class DefaultEvaluationValueConverter implements EvaluationValueConverterIfc { | ||
|
||
static List<ConverterIfc> converters = | ||
Arrays.asList( | ||
new NumberConverter(), | ||
new StringConverter(), | ||
new BooleanConverter(), | ||
new DateTimeConverter(), | ||
new DurationConverter(), | ||
new ExpressionNodeConverter(), | ||
new ArrayConverter(), | ||
new StructureConverter()); | ||
|
||
public EvaluationValue convertObject(Object object, ExpressionConfiguration configuration) { | ||
|
||
if (object == null) { | ||
return EvaluationValue.nullValue(); | ||
} | ||
|
||
for (ConverterIfc converter : converters) { | ||
if (converter.canConvert(object)) { | ||
return converter.convert(object, configuration); | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
"Unsupported data type '" + object.getClass().getName() + "'"); | ||
} | ||
} |
Oops, something went wrong.