Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 1st, 2008, 11:41 AM
Registered User
Join Date: Sep 2008
Location: Hyderabad, AP, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help Convert Epoch to YYYYMMDDHHMMSS +GMT


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:element name="display-name">
                        <xsl:value-of select="channame"/>

            <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 name="stop">
                        <xsl:value-of select="endtimesecs"/>
                    <xsl:attribute name="channel">
                        <xsl:value-of select="chanid"/>
                    <xsl:element name="title">
                        <xsl:value-of select="normalize-space(name)"/>
                    <xsl:element name="desc">
                        <xsl:value-of select="normalize-space(desc)"/>
                    <xsl:element name="category">
                        <xsl:value-of select="normalize-space(catname)"/>
                    <xsl:element name="episode-num">
                        <xsl:value-of select="normalize-space(epname)"/>

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.

epoch Date: 1220286782
Output XML Date: 20080901220302 +0530

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

Srikanth Koka
Reply With Quote
  #2 (permalink)  
Old September 1st, 2008, 11:54 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts

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
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 02:07 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.