Skip to content

Commit aa0df96

Browse files
committed
camel-core - simple languge add millis to date command
1 parent 45c3b1e commit aa0df96

File tree

9 files changed

+46
-9
lines changed

9 files changed

+46
-9
lines changed

catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/languages/simple.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"collate(num)": { "index": 21, "kind": "function", "displayName": "Group Message Body into Sub Lists", "group": "function", "label": "function", "required": false, "javaType": "java.util.Iterator", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The collate function iterates the message body and groups the data into sub lists of specified size. This can be used with the Splitter EIP to split a message body and group\/batch the split sub message into a group of N sub lists.", "ognl": false, "suffix": "}" },
4949
"concat(exp,exp,separator)": { "index": 22, "kind": "function", "displayName": "Concat", "group": "function", "label": "function", "required": false, "javaType": "String", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Performs a string concat using two expressions (message body as default) with optional separator", "ognl": false, "suffix": "}" },
5050
"convertTo(exp,type)": { "index": 23, "kind": "function", "displayName": "Convert To", "group": "function", "label": "function", "required": false, "javaType": "", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Converts the message body (or expression) to the specified type.", "ognl": true, "suffix": "}" },
51-
"date(command)": { "index": 24, "kind": "function", "displayName": "Parse Date", "group": "function", "label": "function", "required": false, "javaType": "java.util.Date", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Evaluates to a java.util.Date object. Supported commands are: `now` for current timestamp, `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long\/Date object in the header with the key xxx. `variable.xxx` to use the Long\/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long\/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.", "ognl": false, "suffix": "}" },
51+
"date(command)": { "index": 24, "kind": "function", "displayName": "Parse Date", "group": "function", "label": "function", "required": false, "javaType": "java.util.Date", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Evaluates to a java.util.Date object. Supported commands are: `now` for current timestamp, `millis` for current timestamp in millis (unix epoch), `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long\/Date object in the header with the key xxx. `variable.xxx` to use the Long\/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long\/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.", "ognl": false, "suffix": "}" },
5252
"date-with-timezone(command:timezone:pattern)": { "index": 25, "kind": "function", "displayName": "Date Formatter", "group": "function", "label": "function", "required": false, "javaType": "String", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Formats the date to a String using the given date pattern, and with support for timezone. Supported commands are: `now` for current timestamp, `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long\/Date object in the header with the key xxx. `variable.xxx` to use the Long\/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long\/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.", "ognl": false, "suffix": "}" },
5353
"distinct(val...)": { "index": 26, "kind": "function", "displayName": "Distinct Values", "group": "function", "label": "function", "required": false, "javaType": "Set", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Returns a set of all the values with duplicates removed", "ognl": false, "suffix": "}" },
5454
"empty(type)": { "index": 27, "kind": "function", "displayName": "Create Empty Object", "group": "function", "label": "function", "required": false, "javaType": "Object", "prefix": "${", "deprecated": true, "deprecationNote": "", "autowired": false, "secret": false, "description": "Creates a new empty object (decided by type). Use `string` to create an empty String. Use `list` to create an empty `java.util.ArrayList`. Use `map` to create an empty `java.util.LinkedHashMap`. Use `set` to create an empty `java.util.LinkedHashSet`.", "ognl": false, "suffix": "}" },

components/camel-csimple-joor/src/test/java/org/apache/camel/language/csimple/joor/OriginalSimpleTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,23 @@ public void testDateWithTimezone() {
721721

722722
@Test
723723
public void testDateNow() {
724-
Object out = evaluateExpression("${date:now:hh:mm:ss a}", null);
724+
Object out = evaluateExpression("${date:now}", null);
725725
assertNotNull(out);
726+
assertIsInstanceOf(Date.class, out);
727+
728+
out = evaluateExpression("${date:now:hh:mm:ss a}", null);
729+
assertNotNull(out);
730+
out = evaluateExpression("${date:now:hh:mm:ss}", null);
731+
assertNotNull(out);
732+
out = evaluateExpression("${date:now-2h:hh:mm:ss}", null);
733+
assertNotNull(out);
734+
}
735+
736+
@Test
737+
public void testDateMillis() {
738+
Object out = evaluateExpression("${date:millis}", null);
739+
assertNotNull(out);
740+
assertIsInstanceOf(Long.class, out);
726741
}
727742

728743
@Test

core/camel-core-languages/src/generated/resources/META-INF/org/apache/camel/language/simple/simple.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"collate(num)": { "index": 21, "kind": "function", "displayName": "Group Message Body into Sub Lists", "group": "function", "label": "function", "required": false, "javaType": "java.util.Iterator", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The collate function iterates the message body and groups the data into sub lists of specified size. This can be used with the Splitter EIP to split a message body and group\/batch the split sub message into a group of N sub lists.", "ognl": false, "suffix": "}" },
4949
"concat(exp,exp,separator)": { "index": 22, "kind": "function", "displayName": "Concat", "group": "function", "label": "function", "required": false, "javaType": "String", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Performs a string concat using two expressions (message body as default) with optional separator", "ognl": false, "suffix": "}" },
5050
"convertTo(exp,type)": { "index": 23, "kind": "function", "displayName": "Convert To", "group": "function", "label": "function", "required": false, "javaType": "", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Converts the message body (or expression) to the specified type.", "ognl": true, "suffix": "}" },
51-
"date(command)": { "index": 24, "kind": "function", "displayName": "Parse Date", "group": "function", "label": "function", "required": false, "javaType": "java.util.Date", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Evaluates to a java.util.Date object. Supported commands are: `now` for current timestamp, `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long\/Date object in the header with the key xxx. `variable.xxx` to use the Long\/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long\/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.", "ognl": false, "suffix": "}" },
51+
"date(command)": { "index": 24, "kind": "function", "displayName": "Parse Date", "group": "function", "label": "function", "required": false, "javaType": "java.util.Date", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Evaluates to a java.util.Date object. Supported commands are: `now` for current timestamp, `millis` for current timestamp in millis (unix epoch), `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long\/Date object in the header with the key xxx. `variable.xxx` to use the Long\/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long\/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.", "ognl": false, "suffix": "}" },
5252
"date-with-timezone(command:timezone:pattern)": { "index": 25, "kind": "function", "displayName": "Date Formatter", "group": "function", "label": "function", "required": false, "javaType": "String", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Formats the date to a String using the given date pattern, and with support for timezone. Supported commands are: `now` for current timestamp, `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long\/Date object in the header with the key xxx. `variable.xxx` to use the Long\/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long\/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.", "ognl": false, "suffix": "}" },
5353
"distinct(val...)": { "index": 26, "kind": "function", "displayName": "Distinct Values", "group": "function", "label": "function", "required": false, "javaType": "Set", "prefix": "${", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Returns a set of all the values with duplicates removed", "ognl": false, "suffix": "}" },
5454
"empty(type)": { "index": 27, "kind": "function", "displayName": "Create Empty Object", "group": "function", "label": "function", "required": false, "javaType": "Object", "prefix": "${", "deprecated": true, "deprecationNote": "", "autowired": false, "secret": false, "description": "Creates a new empty object (decided by type). Use `string` to create an empty String. Use `list` to create an empty `java.util.ArrayList`. Use `map` to create an empty `java.util.LinkedHashMap`. Use `set` to create an empty `java.util.LinkedHashSet`.", "ognl": false, "suffix": "}" },

