Re: [xsl] How to get UTC displayed on XSLT

Subject: Re: [xsl] How to get UTC displayed on XSLT
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Wed, 24 Aug 2005 09:20:05 +0530
This is the corrected stylesheet. There was a bug in the 2nd xsl:when
(an erroneous + here <xsl:variable name="sec3" select="$sec1 + $sec2"
/>
).

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       xmlns:xs="http://www.w3.org/2001/XMLSchema";
                       xmlns:xyz="http://utc";
                       version="2.0">

<xsl:output method="text" />

<xsl:template match="/">
   UTC time - <xsl:value-of select="xyz:UtcTime(xs:string(current-time()))"
/>
</xsl:template>

<xsl:function name="xyz:UtcTime">
   <xsl:param name="curr-time" as="xs:string"/>

   <xsl:choose>
     <xsl:when test="contains($curr-time,'+')">
       <xsl:variable name="local-time"
select="substring-before($curr-time,'+')" />
       <xsl:variable name="time-zone"
select="substring-after($curr-time,'+')" />
       <xsl:variable name="x" select="substring-before($local-time,':')" />
       <xsl:variable name="y"
select="substring-before(substring-after($local-time,':'),':')" />
       <xsl:variable name="z"
select="substring-after(substring-after($local-time,':'),':')" />
       <xsl:variable name="sec1" select="(xs:integer($x) * 60 * 60) +
(xs:integer($y) * 60) + xs:float($z)" />
       <xsl:variable name="u" select="substring-before($time-zone,':')" />
       <xsl:variable name="v" select="substring-after($time-zone,':')" />
       <xsl:variable name="sec2" select="(xs:integer($u) * 60 * 60) +
(xs:integer($v) * 60)" />
       <xsl:variable name="sec3" select="$sec1 - $sec2" />
       <xsl:value-of select="floor(($sec3 div 60) div 60)"
/>:<xsl:value-of select="floor(($sec3 div 60) mod 60)"
/>:<xsl:value-of select="$sec3 - ((floor(($sec3 div 60) div 60) * 60 *
60) + (floor(($sec3 div 60) mod 60) * 60))" />
     </xsl:when>
     <xsl:when test="contains($curr-time,'-')">
       <xsl:variable name="local-time"
select="substring-before($curr-time,'-')" />
       <xsl:variable name="time-zone"
select="substring-after($curr-time,'-')" />
       <xsl:variable name="x" select="substring-before($local-time,':')" />
       <xsl:variable name="y"
select="substring-before(substring-after($local-time,':'),':')" />
       <xsl:variable name="z"
select="substring-after(substring-after($local-time,':'),':')" />
       <xsl:variable name="sec1" select="(xs:integer($x) * 60 * 60) +
(xs:integer($y) * 60) + xs:float($z)" />
       <xsl:variable name="u" select="substring-before($time-zone,':')" />
       <xsl:variable name="v" select="substring-after($time-zone,':')" />
       <xsl:variable name="sec2" select="(xs:integer($u) * 60 * 60) +
(xs:integer($v) * 60)" />
       <xsl:variable name="sec3" select="$sec1 - $sec2" />
       <xsl:value-of select="floor(($sec3 div 60) div 60)"
/>:<xsl:value-of select="floor(($sec3 div 60) mod 60)"
/>:<xsl:value-of select="$sec3 - ((floor(($sec3 div 60) div 60) * 60 *
60) + (floor(($sec3 div 60) mod 60) * 60))" />
     </xsl:when>
   </xsl:choose>
</xsl:function>

</xsl:stylesheet>

Regards,
Mukul


On 8/24/05, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:
> I discovered, this stylesheet has some bugs. I'll try to post the corrected
one.
>
> Regards,
> Mukul
>
> On 8/23/05, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:
> > Here is a XSLT 2.0 solution tested with Saxon 8.4

Current Thread