Index: ccr2ccd-xslt/trunk/lib/date.add.template.xsl
===================================================================
--- ccr2ccd-xslt/trunk/lib/date.add.template.xsl	(revision 748)
+++ ccr2ccd-xslt/trunk/lib/date.add.template.xsl	(revision 748)
@@ -0,0 +1,397 @@
+<?xml version="1.0"?>
+<xsl:stylesheet version="1.0"
+                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                xmlns:date="http://exslt.org/dates-and-times"
+                extension-element-prefixes="date">
+ 
+<date:month-lengths>
+   <date:month>31</date:month>
+   <date:month>28</date:month>
+   <date:month>31</date:month>
+   <date:month>30</date:month>
+   <date:month>31</date:month>
+   <date:month>30</date:month>
+   <date:month>31</date:month>
+   <date:month>31</date:month>
+   <date:month>30</date:month>
+   <date:month>31</date:month>
+   <date:month>30</date:month>
+   <date:month>31</date:month>
+</date:month-lengths>
+
+<xsl:template name="date:add">
+	<xsl:param name="date-time" />
+   <xsl:param name="duration" />
+   <xsl:variable name="dt-neg" select="starts-with($date-time, '-')" />
+   <xsl:variable name="dt-no-neg">
+      <xsl:choose>
+         <xsl:when test="$dt-neg or starts-with($date-time, '+')">
+            <xsl:value-of select="substring($date-time, 2)" />
+         </xsl:when>
+         <xsl:otherwise>
+            <xsl:value-of select="$date-time" />
+         </xsl:otherwise>
+      </xsl:choose>
+   </xsl:variable>
+   <xsl:variable name="dt-no-neg-length" select="string-length($dt-no-neg)" />
+   <xsl:variable name="timezone">
+      <xsl:choose>
+         <xsl:when test="substring($dt-no-neg, $dt-no-neg-length) = 'Z'">Z</xsl:when>
+         <xsl:otherwise>
+            <xsl:variable name="tz" select="substring($dt-no-neg, $dt-no-neg-length - 5)" />
+            <xsl:if test="(substring($tz, 1, 1) = '-' or 
+                           substring($tz, 1, 1) = '+') and
+                          substring($tz, 4, 1) = ':'">
+               <xsl:value-of select="$tz" />
+            </xsl:if>
+         </xsl:otherwise>
+      </xsl:choose>
+   </xsl:variable>
+   <xsl:variable name="new-dt">
+      <xsl:if test="not(string($timezone)) or
+                    $timezone = 'Z' or 
+                    (substring($timezone, 2, 2) &lt;= 23 and
+                     substring($timezone, 5, 2) &lt;= 59)">
+         <xsl:variable name="dt" select="substring($dt-no-neg, 1, $dt-no-neg-length - string-length($timezone))" />
+         <xsl:variable name="dt-length" select="string-length($dt)" />
+         <xsl:variable name="du-neg" select="starts-with($duration, '-')" />
+         <xsl:variable name="du">
+            <xsl:choose>
+               <xsl:when test="$du-neg"><xsl:value-of select="substring($duration, 2)" /></xsl:when>
+               <xsl:otherwise><xsl:value-of select="$duration" /></xsl:otherwise>
+            </xsl:choose>
+         </xsl:variable>
+         <xsl:if test="starts-with($du, 'P') and
+                       not(translate($du, '0123456789PYMDTHS.', ''))">
+            <xsl:variable name="du-date">
+               <xsl:choose>
+                  <xsl:when test="contains($du, 'T')"><xsl:value-of select="substring-before(substring($du, 2), 'T')" /></xsl:when>
+                  <xsl:otherwise><xsl:value-of select="substring($du, 2)" /></xsl:otherwise>
+               </xsl:choose>
+            </xsl:variable>
+            <xsl:variable name="du-time">
+               <xsl:if test="contains($du, 'T')"><xsl:value-of select="substring-after($du, 'T')" /></xsl:if>
+            </xsl:variable>
+            <xsl:if test="(not($du-date) or
+                           (not(translate($du-date, '0123456789YMD', '')) and
+                            not(substring-after($du-date, 'D')) and
+                            (contains($du-date, 'D') or 
+                             (not(substring-after($du-date, 'M')) and
+                              (contains($du-date, 'M') or
+                               not(substring-after($du-date, 'Y'))))))) and
+                          (not($du-time) or
+                           (not(translate($du-time, '0123456789HMS.', '')) and
+                            not(substring-after($du-time, 'S')) and
+                            (contains($du-time, 'S') or
+                             not(substring-after($du-time, 'M')) and
+                             (contains($du-time, 'M') or
+                              not(substring-after($du-time, 'Y'))))))">
+               <xsl:variable name="duy-str">
+                  <xsl:choose>
+                     <xsl:when test="contains($du-date, 'Y')"><xsl:value-of select="substring-before($du-date, 'Y')" /></xsl:when>
+                     <xsl:otherwise>0</xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:variable name="dum-str">
+                  <xsl:choose>
+                     <xsl:when test="contains($du-date, 'M')">
+                        <xsl:choose>
+                           <xsl:when test="contains($du-date, 'Y')"><xsl:value-of select="substring-before(substring-after($du-date, 'Y'), 'M')" /></xsl:when>
+                           <xsl:otherwise><xsl:value-of select="substring-before($du-date, 'M')" /></xsl:otherwise>
+                        </xsl:choose>
+                     </xsl:when>
+                     <xsl:otherwise>0</xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:variable name="dud-str">
+                  <xsl:choose>
+                     <xsl:when test="contains($du-date, 'D')">
+                        <xsl:choose>
+                           <xsl:when test="contains($du-date, 'M')"><xsl:value-of select="substring-before(substring-after($du-date, 'M'), 'D')" /></xsl:when>
+                           <xsl:when test="contains($du-date, 'Y')"><xsl:value-of select="substring-before(substring-after($du-date, 'Y'), 'D')" /></xsl:when>
+                           <xsl:otherwise><xsl:value-of select="substring-before($du-date, 'D')" /></xsl:otherwise>
+                        </xsl:choose>
+                     </xsl:when>
+                     <xsl:otherwise>0</xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:variable name="duh-str">
+                  <xsl:choose>
+                     <xsl:when test="contains($du-time, 'H')"><xsl:value-of select="substring-before($du-time, 'H')" /></xsl:when>
+                     <xsl:otherwise>0</xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:variable name="dumin-str">
+                  <xsl:choose>
+                     <xsl:when test="contains($du-time, 'M')">
+                        <xsl:choose>
+                           <xsl:when test="contains($du-time, 'H')"><xsl:value-of select="substring-before(substring-after($du-time, 'H'), 'M')" /></xsl:when>
+                           <xsl:otherwise><xsl:value-of select="substring-before($du-time, 'M')" /></xsl:otherwise>
+                        </xsl:choose>
+                     </xsl:when>
+                     <xsl:otherwise>0</xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:variable name="dus-str">
+                  <xsl:choose>
+                     <xsl:when test="contains($du-time, 'S')">
+                        <xsl:choose>
+                           <xsl:when test="contains($du-time, 'M')"><xsl:value-of select="substring-before(substring-after($du-time, 'M'), 'S')" /></xsl:when>
+                           <xsl:when test="contains($du-time, 'H')"><xsl:value-of select="substring-before(substring-after($du-time, 'H'), 'S')" /></xsl:when>
+                           <xsl:otherwise><xsl:value-of select="substring-before($du-time, 'S')" /></xsl:otherwise>
+                        </xsl:choose>
+                     </xsl:when>
+                     <xsl:otherwise>0</xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:variable name="mult" select="($du-neg * -2) + 1" />
+               <xsl:variable name="duy" select="$duy-str * $mult" />
+               <xsl:variable name="dum" select="$dum-str * $mult" />
+               <xsl:variable name="dud" select="$dud-str * $mult" />
+               <xsl:variable name="duh" select="$duh-str * $mult" />
+               <xsl:variable name="dumin" select="$dumin-str * $mult" />
+               <xsl:variable name="dus" select="$dus-str * $mult" />
+
+               <xsl:variable name="year" select="substring($dt, 1, 4) * (($dt-neg * -2) + 1)" />
+               <xsl:choose>
+                  <xsl:when test="$year and
+                                  string($duy) = 'NaN' or 
+                                  string($dum) = 'NaN' or 
+                                  string($dud) = 'NaN' or 
+                                  string($duh) = 'NaN' or 
+                                  string($dumin) = 'NaN' or 
+                                  string($dus) = 'NaN'" />
+                  <xsl:when test="$dt-length > 4 or
+                                  $dum or $dud or $duh or $dumin or $dus">
+                     <xsl:variable name="month">
+                        <xsl:choose>
+                           <xsl:when test="$dt-length > 4">
+                              <xsl:if test="substring($dt, 5, 1) = '-'">
+                                 <xsl:value-of select="substring($dt, 6, 2)" />
+                              </xsl:if>
+                           </xsl:when>
+                           <xsl:otherwise>1</xsl:otherwise>
+                        </xsl:choose>
+                     </xsl:variable>
+                     <xsl:choose>
+                        <xsl:when test="not($month) or $month > 12" />
+                        <xsl:when test="$dt-length > 7 or
+                                        $dud or $duh or $dumin or $dus">
+                           <xsl:variable name="day">
+                              <xsl:choose>
+                                 <xsl:when test="$dt-length > 7">
+                                    <xsl:if test="substring($dt, 8, 1) = '-'">
+                                       <xsl:value-of select="substring($dt, 9, 2)" />
+                                    </xsl:if>
+                                 </xsl:when>
+                                 <xsl:otherwise>1</xsl:otherwise>
+                              </xsl:choose>
+                           </xsl:variable>
+                           <xsl:choose>
+                              <xsl:when test="not($day) or $day > 31" />
+                              <xsl:when test="$dt-length > 10 or
+                                              $duh or $dumin or $dus">
+                                 <xsl:if test="$dt-length = 10 or
+                                               (substring($dt, 11, 1) = 'T' and
+                                                substring($dt, 14, 1) = ':' and
+                                                substring($dt, 17, 1) = ':')">
+                                    <xsl:variable name="hour">
+                                       <xsl:choose>
+                                          <xsl:when test="$dt-length > 10"><xsl:value-of select="substring($dt, 12, 2)" /></xsl:when>
+                                          <xsl:otherwise>0</xsl:otherwise>
+                                       </xsl:choose>
+                                    </xsl:variable>
+                                    <xsl:variable name="minute">
+                                       <xsl:choose>
+                                          <xsl:when test="$dt-length > 10"><xsl:value-of select="substring($dt, 15, 2)" /></xsl:when>
+                                          <xsl:otherwise>0</xsl:otherwise>
+                                       </xsl:choose>
+                                    </xsl:variable>
+                                    <xsl:variable name="second">
+                                       <xsl:choose>
+                                          <xsl:when test="$dt-length > 10"><xsl:value-of select="substring($dt, 18)" /></xsl:when>
+                                          <xsl:otherwise>0</xsl:otherwise>
+                                       </xsl:choose>
+                                    </xsl:variable>
+                                    <xsl:if test="$hour &lt;= 23 and $minute &lt;= 59 and $second &lt;= 60">
+                                       <xsl:variable name="new-second" select="$second + $dus" />
+                                       <xsl:variable name="new-minute" select="$minute + $dumin + floor($new-second div 60)" />
+                                       <xsl:variable name="new-hour" select="$hour + $duh + floor($new-minute div 60)" />
+                                       <xsl:variable name="new-month" select="$month + $dum" />
+                                       <xsl:call-template name="date:_add-days">
+                                          <xsl:with-param name="year" select="$year + $duy + floor(($new-month - 1) div 12)" />
+                                          <xsl:with-param name="month">
+                                             <xsl:variable name="m">
+                                                <xsl:choose>
+                                                   <xsl:when test="$new-month &lt; 1"><xsl:value-of select="$new-month + 12" /></xsl:when>
+                                                   <xsl:otherwise><xsl:value-of select="$new-month" /></xsl:otherwise>
+                                                </xsl:choose>
+                                             </xsl:variable>
+                                             <xsl:choose>
+                                                <xsl:when test="$m mod 12">
+                                                   <xsl:value-of select="format-number($m mod 12, '00')" />
+                                                </xsl:when>
+                                                <xsl:otherwise>12</xsl:otherwise>
+                                             </xsl:choose>
+                                          </xsl:with-param>
+                                          <xsl:with-param name="day" select="$day" />
+                                          <xsl:with-param name="days" select="$dud + floor($new-hour div 24)" />
+                                       </xsl:call-template>
+                                       <xsl:text>T</xsl:text>
+                                       <xsl:value-of select="format-number(($new-hour + 24) mod 24, '00')" />
+                                       <xsl:text>:</xsl:text>
+                                       <xsl:value-of select="format-number($new-minute mod 60, '00')" />
+                                       <xsl:text>:</xsl:text>
+                                       <xsl:if test="$new-second mod 60 &lt; 10">0</xsl:if>
+                                       <xsl:value-of select="$new-second mod 60" />
+                                       <xsl:value-of select="$timezone" />
+                                    </xsl:if>
+                                 </xsl:if>
+                              </xsl:when>
+                              <xsl:otherwise>
+                                 <xsl:variable name="new-month" select="$month + $dum" />
+                                 <xsl:call-template name="date:_add-days">
+                                    <xsl:with-param name="year" select="$year + $duy + floor(($new-month - 1) div 12)" />
+                                    <xsl:with-param name="month">
+                                       <xsl:variable name="m">
+                                          <xsl:choose>
+                                             <xsl:when test="$new-month &lt; 1"><xsl:value-of select="$new-month + 12" /></xsl:when>
+                                             <xsl:otherwise><xsl:value-of select="$new-month" /></xsl:otherwise>
+                                          </xsl:choose>
+                                       </xsl:variable>
+                                       <xsl:choose>
+                                          <xsl:when test="$m mod 12">
+                                             <xsl:value-of select="format-number($m mod 12, '00')" />
+                                          </xsl:when>
+                                          <xsl:otherwise>12</xsl:otherwise>
+                                       </xsl:choose>
+                                    </xsl:with-param>
+                                    <xsl:with-param name="day" select="$day" />
+                                    <xsl:with-param name="days" select="$dud" />
+                                 </xsl:call-template>
+                                 <xsl:value-of select="$timezone" />
+                              </xsl:otherwise>
+                           </xsl:choose>
+                        </xsl:when>
+                        <xsl:otherwise>
+                           <xsl:variable name="new-month" select="$month + $dum" />
+                           <xsl:value-of select="format-number($year + $duy + floor(($new-month - 1) div 12), '0000')" />
+                           <xsl:text>-</xsl:text>
+                           <xsl:variable name="m">
+                              <xsl:choose>
+                                 <xsl:when test="$new-month &lt; 1"><xsl:value-of select="$new-month + 12" /></xsl:when>
+                                 <xsl:otherwise><xsl:value-of select="$new-month" /></xsl:otherwise>
+                              </xsl:choose>
+                           </xsl:variable>
+                           <xsl:choose>
+                              <xsl:when test="$m mod 12">
+                                 <xsl:value-of select="format-number($m mod 12, '00')" />
+                              </xsl:when>
+                              <xsl:otherwise>12</xsl:otherwise>
+                           </xsl:choose>
+                           <xsl:value-of select="$timezone" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:otherwise>
+                     <xsl:value-of select="format-number($year + $duy, '0000')" />
+                     <xsl:value-of select="$timezone" />
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:if>
+         </xsl:if>
+      </xsl:if>
+   </xsl:variable>
+   <xsl:choose>
+     <xsl:when test="string-length($date-time) > 10">
+       <xsl:value-of select="$new-dt" />
+     </xsl:when>
+     <xsl:otherwise>
+       <xsl:value-of select="substring($new-dt, 1, string-length($date-time))" />
+     </xsl:otherwise>
+   </xsl:choose>
+</xsl:template>
+
+<xsl:template name="date:_add-days">
+   <xsl:param name="year" />
+   <xsl:param name="month" />
+   <xsl:param name="day" />
+   <xsl:param name="days" />
+   <xsl:param name="new-day" select="'NaN'" />
+   <xsl:variable name="leap" select="(not($year mod 4) and $year mod 100) or not($year mod 400)" />
+   <xsl:variable name="month-days" select="document('')/*/date:month-lengths/date:month" />
+   <xsl:variable name="days-in-month">
+      <xsl:choose>
+         <xsl:when test="$month = 2 and $leap">
+            <xsl:value-of select="$month-days[number($month)] + 1" />
+         </xsl:when>
+         <xsl:otherwise>
+            <xsl:value-of select="$month-days[number($month)]" />
+         </xsl:otherwise>
+      </xsl:choose>
+   </xsl:variable>
+   <xsl:choose>
+      <xsl:when test="$new-day = 'NaN'">
+         <xsl:call-template name="date:_add-days">
+            <xsl:with-param name="year" select="$year" />
+            <xsl:with-param name="month" select="$month" />
+            <xsl:with-param name="new-day">
+               <xsl:choose>
+                  <xsl:when test="$day > $days-in-month">
+                     <xsl:value-of select="$days-in-month + $days" />
+                  </xsl:when>
+                  <xsl:otherwise><xsl:value-of select="$day + $days" /></xsl:otherwise>
+               </xsl:choose>
+            </xsl:with-param>
+         </xsl:call-template>
+      </xsl:when>
+      <xsl:otherwise>
+         <xsl:choose>
+            <xsl:when test="$new-day &lt; 1">
+               <xsl:call-template name="date:_add-days">
+                  <xsl:with-param name="year" select="$year - ($month = 1)" />
+                  <xsl:with-param name="month">
+                     <xsl:choose>
+                        <xsl:when test="$month = 1">12</xsl:when>
+                        <xsl:otherwise><xsl:value-of select="$month - 1" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:with-param>
+                  <xsl:with-param name="new-day">
+                     <xsl:variable name="days-in-new-month">
+                        <xsl:choose>
+                           <xsl:when test="$leap and $month = 3">29</xsl:when>
+                           <xsl:when test="$month = 1">31</xsl:when>
+                           <xsl:otherwise>
+                              <xsl:value-of select="$month-days[$month - 1]" />
+                           </xsl:otherwise>
+                        </xsl:choose>
+                     </xsl:variable>                     
+                     <xsl:value-of select="$new-day + $days-in-new-month" />
+                  </xsl:with-param>
+               </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="$new-day > $days-in-month">
+               <xsl:call-template name="date:_add-days">
+                  <xsl:with-param name="year" select="$year + ($month = 12)" />
+                  <xsl:with-param name="month">
+                     <xsl:choose>
+                        <xsl:when test="$month = 12">1</xsl:when>
+                        <xsl:otherwise><xsl:value-of select="$month + 1" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:with-param>
+                  <xsl:with-param name="new-day" select="$new-day - $days-in-month" />
+               </xsl:call-template>
+            </xsl:when>
+            <xsl:otherwise>
+               <xsl:value-of select="format-number($year, '0000')" />
+               <xsl:text>-</xsl:text>
+               <xsl:value-of select="format-number($month, '00')" />
+               <xsl:text>-</xsl:text>
+               <xsl:value-of select="format-number($new-day, '00')" />
+            </xsl:otherwise>
+         </xsl:choose>
+      </xsl:otherwise>
+   </xsl:choose>
+</xsl:template>
+
+</xsl:stylesheet>
Index: ccr2ccd-xslt/trunk/lib/date.date-time.xsl
===================================================================
--- ccr2ccd-xslt/trunk/lib/date.date-time.xsl	(revision 748)
+++ ccr2ccd-xslt/trunk/lib/date.date-time.xsl	(revision 748)
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates and times" version="1.0" extension-element-prefixes="date" date:doc="http://www.exslt.org/date">
+   <func:script language="exslt:javascript" implements-prefix="date" src="date.js"/>
+   <func:script language="exslt:msxsl" implements-prefix="date" src="date.msxsl.xsl"/>
+</stylesheet>
Index: ccr2ccd-xslt/trunk/lib/date.format-date.template.xsl
===================================================================
--- ccr2ccd-xslt/trunk/lib/date.format-date.template.xsl	(revision 748)
+++ ccr2ccd-xslt/trunk/lib/date.format-date.template.xsl	(revision 748)
@@ -0,0 +1,508 @@
+<?xml version="1.0"?>
+<xsl:stylesheet version="1.0"
+                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                xmlns:date="http://exslt.org/dates-and-times"
+                xmlns:str="http://exslt.org/strings"
+                extension-element-prefixes="date str">
+
+<xsl:import href="./str.padding.template.xsl" />
+
+<date:months>
+   <date:month length="31" abbr="Jan">January</date:month>
+   <date:month length="28" abbr="Feb">February</date:month>
+   <date:month length="31" abbr="Mar">March</date:month>
+   <date:month length="30" abbr="Apr">April</date:month>
+   <date:month length="31" abbr="May">May</date:month>
+   <date:month length="30" abbr="Jun">June</date:month>
+   <date:month length="31" abbr="Jul">July</date:month>
+   <date:month length="31" abbr="Aug">August</date:month>
+   <date:month length="30" abbr="Sep">September</date:month>
+   <date:month length="31" abbr="Oct">October</date:month>
+   <date:month length="30" abbr="Nov">November</date:month>
+   <date:month length="31" abbr="Dec">December</date:month>
+</date:months>
+
+<date:days>
+   <date:day abbr="Sun">Sunday</date:day>
+   <date:day abbr="Mon">Monday</date:day>
+   <date:day abbr="Tue">Tuesday</date:day>
+   <date:day abbr="Wed">Wednesday</date:day>
+   <date:day abbr="Thu">Thursday</date:day>
+   <date:day abbr="Fri">Friday</date:day>
+   <date:day abbr="Sat">Saturday</date:day>
+</date:days>
+
+<xsl:template name="date:format-date">
+	<xsl:param name="date-time" />
+   <xsl:param name="pattern" />
+   <xsl:variable name="formatted">
+      <xsl:choose>
+         <xsl:when test="starts-with($date-time, '---')">
+            <xsl:call-template name="date:_format-date">
+               <xsl:with-param name="year" select="'NaN'" />
+               <xsl:with-param name="month" select="'NaN'" />
+               <xsl:with-param name="day" select="number(substring($date-time, 4, 2))" />
+               <xsl:with-param name="pattern" select="$pattern" />
+            </xsl:call-template>
+         </xsl:when>
+         <xsl:when test="starts-with($date-time, '--')">
+            <xsl:call-template name="date:_format-date">
+               <xsl:with-param name="year" select="'NaN'" />
+               <xsl:with-param name="month" select="number(substring($date-time, 3, 2))" />
+               <xsl:with-param name="day" select="number(substring($date-time, 6, 2))" />
+               <xsl:with-param name="pattern" select="$pattern" />
+            </xsl:call-template>
+         </xsl:when>
+         <xsl:otherwise>
+            <xsl:variable name="neg" select="starts-with($date-time, '-')" />
+            <xsl:variable name="no-neg">
+               <xsl:choose>
+                  <xsl:when test="$neg or starts-with($date-time, '+')">
+                     <xsl:value-of select="substring($date-time, 2)" />
+                  </xsl:when>
+                  <xsl:otherwise>
+                     <xsl:value-of select="$date-time" />
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:variable>
+            <xsl:variable name="no-neg-length" select="string-length($no-neg)" />
+            <xsl:variable name="timezone">
+               <xsl:choose>
+                  <xsl:when test="substring($no-neg, $no-neg-length) = 'Z'">Z</xsl:when>
+                  <xsl:otherwise>
+                     <xsl:variable name="tz" select="substring($no-neg, $no-neg-length - 5)" />
+                     <xsl:if test="(substring($tz, 1, 1) = '-' or 
+                                    substring($tz, 1, 1) = '+') and
+                                   substring($tz, 4, 1) = ':'">
+                        <xsl:value-of select="$tz" />
+                     </xsl:if>
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:variable>
+            <xsl:if test="not(string($timezone)) or
+                          $timezone = 'Z' or 
+                          (substring($timezone, 2, 2) &lt;= 23 and
+                           substring($timezone, 5, 2) &lt;= 59)">
+               <xsl:variable name="dt" select="substring($no-neg, 1, $no-neg-length - string-length($timezone))" />
+               <xsl:variable name="dt-length" select="string-length($dt)" />
+               <xsl:choose>
+                  <xsl:when test="substring($dt, 3, 1) = ':' and
+                                  substring($dt, 6, 1) = ':'">
+                     <xsl:variable name="hour" select="substring($dt, 1, 2)" />
+                     <xsl:variable name="min" select="substring($dt, 4, 2)" />
+                     <xsl:variable name="sec" select="substring($dt, 7)" />
+                     <xsl:if test="$hour &lt;= 23 and
+                                   $min &lt;= 59 and
+                                   $sec &lt;= 60">
+                        <xsl:call-template name="date:_format-date">
+                           <xsl:with-param name="year" select="'NaN'" />
+                           <xsl:with-param name="month" select="'NaN'" />
+                           <xsl:with-param name="day" select="'NaN'" />
+                           <xsl:with-param name="hour" select="$hour" />
+                           <xsl:with-param name="minute" select="$min" />
+                           <xsl:with-param name="second" select="$sec" />
+                           <xsl:with-param name="timezone" select="$timezone" />
+                           <xsl:with-param name="pattern" select="$pattern" />
+                        </xsl:call-template>
+                     </xsl:if>
+                  </xsl:when>
+                  <xsl:otherwise>
+                     <xsl:variable name="year" select="substring($dt, 1, 4) * (($neg * -2) + 1)" />
+                     <xsl:choose>
+                        <xsl:when test="not(number($year))" />
+                        <xsl:when test="$dt-length = 4">
+                           <xsl:call-template name="date:_format-date">
+                              <xsl:with-param name="year" select="$year" />
+                              <xsl:with-param name="timezone" select="$timezone" />
+                              <xsl:with-param name="pattern" select="$pattern" />
+                           </xsl:call-template>
+                        </xsl:when>
+                        <xsl:when test="substring($dt, 5, 1) = '-'">
+                           <xsl:variable name="month" select="substring($dt, 6, 2)" />
+                           <xsl:choose>
+                              <xsl:when test="not($month &lt;= 12)" />
+                              <xsl:when test="$dt-length = 7">
+                                 <xsl:call-template name="date:_format-date">
+                                    <xsl:with-param name="year" select="$year" />
+                                    <xsl:with-param name="month" select="$month" />
+                                    <xsl:with-param name="timezone" select="$timezone" />
+                                    <xsl:with-param name="pattern" select="$pattern" />
+                                 </xsl:call-template>
+                              </xsl:when>
+                              <xsl:when test="substring($dt, 8, 1) = '-'">
+                                 <xsl:variable name="day" select="substring($dt, 9, 2)" />
+                                 <xsl:if test="$day &lt;= 31">
+                                    <xsl:choose>
+                                       <xsl:when test="$dt-length = 10">
+                                          <xsl:call-template name="date:_format-date">
+                                             <xsl:with-param name="year" select="$year" />
+                                             <xsl:with-param name="month" select="$month" />
+                                             <xsl:with-param name="day" select="$day" />
+                                             <xsl:with-param name="timezone" select="$timezone" />
+                                             <xsl:with-param name="pattern" select="$pattern" />
+                                          </xsl:call-template>
+                                       </xsl:when>
+                                       <xsl:when test="substring($dt, 11, 1) = 'T' and
+                                                       substring($dt, 14, 1) = ':' and
+                                                       substring($dt, 17, 1) = ':'">
+                                          <xsl:variable name="hour" select="substring($dt, 12, 2)" />
+                                          <xsl:variable name="min" select="substring($dt, 15, 2)" />
+                                          <xsl:variable name="sec" select="substring($dt, 18)" />
+                                          <xsl:if test="$hour &lt;= 23 and
+                                                        $min &lt;= 59 and
+                                                        $sec &lt;= 60">
+                                             <xsl:call-template name="date:_format-date">
+                                                <xsl:with-param name="year" select="$year" />
+                                                <xsl:with-param name="month" select="$month" />
+                                                <xsl:with-param name="day" select="$day" />
+                                                <xsl:with-param name="hour" select="$hour" />
+                                                <xsl:with-param name="minute" select="$min" />
+                                                <xsl:with-param name="second" select="$sec" />
+                                                <xsl:with-param name="timezone" select="$timezone" />
+                                                <xsl:with-param name="pattern" select="$pattern" />
+                                             </xsl:call-template>
+                                          </xsl:if>
+                                       </xsl:when>
+                                    </xsl:choose>
+                                 </xsl:if>
+                              </xsl:when>
+                           </xsl:choose>
+                        </xsl:when>
+                     </xsl:choose>
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:if>
+         </xsl:otherwise>
+      </xsl:choose>
+   </xsl:variable>
+   <xsl:value-of select="$formatted" />   
+</xsl:template>
+
+<xsl:template name="date:_format-date">
+   <xsl:param name="year" />
+   <xsl:param name="month" select="1" />
+   <xsl:param name="day" select="1" />
+   <xsl:param name="hour" select="0" />
+   <xsl:param name="minute" select="0" />
+   <xsl:param name="second" select="0" />
+   <xsl:param name="timezone" select="'Z'" />
+   <xsl:param name="pattern" select="''" />
+   <xsl:variable name="char" select="substring($pattern, 1, 1)" />
+   <xsl:choose>
+      <xsl:when test="not($pattern)" />
+      <xsl:when test='$char = "&apos;"'>
+         <xsl:choose>
+            <xsl:when test='substring($pattern, 2, 1) = "&apos;"'>
+               <xsl:text>&apos;</xsl:text>
+               <xsl:call-template name="date:_format-date">
+                  <xsl:with-param name="year" select="$year" />
+                  <xsl:with-param name="month" select="$month" />
+                  <xsl:with-param name="day" select="$day" />
+                  <xsl:with-param name="hour" select="$hour" />
+                  <xsl:with-param name="minute" select="$minute" />
+                  <xsl:with-param name="second" select="$second" />
+                  <xsl:with-param name="timezone" select="$timezone" />
+                  <xsl:with-param name="pattern" select="substring($pattern, 3)" />
+               </xsl:call-template>
+            </xsl:when>
+            <xsl:otherwise>
+               <xsl:variable name="literal-value" select='substring-before(substring($pattern, 2), "&apos;")' />
+               <xsl:value-of select="$literal-value" />
+               <xsl:call-template name="date:_format-date">
+                  <xsl:with-param name="year" select="$year" />
+                  <xsl:with-param name="month" select="$month" />
+                  <xsl:with-param name="day" select="$day" />
+                  <xsl:with-param name="hour" select="$hour" />
+                  <xsl:with-param name="minute" select="$minute" />
+                  <xsl:with-param name="second" select="$second" />
+                  <xsl:with-param name="timezone" select="$timezone" />
+                  <xsl:with-param name="pattern" select="substring($pattern, string-length($literal-value) + 2)" />
+               </xsl:call-template>
+            </xsl:otherwise>
+         </xsl:choose>
+      </xsl:when>
+      <xsl:when test="not(contains('abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $char))">
+         <xsl:value-of select="$char" />
+         <xsl:call-template name="date:_format-date">
+            <xsl:with-param name="year" select="$year" />
+            <xsl:with-param name="month" select="$month" />
+            <xsl:with-param name="day" select="$day" />
+            <xsl:with-param name="hour" select="$hour" />
+            <xsl:with-param name="minute" select="$minute" />
+            <xsl:with-param name="second" select="$second" />
+            <xsl:with-param name="timezone" select="$timezone" />
+            <xsl:with-param name="pattern" select="substring($pattern, 2)" />
+         </xsl:call-template>
+      </xsl:when>
+      <xsl:when test="not(contains('GyMdhHmsSEDFwWakKz', $char))">
+         <xsl:message>
+            Invalid token in format string: <xsl:value-of select="$char" />
+         </xsl:message>
+         <xsl:call-template name="date:_format-date">
+            <xsl:with-param name="year" select="$year" />
+            <xsl:with-param name="month" select="$month" />
+            <xsl:with-param name="day" select="$day" />
+            <xsl:with-param name="hour" select="$hour" />
+            <xsl:with-param name="minute" select="$minute" />
+            <xsl:with-param name="second" select="$second" />
+            <xsl:with-param name="timezone" select="$timezone" />
+            <xsl:with-param name="pattern" select="substring($pattern, 2)" />
+         </xsl:call-template>
+      </xsl:when>
+      <xsl:otherwise>
+         <xsl:variable name="next-different-char" select="substring(translate($pattern, $char, ''), 1, 1)" />
+         <xsl:variable name="pattern-length">
+            <xsl:choose>
+               <xsl:when test="$next-different-char">
+                  <xsl:value-of select="string-length(substring-before($pattern, $next-different-char))" />
+               </xsl:when>
+               <xsl:otherwise>
+                  <xsl:value-of select="string-length($pattern)" />
+               </xsl:otherwise>
+            </xsl:choose>
+         </xsl:variable>
+         <xsl:choose>
+            <xsl:when test="$char = 'G'">
+               <xsl:choose>
+                  <xsl:when test="string($year) = 'NaN'" />
+                  <xsl:when test="$year > 0">AD</xsl:when>
+                  <xsl:otherwise>BC</xsl:otherwise>
+               </xsl:choose>
+            </xsl:when>
+            <xsl:when test="$char = 'M'">
+               <xsl:choose>
+                  <xsl:when test="string($month) = 'NaN'" />
+                  <xsl:when test="$pattern-length >= 3">
+                     <xsl:variable name="month-node" select="document('')/*/date:months/date:month[number($month)]" />
+                     <xsl:choose>
+                        <xsl:when test="$pattern-length >= 4">
+                           <xsl:value-of select="$month-node" />
+                        </xsl:when>
+                        <xsl:otherwise>
+                           <xsl:value-of select="$month-node/@abbr" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$pattern-length = 2">
+                     <xsl:value-of select="format-number($month, '00')" />
+                  </xsl:when>
+                  <xsl:otherwise>
+                     <xsl:value-of select="$month" />
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:when>
+            <xsl:when test="$char = 'E'">
+               <xsl:choose>
+                  <xsl:when test="string($year) = 'NaN' or string($month) = 'NaN' or string($day) = 'NaN'" />
+                  <xsl:otherwise>
+                     <xsl:variable name="month-days" select="sum(document('')/*/date:months/date:month[position() &lt; $month]/@length)" />
+                     <xsl:variable name="days" select="$month-days + $day + boolean(((not(boolean($year mod 4)) and $year mod 100) or not(boolean($year mod 400))) and $month > 2)" />
+                     <xsl:variable name="y-1" select="$year - 1" />
+                     <xsl:variable name="dow"
+                                   select="(($y-1 + floor($y-1 div 4) -
+                                             floor($y-1 div 100) + floor($y-1 div 400) +
+                                             $days) 
+                                            mod 7) + 1" />
+                     <xsl:variable name="day-node" select="document('')/*/date:days/date:day[number($dow)]" />
+                     <xsl:choose>
+                        <xsl:when test="$pattern-length >= 4">
+                           <xsl:value-of select="$day-node" />
+                        </xsl:when>
+                        <xsl:otherwise>
+                           <xsl:value-of select="$day-node/@abbr" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:when>
+            <xsl:when test="$char = 'a'">
+               <xsl:choose>
+                  <xsl:when test="string($hour) = 'NaN'" />
+                  <xsl:when test="$hour >= 12">PM</xsl:when>
+                  <xsl:otherwise>AM</xsl:otherwise>
+               </xsl:choose>
+            </xsl:when>
+            <xsl:when test="$char = 'z'">
+               <xsl:choose>
+                  <xsl:when test="$timezone = 'Z'">UTC</xsl:when>
+                  <xsl:otherwise>UTC<xsl:value-of select="$timezone" /></xsl:otherwise>
+               </xsl:choose>
+            </xsl:when>
+            <xsl:otherwise>
+               <xsl:variable name="padding">
+                  <xsl:choose>
+                     <xsl:when test="$pattern-length > 10">
+                        <xsl:call-template name="str:padding">
+                           <xsl:with-param name="length" select="$pattern-length" />
+                           <xsl:with-param name="chars" select="'0'" />
+                        </xsl:call-template>
+                     </xsl:when>
+                     <xsl:otherwise>
+                        <xsl:value-of select="substring('0000000000', 1, $pattern-length)" />
+                     </xsl:otherwise>
+                  </xsl:choose>
+               </xsl:variable>
+               <xsl:choose>
+                  <xsl:when test="$char = 'y'">
+                     <xsl:choose>
+                        <xsl:when test="string($year) = 'NaN'" />
+                        <xsl:when test="$pattern-length > 2"><xsl:value-of select="format-number($year, $padding)" /></xsl:when>
+                        <xsl:otherwise><xsl:value-of select="format-number(substring($year, string-length($year) - 1), $padding)" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'd'">
+                     <xsl:choose>
+                        <xsl:when test="string($day) = 'NaN'" />
+                        <xsl:otherwise><xsl:value-of select="format-number($day, $padding)" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'h'">
+                     <xsl:variable name="h" select="$hour mod 12" />
+                     <xsl:choose>
+                        <xsl:when test="string($hour) = 'NaN'"></xsl:when>
+                        <xsl:when test="$h"><xsl:value-of select="format-number($h, $padding)" /></xsl:when>
+                        <xsl:otherwise><xsl:value-of select="format-number(12, $padding)" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'H'">
+                     <xsl:choose>
+                        <xsl:when test="string($hour) = 'NaN'" />
+                        <xsl:otherwise>
+                           <xsl:value-of select="format-number($hour, $padding)" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'k'">
+                     <xsl:choose>
+                        <xsl:when test="string($hour) = 'NaN'" />
+                        <xsl:when test="$hour"><xsl:value-of select="format-number($hour, $padding)" /></xsl:when>
+                        <xsl:otherwise><xsl:value-of select="format-number(24, $padding)" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'K'">
+                     <xsl:choose>
+                        <xsl:when test="string($hour) = 'NaN'" />
+                        <xsl:otherwise><xsl:value-of select="format-number($hour mod 12, $padding)" /></xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'm'">
+                     <xsl:choose>
+                        <xsl:when test="string($minute) = 'NaN'" />
+                        <xsl:otherwise>
+                           <xsl:value-of select="format-number($minute, $padding)" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 's'">
+                     <xsl:choose>
+                        <xsl:when test="string($second) = 'NaN'" />
+                        <xsl:otherwise>
+                           <xsl:value-of select="format-number($second, $padding)" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'S'">
+                     <xsl:choose>
+                        <xsl:when test="string($second) = 'NaN'" />
+                        <xsl:otherwise>
+                           <xsl:value-of select="format-number(substring-after($second, '.'), $padding)" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="$char = 'F'">
+                     <xsl:choose>
+                        <xsl:when test="string($day) = 'NaN'" />
+                        <xsl:otherwise>
+                           <xsl:value-of select="floor($day div 7) + 1" />
+                        </xsl:otherwise>
+                     </xsl:choose>
+                  </xsl:when>
+                  <xsl:when test="string($year) = 'NaN' or string($month) = 'NaN' or string($day) = 'NaN'" />
+                  <xsl:otherwise>
+                     <xsl:variable name="month-days" select="sum(document('')/*/date:months/date:month[position() &lt; $month]/@length)" />
+                     <xsl:variable name="days" select="$month-days + $day + boolean(((not($year mod 4) and $year mod 100) or not($year mod 400)) and $month > 2)" />
+                     <xsl:choose>
+                        <xsl:when test="$char = 'D'">
+                           <xsl:value-of select="format-number($days, $padding)" />
+                        </xsl:when>
+                        <xsl:when test="$char = 'w'">
+                           <xsl:call-template name="date:_week-in-year">
+                              <xsl:with-param name="days" select="$days" />
+                              <xsl:with-param name="year" select="$year" />
+                           </xsl:call-template>
+                        </xsl:when>
+                        <xsl:when test="$char = 'W'">
+                           <xsl:variable name="y-1" select="$year - 1" />
+                           <xsl:variable name="day-of-week" 
+                                         select="(($y-1 + floor($y-1 div 4) -
+                                                  floor($y-1 div 100) + floor($y-1 div 400) +
+                                                  $days) 
+                                                  mod 7) + 1" />
+                           <xsl:choose>
+                              <xsl:when test="($day - $day-of-week) mod 7">
+                                 <xsl:value-of select="floor(($day - $day-of-week) div 7) + 2" />
+                              </xsl:when>
+                              <xsl:otherwise>
+                                 <xsl:value-of select="floor(($day - $day-of-week) div 7) + 1" />
+                              </xsl:otherwise>
+                           </xsl:choose>
+                        </xsl:when>
+                     </xsl:choose>
+                  </xsl:otherwise>
+               </xsl:choose>
+            </xsl:otherwise>
+         </xsl:choose>
+         <xsl:call-template name="date:_format-date">
+            <xsl:with-param name="year" select="$year" />
+            <xsl:with-param name="month" select="$month" />
+            <xsl:with-param name="day" select="$day" />
+            <xsl:with-param name="hour" select="$hour" />
+            <xsl:with-param name="minute" select="$minute" />
+            <xsl:with-param name="second" select="$second" />
+            <xsl:with-param name="timezone" select="$timezone" />
+            <xsl:with-param name="pattern" select="substring($pattern, $pattern-length + 1)" />
+         </xsl:call-template>
+      </xsl:otherwise>
+   </xsl:choose>
+</xsl:template>
+
+<xsl:template name="date:_week-in-year">
+   <xsl:param name="days" />
+   <xsl:param name="year" />
+   <xsl:variable name="y-1" select="$year - 1" />
+   <!-- this gives the day of the week, counting from Sunday = 0 -->
+   <xsl:variable name="day-of-week" 
+                 select="($y-1 + floor($y-1 div 4) -
+                          floor($y-1 div 100) + floor($y-1 div 400) +
+                          $days) 
+                         mod 7" />
+   <!-- this gives the day of the week, counting from Monday = 1 -->
+   <xsl:variable name="dow">
+      <xsl:choose>
+         <xsl:when test="$day-of-week"><xsl:value-of select="$day-of-week" /></xsl:when>
+         <xsl:otherwise>7</xsl:otherwise>
+      </xsl:choose>
+   </xsl:variable>
+   <xsl:variable name="start-day" select="($days - $dow + 7) mod 7" />
+   <xsl:variable name="week-number" select="floor(($days - $dow + 7) div 7)" />
+   <xsl:choose>
+      <xsl:when test="$start-day >= 4">
+         <xsl:value-of select="$week-number + 1" />
+      </xsl:when>
+      <xsl:otherwise>
+         <xsl:choose>
+            <xsl:when test="not($week-number)">
+               <xsl:call-template name="date:_week-in-year">
+                  <xsl:with-param name="days" select="365 + ((not($y-1 mod 4) and $y-1 mod 100) or not($y-1 mod 400))" />
+                  <xsl:with-param name="year" select="$y-1" />
+               </xsl:call-template>
+            </xsl:when>
+            <xsl:otherwise>
+               <xsl:value-of select="$week-number" />
+            </xsl:otherwise>
+         </xsl:choose>
+      </xsl:otherwise>
+   </xsl:choose>
+</xsl:template>
+
+</xsl:stylesheet>
Index: ccr2ccd-xslt/trunk/lib/str.padding.template.xsl
===================================================================
--- ccr2ccd-xslt/trunk/lib/str.padding.template.xsl	(revision 748)
+++ ccr2ccd-xslt/trunk/lib/str.padding.template.xsl	(revision 748)
@@ -0,0 +1,31 @@
+<?xml version="1.0"?>
+<xsl:stylesheet version="1.0"
+                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                xmlns:str="http://exslt.org/strings"
+                extension-element-prefixes="str">
+
+<xsl:template name="str:padding">
+	<xsl:param name="length" select="0" />
+   <xsl:param name="chars" select="' '" />
+   <xsl:choose>
+      <xsl:when test="not($length) or not($chars)" />
+      <xsl:otherwise>
+         <xsl:variable name="string" 
+                       select="concat($chars, $chars, $chars, $chars, $chars, 
+                                      $chars, $chars, $chars, $chars, $chars)" />
+         <xsl:choose>
+            <xsl:when test="string-length($string) >= $length">
+               <xsl:value-of select="substring($string, 1, $length)" />
+            </xsl:when>
+            <xsl:otherwise>
+               <xsl:call-template name="str:padding">
+                  <xsl:with-param name="length" select="$length" />
+                  <xsl:with-param name="chars" select="$string" />
+               </xsl:call-template>
+            </xsl:otherwise>
+         </xsl:choose>
+      </xsl:otherwise>
+   </xsl:choose>
+</xsl:template>
+
+</xsl:stylesheet>
