Arun Lal
Arun Lal

Reputation: 59

How to Convert timestamp to UTC in WSO2

I'm using WSO2 Micro Integrator and I have an API that receives a request with a Timestamp. I want to convert the Timestamp to UTC format. How can I convert a timestamp to UTC in wso2? The timestamp is like 2020-10-06T08:08:35-04:00.

Upvotes: 1

Views: 494

Answers (1)

ycr
ycr

Reputation: 14584

Option 1 : Script Mediator

The easiest way is to use a script mediator.

<property expression="//time1/text()" name="time" scope="default" type="STRING"/>
<script language="nashornJs">
<![CDATA[
  if(mc.getProperty('time')) {
     var date = new Date(mc.getProperty("time"));
     mc.setProperty("convUTC", date.toISOString());
  }
]]>
</script>

Option 2 : XSLT Mediator

You can also do the same with XSLT Mediator as well. Here is an XSLT that will work for you. First, add the XSLT as a Local Entry like below and then use it to transform the timestamp.

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="TimeToUTC" xmlns="http://ws.apache.org/ns/synapse">
    <xsl:stylesheet version="1.0" xmlns:ns4="http://nmdp.org/esb/domain/v04" xmlns:ns7="http://nmdp.org/esb/domain/v02" xmlns:ns8="http://nmdp.org/esb/domain/v01" xmlns:sfdc="sfdc" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
        <xsl:param name="TimeStamp"/>
        <xsl:template match="/">
            <payload xmlns="">
                <UTCTime>
                    <xsl:call-template name="dateTime-to-UTC">
                        <xsl:with-param name="dateTime" select="$TimeStamp"/>
                    </xsl:call-template>
                </UTCTime>
            </payload>
        </xsl:template>
        <xsl:template name="dateTime-to-UTC">
            <xsl:param name="dateTime"/>
            <xsl:choose>
                <xsl:when test="contains($dateTime, 'Z')">
                    <xsl:value-of select="$dateTime"/>
                </xsl:when>
                <xsl:otherwise>
                    <!-- extract components -->
                    <xsl:variable name="date" select="substring-before($dateTime, 'T')"/>
                    <xsl:variable name="time" select="substring-after($dateTime, 'T')"/>
                    <!-- date components -->
                    <xsl:variable name="year" select="substring($date, 1, 4)"/>
                    <xsl:variable name="month" select="substring($date, 6, 2)"/>
                    <xsl:variable name="day" select="substring($date, 9, 2)"/>
                    <!-- time components -->
                    <xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 6)"/>
                    <xsl:variable name="hour" select="substring($local-time, 1, 2)"/>
                    <xsl:variable name="minute" select="substring($local-time, 4, 2)"/>
                    <xsl:variable name="second" select="substring($local-time, 7)"/>
                    <!-- offset components -->
                    <xsl:variable name="offset" select="substring-after($time, $local-time)"/>
                    <xsl:variable name="offset-sign" select="1 - 2 * starts-with($offset, '-')"/>
                    <xsl:variable name="offset-hour" select="substring($offset, 2, 2) * $offset-sign"/>
                    <xsl:variable name="offset-minute" select="substring($offset, 5, 2) * $offset-sign"/>
                    <!-- convert to seconds -->
                    <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
                    <xsl:variable name="y" select="$year + 4800 - $a"/>
                    <xsl:variable name="m" select="$month + 12*$a - 3"/>
                    <xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045"/>
                    <xsl:variable name="total-seconds" select="86400*$jd + 3600*$hour + 60*$minute + $second - 3600*$offset-hour - 60*$offset-minute"/>
                    <!-- convert to date -->
                    <xsl:variable name="new-jd" select="floor($total-seconds div 86400)"/>
                    <xsl:variable name="new-hour" select="floor($total-seconds mod 86400 div 3600)"/>
                    <xsl:variable name="new-minute" select="floor($total-seconds mod 3600 div 60)"/>
                    <xsl:variable name="new-second" select="$total-seconds mod 60"/>
                    <xsl:variable name="f" select="$new-jd + 1401 + floor((floor((4 * $new-jd + 274277) div 146097) * 3) div 4) - 38"/>
                    <xsl:variable name="e" select="4*$f + 3"/>
                    <xsl:variable name="g" select="floor(($e mod 1461) div 4)"/>
                    <xsl:variable name="h" select="5*$g + 2"/>
                    <xsl:variable name="D" select="floor(($h mod 153) div 5 ) + 1"/>
                    <xsl:variable name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
                    <xsl:variable name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
                    <!-- output -->
                    <xsl:value-of select="$Y"/>
                    <xsl:value-of select="format-number($M, '-00')"/>
                    <xsl:value-of select="format-number($D, '-00')"/>
                    <xsl:text>T</xsl:text>
                    <xsl:value-of select="format-number($new-hour, '00')"/>
                    <xsl:value-of select="format-number($new-minute, ':00')"/>
                    <xsl:value-of select="format-number($new-second, ':00.###')"/>
                    <xsl:text>Z</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
</localEntry>

You can use the XSLT like below.

<property expression="//time1/text()" name="time" scope="default" type="STRING"/>
<xslt key="TimeToUTC">
    <property expression="$ctx:time" name="TimeStamp"/>
</xslt>
<log>
    <property expression="//UTCTime/text()" name="Converted Time with XSLT"/>
</log>

Full Combined Example

<?xml version="1.0" encoding="UTF-8"?>
<api context="/time" name="HelloWorld" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <property expression="//time1/text()" name="time" scope="default" type="STRING"/>
            <xslt key="TimeToUTC">
                <property expression="$ctx:time" name="TimeStamp"/>
            </xslt>
            <log>
                <property expression="//UTCTime/text()" name="Converted Time with XSLT"/>
            </log>
            <script language="nashornJs"><![CDATA[if(mc.getProperty('time')) {
            var date = new Date(mc.getProperty("time"));
            mc.setProperty("convUTC", date.toISOString());
            }]]></script>
            <log>
                <property expression="$ctx:convUTC" name="Converted Time with Script"/>
            </log>
            <payloadFactory media-type="xml">
                <format>
                    <LatestTime>$1</LatestTime>
                </format>
                <args>
                    <arg evaluator="xml" expression="$ctx:convUTC"/>
                </args>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Request

<request>
    <time1>2020-10-06T08:08:35-04:00</time1>
</request>

Response

<LatestTime>2020-10-06T12:08:35.000Z</LatestTime>

Upvotes: 2

Related Questions