Re: [xsl] Re: fiscal year calculation in XSL?

Subject: Re: [xsl] Re: fiscal year calculation in XSL?
From: "Mike Haarman" <mhaarman@xxxxxxxxxxxxxxxxxx>
Date: Tue, 17 Jun 2003 15:29:42 -0500
From: "Boris Kortiak" <boriskor@xxxxxxxxxxxxxxxx>

> Is there a function which determines the day of the week in
> the target API?


FWIW, the following is an implementation of Mike Keith's day-of-week algorithm
in XSLT.  A discussion is here:

http://users.aol.com/s6sj7gt/mikecal.htm

The second template in the stylesheet determines whether a given year is a leap
year.  I think beyond these two bits, it can be argued that any given calendar
problem is a glorified grouping problem.

Mr. Carlisle posted a message to this list some years ago which has been my
starting point for solving calendar problems.

http://www.biglist.com/lists/xsl-list/archives/199909/msg00056.html

*** dateTools.xsl ***
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <!-- weekDay template is passed a parameter *date* -->
  <!-- in ISO 8601 format YYYY-MM-DD and returns the -->
  <!-- sequence of the day of the week, where *0* is -->
  <!-- Sunday and *6* is Saturday.                   -->

  <xsl:template name="weekDay">
    <xsl:param name="date"/>

    <!-- variables -->
    <xsl:variable name="day">
      <xsl:choose>
        <xsl:when
          test="substring($date, 9, 1) = '0'">
          <xsl:value-of
            select="substring($date, 10, 1)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of
            select="substring($date, 9, 2)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="month">
      <xsl:choose>
        <xsl:when
          test="substring($date, 6, 1) = '0'">
          <xsl:value-of
            select="substring($date, 7, 1)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of
            select="substring($date, 6, 2)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="year">
      <xsl:value-of
        select="substring($date, 1, 4)"/>
    </xsl:variable>

    <xsl:variable name="zyear">
      <xsl:choose>
        <xsl:when test="$month &lt; 3">
          <xsl:value-of
            select="substring($date, 1, 4) - 1"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$year"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="sub1">
      <xsl:value-of
        select="floor((23 * $month) div 9)"/>
    </xsl:variable>

    <xsl:variable name="sub2">
      <xsl:value-of
        select="floor($zyear div 4)"/>
    </xsl:variable>

    <xsl:variable name="sub3">
      <xsl:value-of
        select="floor($zyear div 100)"/>
    </xsl:variable>

    <xsl:variable name="sub4">
      <xsl:value-of
        select="floor($zyear div 400)"/>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$month &gt;= 3">
        <xsl:value-of
          select="($sub1+$day+4+$year+$sub2-$sub3+$sub4-2)mod7"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of
          select="($sub1+$day+4+$year+$sub2-$sub3+$sub4)mod7"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- leapYear template is passed a parameter *year* -->
  <!-- in ISO 8601 format YYYY and returns a boolean  -->
  <!-- true for leap years.                           -->

  <xsl:template name="leapYear">
    <xsl:param name="year"/>
    <xsl:if test="$year mod 4 = 0">
      <xsl:if test="$year mod 100 = 0">
        <xsl:if test="$year mod 400 = 0">
          <xsl:value-of select="true()"/>
        </xsl:if>
        <xsl:value-of select="false()"/>
      </xsl:if>
      <xsl:value-of select="true()"/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread