Im trying to convert 24 hour to 12 hour in wso2 4.9.0 payload. Is there any way to convert it using payload.
Original Time is 22:45:16
Need to convert to 10:45:16 PM
Im trying to convert 24 hour to 12 hour in wso2 4.9.0 payload. Is there any way to convert it using payload.
Original Time is 22:45:16
Need to convert to 10:45:16 PM
I don't know why you have to do that only using payloadFactory or under payloadfactory. There you can use only xpath to 'convert', at as you see below, it is very nasty and not perfect. If i remember in wso2esb 4.9.0, there already is ScriptMediator, which will be more better for that. I tested that on wso2ei 6.
<sequence name="time" xmlns="http://ws.apache.org/ns/synapse">
<property expression="//time/text()" name="time" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<script language="js"><![CDATA[
var timeIn = mc.getProperty('time');
var displayFormat = new java.text.SimpleDateFormat("hh:mm:ss a");
var parseFormat = new java.text.SimpleDateFormat("HH:mm:ss");
mc.setProperty('scriptTime', displayFormat.format(parseFormat.parse(timeIn)));
]]></script>
<payloadFactory media-type="json">
<format>{"inputTime":"$1", "scriptTime":"$2", "xpathTime":"$3"}</format>
<args>
<arg evaluator="xml" expression="$ctx:time" literal="false" xmlns:ns="http://org.apache.synapse/xsd"/>
<arg evaluator="xml" expression="$ctx:scriptTime" literal="false" xmlns:ns="http://org.apache.synapse/xsd"/>
<arg evaluator="xml"
expression="concat(concat(substring(number(substring-before($ctx:time,':'))+12, 1 div (number(substring-before($ctx:time,':')) = 0)),substring(number(substring-before($ctx:time,':'))-12, 1 div (number(substring-before($ctx:time,':')) > 12)),substring(number(substring-before($ctx:time,':')), 1 div (number(substring-before($ctx:time,':')) <= 12 and number(substring-before($ctx:time,':')) > 0 )),),':',substring-after($ctx:time,':') , concat(substring(' PM', 1, number(number(substring-before($ctx:time,':')) > 11) * string-length(' PM')),substring(' AM', 1, number(not(number(substring-before($ctx:time,':')) > 11)) * string-length(' AM'))))"
literal="false" xmlns:ns="http://org.apache.synapse/xsd"/>
</args>
</payloadFactory>
<log level="custom">
<property expression="$ctx:time" name="time" xmlns:ns="http://org.apache.synapse/xsd"/>
<property expression="$ctx:scriptTime" name="scriptTime" xmlns:ns="http://org.apache.synapse/xsd"/>
<property expression="//xpathTime" name="xpathTime" xmlns:ns="http://org.apache.synapse/xsd"/>
</log>
<respond/>
</sequence>
You can use something like this:
<payloadFactory media-type="json">
<format>
{"time":"$1"}
</format>
<args>
<arg evaluator="xml" expression="get-property('SYSTEM_DATE', 'hh:mm a')"/>
</args>
</payloadFactory>