• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3365
  • Last Modified:

How to Use XSLT to convert Epoch time

I'm new to XSLT; we're starting to use version 2.

In my 3rd party XML document, there are recurring <TIME> tags. The <TIME> can be under the <CUSTOMER>, the <ORDER>, the <BACKORDER> etc. The value of <TIME> is an epoch time value.

I would like to either:
a) transform all of the values of <TIME> to format mm-dd-yyyy  or
b) add another node, say <READABLETIME>, with the mm-dd-yyyy value, and place it after (or before) the <TIME>

Can someone please show me how to structure this? I would prefer not to have to know all of the possible paths to <TIME>.

Thanks.
0
AIBMass
Asked:
AIBMass
  • 7
  • 5
1 Solution
 
Geert BormansInformation ArchitectCommented:
Try this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
     version="2.0">
<xsl:template match="TIME">
    <xsl:copy-of select="."/>
    <READABLETIME>
        <xsl:value-of select="substring-before(string(xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', ., 'S'))), 'T')"></xsl:value-of>
    </READABLETIME>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
Note that by having a template for <TIME> you can easily organise your stylesheet in a way that it activates each time there is a <TIME> element. Just use apply-templates consequently

Note that I cut of the date part. You can easily construct the mm-dd-yyy format by using functions such as day-from-dateTime() etc

this XML
<TIME>1234567890</TIME>

gives me this result
<TIME>1234567890</TIME>
<READABLETIME>2009-02-13</READABLETIME>
0
 
AIBMassAuthor Commented:
Yes, perfect, thanks.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Geert BormansInformation ArchitectCommented:
welcome
0
 
AIBMassAuthor Commented:

Cannot find the script or external object that implements prefix 'http://www.w3.org/2001/XMLSchema'
0
 
Geert BormansInformation ArchitectCommented:
using an XSLT1 processor instead of a xslt2 processor I am afraid
0
 
AIBMassAuthor Commented:
upgraded to XSLT2 processor.  Thanks.  

Above:
<xsl:value-of select="substring-before(string(xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', ., 'S'))), 'T')"></xsl:value-of>

Causes:  
Invalid duration value 'PT1282679470737S'
0
 
Geert BormansInformation ArchitectCommented:
which xslt processor is that?

13 digits for seconds in epoch time seems a lot
0
 
AIBMassAuthor Commented:
Saxon - Home Edition

maybe they're miliseconds?  The xml file is sent to us.  The error message says,

'Invalid duration value 'PT1282679470737S' <non-numeric component>
0
 
AIBMassAuthor Commented:
Looks like we had bad data.  If we truncate the input, it works great.  
Thanks for the insight.  
0
 
Geert BormansInformation ArchitectCommented:
explanation, highest possible integer on a 32bit system is 2147483647
the code works for that number, and not for anything higher
0
 
Geert BormansInformation ArchitectCommented:
This tells me that
2038-01-19
would be an interesting day for epoch time and 32-bit computers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now