Date and Time

1. Another way of getting date and time, if you are connected to the net
2. Get time and data
3. Insert date and time

1.

Another way of getting date and time, if you are connected to the net

Daniel Bibbens



>
>  <h1>On the third stroke it will be <xsl:value-of 
> select="document('http://xobjex.com/cgi-bin/date.pl')"/></h1>
> gives:
>
> On the third stroke it will be Thu Aug 19 00:14:27 20042004819014274
>
> Substrings will work that out, though GMT might make it easier.

I have modified the output of the URL 'http://xobjex.com/cgi-bin/date.pl' to produce timezones (local, and utc), a XML Schema friendly stamp, and component data for each date. Like this:

<?xml version="1.0" encoding="utf-8"?>
<date>
  <local tz="PDT" stamp="2004-08-20T17:40:47">
    <year>2004</year>
    <month>08</month>
    <day>20</day>
    <hour>17</hour>
    <minute>40</minute>
    <second>47</second>
  </local>
  <utc tz="GMT" stamp="2004-08-21T00:40:47">
    <year>2004</year>
    <month>08</month>
    <day>21</day>
    <hour>00</hour>
    <minute>40</minute>
    <second>47</second>
  </utc>
</date>

So you'll want to use it in a manner similar to these examples:

<xsl:variable
 name="date" 
 select="document('http://xobjex.com/cgi-bin/date.pl)/date"/>

<xsl:variable
 name="stamp"
 select="$date/utc/@stamp"/>

<!-- produces: On the third stroke it will be 2004-08-21T00:40:47 --> <h1>
  On the third stroke it will be
  <xsl:value-of select="$stamp"/>
</h1>

<!-- produces: In Santa Monica, California, the sun set 
     at 7:34 PM PDT today. -->
<p>
  <xsl:text>In Santa Monica, California, the sun set at 7:34 PM </xsl:text>
  <xsl:value-of select="$date/local/@tz"/>
  <xsl:text> today.</xsl:text>
</p>

<!-- produces: The date in London is 21-08-2004. --> <p>
  <xsl:text>The date in London is </xsl:text>
  <xsl:value-of select="concat(
		$date/utc/day, '-', 
		$date/utc/month, '-', 
		$date/utc/year
		)"/>
  <xsl:text>.</xsl:text>
</p>

<!-- produces: The date in Santa Monica is 20-08-2004. --> <p>
  <xsl:text>The date in London is </xsl:text>
  <xsl:value-of select="concat(
		$date/local/day, '-', 
		$date/local/month, '-', 
		$date/local/year
		)"/>
  <xsl:text>.</xsl:text>
</p>

2.

Get time and data

Joerg Pietschmann

There is currently no way to do this from within an XSL sheet. I use external tools to write the desired information into a XML file and get the value into the XSL sheet by using document():

Command line frame (requires Unix toolset, Korn shell or bash):

cat >time.xml <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<time>$(date "+%Y-%m-%d %H:%M:%S")</time>
EOF
saxon -o $3 $1 $2

XSL snippet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="time" select="document('time.xml')/time"/>
...

This reads a nicely formatted timestamp into the $time variable. If you need year, month, day etc. separately, you could structure the time.xml file:

cat >time.xml <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<time>
  <year>$(date "+%Y")</year>
  <month>$(date "+%m")</month>
   ...
</time>
EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="time" select="document('time.xml')/time"/>

  ... 
    <my-year><xsl:value-of select="$time/year"/></my-year>
  ...

Of course, if you work on Windows, creating time.xml may be a greater challenge. I installed an Unix toolset on my windows machine (the example above was developed on windows!). There are several, you might want to check out the free CYGWIN toolkit at http://www.cygwin.org . Alternatively, you can use WSH, Visual Basic, Perl or write a java program.

3.

Insert date and time

???

 * generate a timestamp (this is a M$-DOS example)
 echo %date% %time% >timestamp.txt
 * declare this file as an entity:
 <!DOCTYPE xsl:stylesheet[
 ...
 <!ENTITY timestamp SYSTEM "timestamp.txt">
 ...
 ]>
 * and use it
 <pubdate>&timestamp;</pubdate>