EXSLT - date:duration - XSLT Template Implementation

Supported Version: 1
XML Definition: date.duration.xml
Source: date.duration.template.xsl

This implementation relies on date:seconds() being available in order to get the current date/time. If it isn't, it gives a zero duration (for now).

Source

<xsl:import href="../add/date.add.template.xsl" />
<xsl:template match="/">
   <xsl:message terminate="yes">
      <xsl:call-template name="date:add">
         <xsl:with-param name="date-time"
                         select="'1900-01-01'" />
         <xsl:with-param name="duration">
            <xsl:call-template name="date:duration">
               <xsl:with-param name="seconds"
                               select="(37257.041667 - 2) * 60 * 60 * 24" />
            </xsl:call-template>
         </xsl:with-param>
      </xsl:call-template>
   </xsl:message>
</xsl:template>
<xsl:template name="date:duration">
   <xsl:param name="seconds">
      <xsl:choose>
         <xsl:when test="function-available('date:seconds')">
            <xsl:value-of select="date:seconds()" />
         </xsl:when>
         <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
   </xsl:param>
   <xsl:variable name="duration">
      <xsl:variable name="day-s"
                    select="60 * 60 * 24" />
      <xsl:variable name="hour-s"
                    select="60 * 60" />
      <xsl:variable name="min-s"
                    select="60" />
      <xsl:if test="$seconds &lt; 0">-</xsl:if>
      <xsl:text>P</xsl:text>
      <xsl:variable name="s"
                    select="$seconds * ((($seconds >= 0) * 2) - 1)" />
      <xsl:variable name="days"
                    select="floor($s div $day-s)" />
      <xsl:variable name="hours"
                    select="floor(($s - ($days * $day-s)) div $hour-s)" />
      <xsl:variable name="mins"
                    select="floor(($s - ($days * $day-s) - ($hours * $hour-s)) div $min-s)" />
      <xsl:variable name="secs"
                    select="$s - ($days * $day-s) - ($hours * $hour-s) - ($mins * $min-s)" />
      <xsl:if test="$days">
         <xsl:value-of select="$days" />
         <xsl:text>D</xsl:text>
      </xsl:if>
      <xsl:if test="$hours or $mins or $secs">T</xsl:if>
      <xsl:if test="$hours">
         <xsl:value-of select="$hours" />
         <xsl:text>H</xsl:text>
      </xsl:if>
      <xsl:if test="$mins">
         <xsl:value-of select="$mins" />
         <xsl:text>M</xsl:text>
      </xsl:if>
      <xsl:if test="$secs">
         <xsl:value-of select="$secs" />
         <xsl:text>S</xsl:text>
      </xsl:if>
   </xsl:variable>
   <xsl:value-of select="$duration" />
</xsl:template>

Change History

Submitted: 2001-05-12
Creator: Jeni Tennison(http://www.jenitennison.com/)

Returns the result of converting the argument number of seconds to a duration string, or the current date/time as a duration since 1970-01-01T00:00:00Z.

VersionModifiedByDetails
1.12001-06-11Jeni Tennison

Added Javascript implementation.

1.22001-06-12Jeni Tennison

Added implementations in XSLT and EXSLT - Functions.

1.32001-06-16Jeni Tennison
  • Updated Javascript implementation.
  • Added Microsoft-specific stylesheet.
1.42002-08-21Craig Stewart

Added libxslt implementation to the list.

http://www.exslt.org/date/functions/duration/date.duration.template.xsl.html last modified 2002-08-21