11

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.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Anna
  • 4,099
  • 4
  • 24
  • 26
  • The answer to your question is: `0`. seconds-from-duration() just extracts the value of the seconds component from the supplied xs:duration. You obviously want to convert the duration to all seconds and then to calculate whatever "epoch" may be. Please, correct your question. – Dimitre Novatchev Aug 12 '10 at 13:10
  • See the following for a definition of epoch: http://en.wikipedia.org/wiki/Unix_time. Basically, it's the number of seconds from 1/1/1970 (UTC) – Anna Aug 12 '10 at 13:12
  • See my answer for the solution. :) +1 for your question. – Dimitre Novatchev Aug 12 '10 at 13:29

3 Answers3

17

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
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • 1
    +1 for divide by xs:duration (I was extracting each component, so I've deleted the answer). But I keep thinking if **time zone** is defined for *nix epoch. –  Aug 12 '10 at 13:51
4

A pure xsl 1.0 lib example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="https://github.com/ilyakharlamov/pure-xsl/date"
    version="1.0">
    <xsl:import href="https://raw.github.com/ilyakharlamov/pure-xsl/master/date.xsl"/>
    <xsl:template match="/">
        <xsl:variable name="time_as_timestamp" select="1365599995640"/>
        <xsl:text>time_as_timestamp:</xsl:text><xsl:value-of select="$time_as_timestamp"/><xsl:text>&#x0A;</xsl:text>
        <xsl:variable name="time_as_xsdatetime">
            <xsl:call-template name="date:date-time">
                <xsl:with-param name="timestamp" select="$time_as_timestamp"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:text>time_as_xsdatetime:</xsl:text><xsl:value-of select="$time_as_xsdatetime"/><xsl:text>&#x0A;</xsl:text>
        <xsl:text>converted back:</xsl:text>
        <xsl:call-template name="date:timestamp">
            <xsl:with-param name="date-time" select="$time_as_xsdatetime"/>
        </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

Output:

time_as_timestamp:1365599995640
time_as_xsdatetime:2013-04-10T13:19:55.640Z
converted back:1365599995640

Ilya Kharlamov
  • 3,698
  • 1
  • 31
  • 33
  • 1
    Considering that LibXML2 _still_ doesn't support XPath 2.0 functions — in *20-freaking-15* — this answer is awesome and totally saved me a boatload of time. Thanks! – aendra Nov 11 '15 at 17:51
0

As an xpath which does not use division but extracts from the duration:

for $i in (current-dateTime()-xs:dateTime('1970-01-01T00:00:00Z')) 
    return ((days-from-duration($i)*86400)+(hours-from-duration($i)*3600)+(minutes-from-duration($i)*60)+(seconds-from-duration($i)))
WEFX
  • 8,298
  • 8
  • 66
  • 102
Ben
  • 9
  • 1