Nikhil Preeth February 2016

How to convert 2002-05-30T09:30:10-06:00 to Unix time stamp using xslt 2.0

This is the logic i am trying to use .Should i sub string 2002-05-30T09:30:10-06:00 and get 2002-05-30T09:30:10 and 06:00 separate and subtract and use it in the below logic.If there is any easier way kindly let me know. {code}

<tns:timestamp>
      <xsl:variable name="date" select="/Order/@OrderDate"/>
       <xsl:value-of select='xs:dateTime("1970-01-01T00:00:00") + @date *xs:dayTimeDuration("PT0.001S")'/>
      </tns:timestamp>

Answers


Daniel Haley February 2016

I don't think you should subtract the offset in your calculation. If you do:

(xs:dateTime(@OrderDate) - xs:dateTime('1970-01-01T00:00:00Z')) div xs:dayTimeDuration('PT1S')

the result will be:

1022772610

If you convert that unix time into human readable time you get the correct GMT and local time:

enter image description here

Here's an example (see it working here)...

XML Input

<Order OrderDate="2002-05-30T09:30:10-06:00"/>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">
  <xsl:output indent="yes"/>

  <xsl:template match="/Order">
    <Order unix-time="{(xs:dateTime(@OrderDate) - xs:dateTime('1970-01-01T00:00:00Z')) 
      div xs:dayTimeDuration('PT1S')}">
      <xsl:copy-of select="@*|node()"/>
    </Order>
  </xsl:template>

</xsl:stylesheet>

XML Output

<Order unix-time="1022772610" OrderDate="2002-05-30T09:30:10-06:00"/>

Post Status

Asked in February 2016
Viewed 3,113 times
Voted 11
Answered 1 times

Search




Leave an answer