Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert 24 hour format time in to 12 hour Format as a Property in the WSO2 ESB

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

like image 993
007 Avatar asked Nov 17 '25 21:11

007


1 Answers

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,':')) &lt;= 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>
like image 89
tmoasz Avatar answered Nov 19 '25 14:11

tmoasz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!