Reputation: 168
I've run into an issue that I'm not sure how to resolve. I used the answer in Seconds to Time to create a template in XSLT 1.0. However, when my duration gets close to a whole number the result is incorrect. When run against values much larger or smaller than this the result appears correct.
My template is as below. When I have the Duration at 3597 the result comes out at 00:60:57 instead of 00:59:57 and the duration of 955 gives the result of 00:16:55 instead of 00:15:55.
I've tried removing the modulus with the thought that I could then just substring the result, but that didn't change the 3597 result and messed up multiple results that are correct under the below code. I have also expanded the format-number to include more digits but that also does not alter the result. I have also tried using floor($Duration - $hours div 60), but that didn't seem to work, or I formatted it wrong.
What am I missing? I don't see how 3597/60 can equal 60 especially when I am taking the modulus.
<xsl:template name="calculatedDuration">
<xsl:param name="Duration"/>
<xsl:choose>
<xsl:when test="$Duration >= 3600">
<xsl:value-of select="concat(format-number($Duration div 3600, '00'), ':')"/>
</xsl:when>
<xsl:otherwise>00:</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="format-number($Duration div 60 mod 60, '00')"/>
<xsl:value-of select="concat(':', format-number($Duration mod 60, '00'))"/>
</xsl:template>
Upvotes: 0
Views: 280
Reputation: 928
This is because format-number
does rounding, not truncating. format-number(59.97, '00')
has a value of 60, not 59. So use format-number(floor($Duration div 60) mod 60, '00')
instead.
Upvotes: 1