Time calculation in xslt
I am using xsl version 2.0
I have a time field that needs to be added 1 to. I am getting a formatted number from below node:
<xsl:value-of select="Master/End/Timecode"/>
<xsl:text>
The data in "Master / End / Timecode" looks like 01: 00: 00: 00 basically time - hours: minutes: seconds: frames frames are counted from oo to 23. How to add 1 to frames and increase seconds and minutes if frames = 23.
Example 01: 00: 59: 23 plus 1 will result in 01: 01: 00: 00. Any ideas
Just convert it to frames, zoom in, then convert back:
<xsl:analyze-string select="Master/End/Timecode" regex="^(\d+):(\d+):(\d+):(\d+)$">
<xsl:matching-substring>
<!-- Split into components -->
<xsl:variable name="hours"
select="xs:integer(regex-group(1))"/>
<xsl:variable name="minutes"
select="xs:integer(regex-group(2))"/>
<xsl:variable name="seconds"
select="xs:integer(regex-group(3))"/>
<xsl:variable name="frames"
select="xs:integer(regex-group(4))"/>
<!-- Calculate total amount of frames -->
<xsl:variable name="total-frames"
select="$hours * 60 * 60 * 24 +
$minutes * 60 * 24 +
$seconds * 24 +
$frames "/>
<!-- Add 1 frame -->
<xsl:variable name="remaining-frames"
select="$total-frames + 1"/>
<!-- Calculate new component values -->
<xsl:variable name="new-hours"
select="$remaining-frames idiv (60 * 60 * 24)"/>
<xsl:variable name="remaining-frames"
select="$remaining-frames mod (60 * 60 * 24)"/>
<xsl:variable name="new-minutes"
select="$remaining-frames idiv (60 * 24)"/>
<xsl:variable name="remaining-frames"
select="$remaining-frames mod (60 * 24)"/>
<xsl:variable name="new-seconds"
select="$remaining-frames idiv 24"/>
<xsl:variable name="new-frames"
select="$remaining-frames mod 24"/>
<!-- Recombine components -->
<xsl:value-of select="$new-hours"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="$new-minutes"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="$new-seconds"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="$new-frames"/>
</xsl:matching-substring>
</xsl:analyze-string>
source to share
This is similar to what you would like to write a custom XPath function , or process outside of XSLT . There are ways to do this, but they are very difficult, very fast.
source to share
This is what my timecode XSLT library can do: https://github.com/sshaw/xslt-timecode . With it, you can do something like:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tc="http://screenstaring.com/xslt/timecode">
<xsl:import href="timecode.xsl"/>
<xsl:call-template name="tc:add">
<xsl:with-param name="timecode1">01:00:00:00</xsl:with-param>
<xsl:with-param name="timecode2">00:00:00:01</xsl:with-param>
</xsl:call-template>
<!-- Or -->
<xsl:variable name="frames">
<xsl:call-template name="tc:to-frames">
<xsl:with-param name="timecode">01:00:00:00</xsl:with-param>
<xsl:with-param name="fps">23.97</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="plus-one">
<xsl:call-template name="tc:from-frames">
<xsl:with-param name="frames" select="$frames + 1"/>
<xsl:with-param name="fps">23.97</xsl:with-param>
</xsl:call-template>
</xsl:variable>
Unfortunately, you can not just add 1, though multiply
, and divide
support the whole operands, so that add
is also likely to support him.
source to share