Search code examples
xmlxslt-2.0

Format duration time


I have got two timestamps in milliseconds. I calculate duration between them, but the result has also milliseconds value, but I want just second.

First timestamp,

1519077042063

Second timestamp

1519077045841

Result after XSLT

3.778

xs:dateTime(1519077045841)-xs:dateTime(1519077042063))

Wanted result

3

EDIT

Here is how i calculate the dateTime from timestamp.

<xsl:variable name="start" select='xs:dateTime("1970-01-01T00:00:00") + 1519077042063 * xs:dayTimeDuration("PT0.001S")'/>

<xsl:variable name="stop" select='xs:dateTime("1970-01-01T00:00:00") + 1519077045841 * xs:dayTimeDuration("PT0.001S")'/>

Then I calculate a duration from start and stop which is PT3.778S and I want just 3. It is possible to do it?

<xsl:value-of select="xs:dateTime($stop)-xs:dateTime($start))"/>

Solution

  • I don't see how you could create an xs:dateTime directly from the timestamp value as you show with xs:dateTime(1519077045841)-xs:dateTime(1519077042063) but if you substract two xs:dateTimes you get a duration and you can then extract the seconds-from-duration and call the floor function on it: floor(seconds-from-duration($dateTime1 - $dateTime2)).

    So for your sample in the edit

       <xsl:variable name="start" select='xs:dateTime("1970-01-01T00:00:00") + 1519077042063 * xs:dayTimeDuration("PT0.001S")'/>
       <xsl:variable name="stop" select='xs:dateTime("1970-01-01T00:00:00") + 1519077045841 * xs:dayTimeDuration("PT0.001S")'/>
       <xsl:value-of select="floor(seconds-from-duration($stop - $start))"/>
    

    gives 3: http://xsltransform.hikmatu.com/nbUY4ks