Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dateTime to Epoch and vice versa xslt

Tags:

xslt

I have been trying to convert a given dateTime to epoch time and also a given epoch time to dateTime. I am quite new to xslt and have been struggling with this quite for some time, it is not giving me back any results. Here is my xslt so far

    <xsl:stylesheet version="1.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ns0="http://www.NoPreAuth.org"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="xsi xsl ns0 xsd">
<xsl:template match="/">
<xsl:variable name="date1">
        <xsl:value-of select="/ns0:NoAuthInput/ns0:StartDate"/>
</xsl:variable>
<xsl:variable name="date2">
        <xsl:value-of select="/ns0:NoAuthInput/ns0:EndDate"/>
</xsl:variable>
<ns0:NoPreAuthInput>
<ns0:Product>
        <xsl:value-of select="/ns0:NoAuthInput/ns0:Product"/>
</ns0:Product>
<!-- datTime to epoch -->
<ns0:END_T> 
  <xsl:value-of select= "(('$date1')  - xsd:dateTime('1970-01-01T00:00:00') )  div  xsd:dayTimeDuration('PT1S') "/>
</ns0:END_T>
<!-- epoch To datTime -->
<ns0:Closed_T> 
  <xsl:value-of select= "(('$date2')  + xsd:dateTime('1970-01-01T00:00:00') )  *  xsd:dayTimeDuration('PT1S') "/>
</ns0:Closed_T>
</ns0:NoPreAuthInput>
</xsl:template>
</xsl:stylesheet>

and the xml which I am trying to convert is:

<?xml version="1.0" encoding="UTF-8" ?>
<NoAuthInput xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.NoAuth.org 

 xmlns="http://www.NoAuth.org">
   <Product>ABC</Product>
   <StartDate>2015-10-05T15:52:40.782</StartDate>
   <EndDate>1444150760</EndDate>
</NoAuthInput> 

any help on this much appreciated. Thanks

like image 275
Novice Avatar asked Jan 08 '23 10:01

Novice


2 Answers

To convert Unix time to ISO 8601 date-time:

<xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', UnixTime, 'S'))"/>

To convert ISO 8601 date-time to Unix time;

<xsl:value-of select="floor((xs:dateTime(ISODateTime) - xs:dateTime('1970-01-01T00:00:00')) div xs:dayTimeDuration('PT1S')) "/>

Requires XSLT 2.0.

Working demo: http://xsltransform.net/94rmq5L

like image 68
michael.hor257k Avatar answered Jan 25 '23 22:01

michael.hor257k


If you are trying to do this in XSLT 1.0 on MSXML (I know the original asker is not):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:userCSharp="http://stackoverflow.com/xsltexample">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
  <xsl:template match="/">
    <xsl:value-of select="userCSharp:DateToEpoch('1970-01-02')" />
  </xsl:template>
  <msxsl:script language="CSharp" implements-prefix="userCSharp"><![CDATA[
     public string DateToEpoch(string s)
     {
         DateTime dt = DateTime.Parse(s);  
         DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

         return (dt - epoch).TotalSeconds.ToString();
     }
   ]]></msxsl:script>
</xsl:stylesheet>

Replace the '1970-01-02' with whatever text node you want and this should work, as long as that node was a valid date time. If not, it's easy enough to write up another simple method to do that using DateTime.Parse/TryParse. The output of this template (against any valid XML) would be 86400. Note that it's best to define methods in a CDATA node to avoid needing to escape quotes or angle brackets (this code doesn't happen to use any but might be extended to for some reason).

like image 37
Dan Field Avatar answered Jan 25 '23 22:01

Dan Field