Friday, November 16, 2012

Convert date times based on TimeZone in XSL

Recently I had a requirement where I had to convert time to different timezones based on the clients different locations. One issue we had was with the table column length which was limited to 20 chars.So we had to format the time to remove the offset as it full timestamp usually has a lenght close to 30 chars.Since we were not able to insert the time directly, we had to come up with a workaround.

I am pasting the XSL code snippet  which can be used to modify the timestamp based on the offset which defines the timezone time difference.

<xsl:variable name="TIMEZONE_DIFF"
                        select="xp20:timezone-from-dateTime(startTime)"/>
          <xsl:variable name="DIFF_SIGN"
                        select="substring($TIMEZONE_DIFF,1.0,1.0)"/>
          <xsl:variable name="DIFF_HOUR"
                        select="substring($TIMEZONE_DIFF,2.0,2.0)"/>
          <xsl:variable name="DIFF_MIN"
                        select="substring($TIMEZONE_DIFF,5.0,2.0)"/>
          <xsl:variable name="DURATION"
                        select='concat("PT",$DIFF_HOUR,"H",$DIFF_MIN,"M")'/>


              <xsl:choose>
                  <xsl:when test="$DIFF_SIGN='+'">                
                    <ns0:StartTime>
                      <xsl:value-of select='xp20:format-dateTime(xp20:add-dayTimeDuration-to-dateTime(startTime,$DURATION),"[M01]-[D01]-[Y0001] [H01]:[m01]:[s01]")'/>
                    </ns0: StartTime >
                  </xsl:when>
                  <xsl:otherwise>
                    <ns0: StartTime >
                      <xsl:value-of select='xp20:format-dateTime(xp20:subtract-dayTimeDuration-from-dateTime(startTime,$DURATION),"[M01]-[D01]-[Y0001] [H01]:[m01]:[s01]")'/>
                    </ns0: StartTime >
                  </xsl:otherwise>
                </xsl:choose>
Hope this helps

No comments: