Convert dateTime to unix epoch in xslt

Anna picture Anna · Aug 12, 2010 · Viewed 12.9k times · Source

I have a dateTime variable, and I want to convert it to a decimal value of epoch. How can this be done?

I tried using:

seconds-from-duration($time, xs:dateTime('1970-01-01T00:00:00'))

but it just returns 0.

Please advice. Thanks.

Answer

Dimitre Novatchev picture Dimitre Novatchev · Aug 12, 2010

This transformation:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="text"/>

 <xsl:template match="/">
   <xsl:sequence select="current-dateTime()"/>

   <xsl:sequence select=
   "( current-dateTime() - xs:dateTime('1970-01-01T00:00:00') )
    div
     xs:dayTimeDuration('PT1S')
     "/>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), produces the wanted result -- the current date-time and its Unix epoch (the number of seconds since 1/1/1970 ):

2010-08-12T06:26:54.273-07:00    1281594414.273