[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

REx6: GMT to BST converter

Subject: REx6: GMT to BST converter
From: cknell@xxxxxxxxxx
Date: Mon, 18 Apr 2005 15:01:15 -0400
bst dates
I have cleared up the problems I know about. Here is a tidier and shorter version of the stylesheet with the template in question. I have modified the stylesheet so I could test multiple inputs on a single run.

First my test input document:

<?xml version="1.0" encoding="UTF-8" ?>
<date-list>
 <Date>Sunday, 27 March, 2005, 01:21 GMT</Date>
 <Date>Sunday, 27 May, 2005, 01:21 GMT</Date>
 <Date>Sunday, 27 October, 2005, 01:21 GMT</Date>
 <Date>Sunday, 27 January, 2005, 01:21 GMT</Date>
 <Date>Sunday, 27 June, 2005, 23:21 GMT</Date>
</date-list>

The output document:
<?xml version="1.0" encoding="UTF-8"?>
<date-list>
  <Date>Sunday, 27 March, 2005, 2:21 BST</Date>
  <Date>Sunday, 27 May, 2005, 2:21 BST</Date>
  <Date>Sunday, 27 October, 2005, 01:21 GMT</Date>
  <Date>Sunday, 27 January, 2005, 01:21 GMT</Date>
  <Date>Monday, 28 June, 2005, 00:21 BST</Date>
</date-list>

The stylesheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" encoding="UTF-8" />
 <xsl:strip-space elements="*" />

 <xsl:template match="/">
   <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="date-list">
   <date-list>
   <xsl:apply-templates />
 </date-list>
 </xsl:template>

 <xsl:template match="Date">
   <!-- Some convenience variables -->
     <!-- Day of the Week (e.g., Sunday) -->
     <xsl:variable name="DOW" select="substring-before(., ', ')" />

     <!-- Date and month substring (e.g., 14 January) -->
     <xsl:variable name="dm" select="substring-before(substring-after(.,', '),', ')" />

     <!-- Month name (e.g., January) -->
     <xsl:variable name="M" select="substring-after($dm,' ')" />

     <!-- Day number (e.g., 14) -->
     <xsl:variable name="D" select="substring-before($dm,' ')" />

     <!-- Hour:Minutes -->
     <xsl:variable name="h" select="substring-before(substring-after(substring-after(substring-after(.,$dm), ', '), ', '), ' GMT')" />

     <!-- Year with surrounding characters -->
     <xsl:variable name="y" select="substring-after(substring-before(., $h),$dm)" />

     <!-- Year stripped of surrounding characters -->
     <xsl:variable name="Y" select="substring-before(substring-after($y, ', '), ',')" />

   <xsl:variable name="BST-in-effect">
     <xsl:choose>
       <xsl:when test="$M = 'November' or $M = 'December' or $M = 'January' or $M = 'February'">False</xsl:when>
       <xsl:when test="$M = 'April' or $M = 'May' or $M = 'June' or $M = 'July' or $M = 'August' or $M = 'September'">True</xsl:when>
       <xsl:when test="$M = 'October'">
           <xsl:choose>
             <xsl:when test="$DOW = 'Sunday' and $D &gt; 24">False</xsl:when>
             <xsl:when test="$DOW = 'Monday' and $D &gt; 25">False</xsl:when>
             <xsl:when test="$DOW = 'Tuesday' and $D &gt; 26">False</xsl:when>
             <xsl:when test="$DOW = 'Wednesday' and $D &gt; 27">False</xsl:when>
             <xsl:when test="$DOW = 'Thursday' and $D &gt; 28">False</xsl:when>
             <xsl:when test="$DOW = 'Friday' and $D &gt; 29">False</xsl:when>
             <xsl:when test="$DOW = 'Saturday' and $D &gt; 30">False</xsl:when>
             <xsl:otherwise>True</xsl:otherwise>
           </xsl:choose>
         </xsl:when>
         <xsl:when test="$M='March'">
           <!-- Edge cases for changing to BST -->
           <xsl:choose>
             <xsl:when test="$DOW = 'Sunday' and $D &gt; 24">True</xsl:when>
             <xsl:when test="$DOW = 'Monday' and $D &gt; 25">True</xsl:when>
             <xsl:when test="$DOW = 'Tuesday' and $D &gt; 26">True</xsl:when>
             <xsl:when test="$DOW = 'Wednesday' and $D &gt; 27">True</xsl:when>
             <xsl:when test="$DOW = 'Thursday' and $D &gt; 28">True</xsl:when>
             <xsl:when test="$DOW = 'Friday' and $D &gt; 29">True</xsl:when>
             <xsl:when test="$DOW = 'Saturday' and $D &gt; 30">True</xsl:when>
             <xsl:otherwise>False</xsl:otherwise>
           </xsl:choose>
         </xsl:when>
     </xsl:choose>
   </xsl:variable>

   <xsl:choose>
     <xsl:when test="$BST-in-effect = 'False'">
       <xsl:copy-of select="." />
     </xsl:when>
     <xsl:otherwise>
     <!-- BST is in effect. Adjustments to the day of week, date, month, and hour may be necessary -->
       <xsl:variable name="D-BST">
         <!-- Check to see if the date must roll over -->
         <xsl:choose>
           <xsl:when test="number(substring-before($h, ':')) = 23">
             <xsl:choose>
               <!-- Cases where the roll-over brings the first day of the subsequent month -->
               <xsl:when test="$M = 'March' and $D = '31'">1</xsl:when>
                 <xsl:when test="$M = 'April' and $D = '30'">1</xsl:when>
                 <xsl:when test="$M = 'May' and $D = '31'">1</xsl:when>
                 <xsl:when test="$M = 'June' and $D = '30'">1</xsl:when>
                 <xsl:when test="$M = 'July' and $D = '31'">1</xsl:when>
                 <xsl:when test="$M = 'August' and $D = '31'">1</xsl:when>
                 <xsl:when test="$M = 'September' and $D = '30'">1</xsl:when>
                 <!-- Necessary only to add one to the current date -->
                 <xsl:otherwise><xsl:value-of select="number($D) + 1" /></xsl:otherwise>
             </xsl:choose>
           </xsl:when>
           <!-- No adjustment necessary -->
           <xsl:otherwise><xsl:value-of select="$D" /></xsl:otherwise>
         </xsl:choose>
       </xsl:variable>
       <xsl:variable name="DOW-BST">
         <!-- Check to see if the day of the week must roll over -->
         <xsl:choose>
           <xsl:when test="number(substring-before($h, ':')) = 23">
             <xsl:choose>
               <xsl:when test="$DOW = 'Sunday'">Monday</xsl:when>
               <xsl:when test="$DOW = 'Monday'">Tuesday</xsl:when>
               <xsl:when test="$DOW = 'Tuesday'">Wednesday</xsl:when>
               <xsl:when test="$DOW = 'Wednesday'">Thursday</xsl:when>
               <xsl:when test="$DOW = 'Thursday'">Friday</xsl:when>
               <xsl:when test="$DOW = 'Friday'">Saturday</xsl:when>
               <xsl:when test="$DOW = 'Saturday'">Sunday</xsl:when>
             </xsl:choose>
           </xsl:when>
           <xsl:otherwise><xsl:value-of select="$DOW" /></xsl:otherwise>
         </xsl:choose>
       </xsl:variable>
       <xsl:variable name="H-BST">
         <!-- Adjust the hour if necessary -->
         <xsl:choose>
           <xsl:when test="number(substring-before($h, ':')) = 23"><xsl:value-of select="concat('00:', substring-after($h, ':'))" /></xsl:when>
           <xsl:otherwise><xsl:value-of select="concat(number(substring-before($h, ':')) + 1, ':', substring-after($h, ':'))" /></xsl:otherwise>
         </xsl:choose>
       </xsl:variable>
       <xsl:variable name="M-BST">
         <!-- Adjust the month if necessary -->
         <xsl:choose>
           <xsl:when test="number(substring-before($h, ':')) = 23">
             <xsl:choose>
               <xsl:when test="$M = 'March' and $D = '31'">April</xsl:when>
               <xsl:when test="$M = 'April' and $D = '30'">May</xsl:when>
               <xsl:when test="$M = 'May' and $D = '31'">June</xsl:when>
               <xsl:when test="$M = 'June' and $D = '30'">July</xsl:when>
               <xsl:when test="$M = 'July' and $D = '31'">August</xsl:when>
               <xsl:when test="$M = 'August' and $D = '31'">September</xsl:when>
               <xsl:when test="$M = 'September' and $D = '30'">October</xsl:when>
               <xsl:otherwise><xsl:value-of select="$M" /></xsl:otherwise>
             </xsl:choose>
           </xsl:when>
           <xsl:otherwise><xsl:value-of select="$M" /></xsl:otherwise>
         </xsl:choose>
       </xsl:variable>
       <Date><xsl:value-of select="$DOW-BST" />, <xsl:value-of select="concat($D-BST, ' ')" /> <xsl:value-of select="$M-BST" />, <xsl:value-of select="$Y" />, <xsl:value-of select="$H-BST" /> BST</Date>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

</xsl:stylesheet>
--
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     cknell@xxxxxxxxxx
Sent:     Mon, 18 Apr 2005 07:42:38 -0400
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  RE: RE: RE: RE: RE:  GMT to BST converter

There are some omissions, one of which I have corrected (see the reply to Dave Pawson), but there are no hard-coded specific dates and times. Please tell me where you see them and I'll explain what I think they really are.
--
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     tom tom <tomxsllist@xxxxxxxxxxx>
Sent:     Mon, 18 Apr 2005 11:05:11 +0100
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  RE: RE: RE: RE:  GMT to BST converter

Thanks Charles, though as you have specific dates and times hardcoded I'm presuming that it's only good for this year?

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.