core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ And the `not` function is function to inverse the boolean value.
324324
[width="100%",cols="10%,10%,80%",options="header",]
325325
|====
326326
|Function |Response Type |Description
327+
|`date:millis` | `long` | Returns the current timestamp as millis in unix epoch.
327328
|`date:command` | `Date` | Evaluates to a `java.util.Date` object. Supported commands are: `now` for current timestamp, `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the `Long/Date` object in the header with the key xxx. `variable.xxx` to use the `Long/Date` in the variable with the key xxx. `exchangeProperty.xxx` to use the `Long/Date` object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (only available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or `now+1h30m-100`.
328329
|`date:command:pattern` | `String` | Date formatting using `java.text.SimpleDateFormat` patterns. See `data:command` function for additional documentation on the commands.
329330
|`date-with-timezone:command:timezone:pattern` | `String` | Date formatting using `java.text.SimpleDateFormat` timezones and patterns. See `data:command` function for additional documentation on the commands.
@@ -332,6 +333,8 @@ And the `not` function is function to inverse the boolean value.
332333
The date functions is used for parsing and formatting with date and times.
333334

334335
For example to get the current time use `${date:now}` which is returned as a `java.util.Date` object.
336+
And you can use `${date:millis}` to get unix epoch timestamp as a long value.
337+
335338
If you want to format this to a String value, you can use the pattern command, such as `${date:now:hh:mm:ss}`.
336339
And to get the time 8 hours in the future `${date:now+8h:hh:mm:ss}`.
337340

@@ -386,7 +389,7 @@ This will get the `Date` object from the header with key birthday, and format th
386389
|`capitalize()` | `String` | Capitalizes the message body as a String value (upper case every words)
387390
|`capitalize(exp)` | `String` | Capitalizes the expression as a String value (upper case every words)
388391
|`concat(exp,exp,separator)` | `String` | Performs a string concat using two expressions (message body as default) with optional separator (uses comma by default).
389-
|`join(separator,prefix,exp` | `String` | The join function iterates the message body (by default) and joins the data into a `String`. The separator is by default a comma. The prefix is optional. The join uses the message body as source by default. It is possible to refer to another source (simple language) such as a header via the exp parameter. For example `join('&','id=','$\{header.ids}')`
392+
|`join(separator,prefix,exp)` | `String` | The join function iterates the message body (by default) and joins the data into a `String`. The separator is by default a comma. The prefix is optional. The join uses the message body as source by default. It is possible to refer to another source (simple language) such as a header via the exp parameter. For example `join('&','id=','$\{header.ids}')`
390393
|`lowercase()` | `String` | Lowercases the message body
391394
|`lowercase(exp)` | `String` | Lowercases the expression
392395
|`normalizeWhitespace()` | `String` | Normalizes the whitespace in the message body by cleaning up excess whitespaces.

core/camel-core-languages/src/main/java/org/apache/camel/language/csimple/CSimpleHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ private static Date evalDate(Exchange exchange, String command) {
406406
date = LanguageHelper.dateFromExchangeCreated(exchange);
407407
} else if (command.startsWith("header.")) {
408408
date = LanguageHelper.dateFromHeader(exchange, command, (e, o) -> failDueToMissingObjectAtCommand(command));
409+
} else if (command.startsWith("variable.")) {
410+
date = LanguageHelper.dateFromVariable(exchange, command, (e, o) -> failDueToMissingObjectAtCommand(command));
409411
} else if (command.startsWith("exchangeProperty.")) {
410412
date = LanguageHelper.dateFromExchangeProperty(exchange, command,
411413
(e, o) -> failDueToMissingObjectAtCommand(command));

core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public final class SimpleConstants {
107107
displayName = "Convert To")
108108
public static final String CONVERT_TO = "convertTo(exp,type)";
109109

110-
@Metadata(description = "Evaluates to a java.util.Date object. Supported commands are: `now` for current timestamp, `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long/Date object in the header with the key xxx. `variable.xxx` to use the Long/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.",
110+
@Metadata(description = "Evaluates to a java.util.Date object. Supported commands are: `now` for current timestamp, `millis` for current timestamp in millis (unix epoch), `exchangeCreated` for the timestamp when the current exchange was created, `header.xxx` to use the Long/Date object in the header with the key xxx. `variable.xxx` to use the Long/Date in the variable with the key xxx. `exchangeProperty.xxx` to use the Long/Date object in the exchange property with the key xxx. `file` for the last modified timestamp of the file (available with a File consumer). Command accepts offsets such as: `now-24h` or `header.xxx+1h` or even `now+1h30m-100`.",
111111
label = "function", javaType = "java.util.Date", displayName = "Parse Date")
112112
public static final String DATE = "date(command)";
113113

core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,8 +2239,11 @@ public static Expression dateExpression(final String commandWithOffsets, final S
22392239
@Override
22402240
public Object evaluate(Exchange exchange) {
22412241
Date date = evalDate(exchange, command);
2242-
2243-
return LanguageHelper.applyDateOffsets(date, offsets, pattern, timezone);
2242+
Object answer = LanguageHelper.applyDateOffsets(date, offsets, pattern, timezone);
2243+
if ("millis".equals(command) && answer instanceof Date d) {
2244+
answer = d.getTime();
2245+
}
2246+
return answer;
22442247
}
22452248

22462249
@Override
@@ -2258,7 +2261,7 @@ public String toString() {
22582261

22592262
private static Date evalDate(Exchange exchange, String command) {
22602263
Date date;
2261-
if ("now".equals(command)) {
2264+
if ("now".equals(command) || "millis".equals(command)) {
22622265
date = new Date();
22632266
} else if ("exchangeCreated".equals(command)) {
22642267
date = LanguageHelper.dateFromExchangeCreated(exchange);

core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,9 @@ private String doCreateCode(CamelContext camelContext, String expression) throws
16271627
return createCodeFileExpression(remainder);
16281628
}
16291629

1630+
if ("date:millis".equals(function)) {
1631+
return "System.currentTimeMillis()";
1632+
}
16301633
// date: prefix
16311634
remainder = ifStartsWithReturnRemainder("date:", function);
16321635
if (remainder != null) {

core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,25 @@ public void testDateWithTimezone() {
663663

664664
@Test
665665
public void testDateNow() {
666-
Object out = evaluateExpression("${date:now:hh:mm:ss a}", null);
666+
Object out = evaluateExpression("${date:now}", null);
667+
assertNotNull(out);
668+
assertIsInstanceOf(Date.class, out);
669+
670+
out = evaluateExpression("${date:now:hh:mm:ss a}", null);
667671
assertNotNull(out);
668672
out = evaluateExpression("${date:now:hh:mm:ss}", null);
669673
assertNotNull(out);
670674
out = evaluateExpression("${date:now-2h:hh:mm:ss}", null);
671675
assertNotNull(out);
672676
}
673677

678+
@Test
679+
public void testDateMillis() {
680+
Object out = evaluateExpression("${date:millis}", null);
681+
assertNotNull(out);
682+
assertIsInstanceOf(Long.class, out);
683+
}
684+
674685
@Test
675686
public void testDateExchangeCreated() {
676687
Object out

0 commit comments

Comments
 (0)