Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 1st, 2008, 11:41 AM
Registered User
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help Convert Epoch to YYYYMMDDHHMMSS +GMT

Hi,

I must confess I am an absolute XSLT newbie. The task at my hand is to convert some TV guide listing XML into another XML that can be read by a program (XMLTV format). I got most of it working except for the date conversion from epoch to YYYYMMDDHHMMSS +GMT format.

I got some javascript code on the web for conversion of epoch date to other formats but I do not know how to use it. I am hopoing someone would kindly help me with this.

My XSL is as follows.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="unique-channel" match="Srikanth/programme" use="chanid"/>

    <xsl:template match="/">

        <xsl:element name="tv">
            <xsl:for-each select="Srikanth/programme[count(. | key('unique-channel', chanid)[1]) = 1]">
                <xsl:sort select="chanid"/>
                <xsl:element name="channel">
                    <xsl:attribute name="id">
                        <xsl:value-of select="chanid"/>
                    </xsl:attribute>
                    <xsl:element name="display-name">
                        <xsl:value-of select="channame"/>
                    </xsl:element>
                </xsl:element>
            </xsl:for-each>

            <xsl:for-each select="Srikanth/programme">
                <xsl:sort select="chanid"/>
                <xsl:sort select="startdate"/>
                <xsl:sort select="starttime"/>

                <xsl:element name="programme">
                    <xsl:attribute name="start">


                        <xsl:value-of select="starttimesecs"/>
                    </xsl:attribute>
                    <xsl:attribute name="stop">
                        <xsl:value-of select="endtimesecs"/>
                    </xsl:attribute>
                    <xsl:attribute name="channel">
                        <xsl:value-of select="chanid"/>
                    </xsl:attribute>
                    <xsl:element name="title">
                        <xsl:value-of select="normalize-space(name)"/>
                    </xsl:element>
                    <xsl:element name="desc">
                        <xsl:value-of select="normalize-space(desc)"/>
                    </xsl:element>
                    <xsl:element name="category">
                        <xsl:value-of select="normalize-space(catname)"/>
                    </xsl:element>
                    <xsl:element name="episode-num">
                        <xsl:value-of select="normalize-space(epname)"/>
                    </xsl:element>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>


The starttimesecs and endtimesecs are in epoch seconds. I found by googling that the following javascript can convert epoch to date format.

var myDate = new Date( your epoch date *1000);
document.write(myDate.toGMTString()+"<br>"+myDate. toLocaleString());

How do I use this code in my XSL to create an output XML document? I am using Stylus Studio to get the output XML.

Example:
epoch Date: 1220286782
Output XML Date: 20080901220302 +0530


Could somebody [u]PLEASE </u>help me?

Thanks,
Srikanth Koka
 
Old September 1st, 2008, 11:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Looks like you are using XSLT 1.0. Date/time manipulation is much easier in XSLT 2.0, so switch to 2.0 if that's an option. Some XSLT 1.0 processors allow calling out to Javascript, others to Java, so calling an external function might be an option if your code doesn't have to be portable.

The other option is to use date:add-duration() from www.exslt.org. (The epoch date, if I remember correctly, is the number of seconds since 1 Jan 1970, so you can convert it to a proper timestamp by treating it as a duration and adding it to the baseline date/time).

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
GMT(Time Zone) Help Mukesh Dubey ASP.NET 2.0 Professional 1 May 7th, 2008 09:18 PM
Converting Epoch Time to Readable Date maigoro ASP.NET 3.5 Basics 2 April 12th, 2008 11:42 AM
To get the gmt time using Crystal reports pallavi BOOK: Professional Crystal Reports for VS.NET 0 August 24th, 2004 06:10 AM
Help with GMT timestamps javahelpet JSP Basics 0 December 31st, 2003 02:54 PM
Working with GMT am_po_28 Beginning VB 6 1 December 8th, 2003 10:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.