Skip to content

Commit ea97f01

Browse files
Create FromMillisZonedFunction.java
Signed-off-by: Marcio Andrada <[email protected]>
1 parent d12aaa9 commit ea97f01

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* (c) Copyright 2018, 2019 IBM Corporation
3+
* 1 New Orchard Road,
4+
* Armonk, New York, 10504-1722
5+
* United States
6+
* +1 914 499 1900
7+
* support: Nathaniel Mills [email protected]
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*/
22+
23+
package com.api.jsonata4java.expressions.functions;
24+
25+
import com.api.jsonata4java.expressions.EvaluateRuntimeException;
26+
import com.api.jsonata4java.expressions.ExpressionsVisitor;
27+
import com.api.jsonata4java.expressions.generated.MappingExpressionParser.Function_callContext;
28+
import com.api.jsonata4java.expressions.utils.Constants;
29+
import com.api.jsonata4java.expressions.utils.DateTimeUtils;
30+
import com.api.jsonata4java.expressions.utils.FunctionUtils;
31+
import com.fasterxml.jackson.databind.JsonNode;
32+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
33+
import com.fasterxml.jackson.databind.node.TextNode;
34+
35+
/**
36+
* From http://docs.jsonata.org/string-functions.html:
37+
*
38+
* $fromMillis(number)
39+
*
40+
* Convert a number representing milliseconds since the Unix Epoch (1 January,
41+
* 1970 UTC) to a timestamp string in the ISO 8601 format.
42+
*
43+
* Examples
44+
*
45+
* $fromMillis(1510067557121)=="2017-11-07T15:12:37.121Z"
46+
*
47+
*/
48+
public class FromMillisZonedFunction extends FunctionBase {
49+
50+
public static String ERR_BAD_CONTEXT = String.format(Constants.ERR_MSG_BAD_CONTEXT, Constants.FUNCTION_FROM_MILLIS);
51+
public static String ERR_ARG1BADTYPE = String.format(Constants.ERR_MSG_ARG1_BAD_TYPE,
52+
Constants.FUNCTION_FROM_MILLIS);
53+
public static String ERR_ARG2BADTYPE = String.format(Constants.ERR_MSG_ARG2_BAD_TYPE,
54+
Constants.FUNCTION_FROM_MILLIS);
55+
56+
public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContext ctx) {
57+
// Create the variable to return
58+
JsonNode result = null;
59+
60+
// Retrieve the number of arguments
61+
JsonNode argNumber = JsonNodeFactory.instance.nullNode();
62+
boolean useContext = FunctionUtils.useContextVariable(this, ctx, getSignature());
63+
int argCount = getArgumentCount(ctx);
64+
if (useContext) {
65+
argNumber = FunctionUtils.getContextVariable(expressionVisitor);
66+
if (argNumber != null && argNumber.isNull() == false) {
67+
argCount++;
68+
} else {
69+
useContext = false;
70+
}
71+
}
72+
73+
// Make sure that we have the right number of arguments
74+
if (argCount >= 1 && argCount <= 3) {
75+
if (!useContext) {
76+
argNumber = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, 0);
77+
}
78+
JsonNode picture = JsonNodeFactory.instance.nullNode();
79+
if (argCount >= 2) {
80+
picture = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, useContext ? 0 : 1);
81+
}
82+
JsonNode timezone = JsonNodeFactory.instance.nullNode();
83+
if (argCount == 3) {
84+
timezone = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, useContext ? 1 : 2);
85+
}
86+
if (argNumber == null) {
87+
return null;
88+
}
89+
if (argNumber.isNumber()) {
90+
final Long millis = argNumber.asLong();
91+
String pictureStr = null;
92+
if (picture != null && picture.isNull() == false) {
93+
pictureStr = picture.asText();
94+
}
95+
String timezoneStr = null;
96+
if (timezone != null && timezone.isNull() == false) {
97+
timezoneStr = timezone.asText();
98+
}
99+
result = new TextNode(DateTimeUtils.formatDateTimeFromZoneId(millis, pictureStr, timezoneStr));
100+
} else {
101+
throw new EvaluateRuntimeException(ERR_ARG1BADTYPE);
102+
}
103+
} else {
104+
throw new EvaluateRuntimeException(ERR_BAD_CONTEXT);
105+
}
106+
107+
return result;
108+
}
109+
110+
@Override
111+
public int getMaxArgs() {
112+
return 3;
113+
}
114+
115+
@Override
116+
public int getMinArgs() {
117+
return 0; // account for context variable
118+
}
119+
120+
@Override
121+
public String getSignature() {
122+
// accepts a number (or context variable), an optional string, another optional
123+
// string, returns a number
124+
return "<n-s?s?:s>";
125+
}
126+
}

0 commit comments

Comments
 (0)