XML technologies / XSLT / XSLT Cookbook / Dates and times / Determine secular and religious holidays

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to know whether a given date is a holiday.

Solution

The first category comprises holidays falling on the same date every year. For example, determining US Independence Day for any year is simple:

<xsl:template name="ckbk:independence-day">
  <xsl:param name="year"/>
  <xsl:call-template name="ckbk:date-to-absolute-day">
    <xsl:with-param name="month" select="7"/>
    <xsl:with-param name="day" select="4"/>
    <xsl:with-param name="year" select="$year"/>
  </xsl:call-template>
</xsl:template>

The second category falls on a fixed weekday relative to the start or end of a month. Determine these dates with the following small program, which uses the template k-day-on-or-before-abs-day from recipe Work with the Islamic calendar:

<xsl:template name="ckbk:n-th-k-day">
  <!-- Das n-te Vorkommen von k im angegebenen Monat -->
  <!-- Positive n-Werte vom Anfang des Monats, negative vom Ende. -->
  <xsl:param name="n"/>
  <!-- k = der Wochentag (0 = Sonntag) -->
  <xsl:param name="k"/>
  <xsl:param name="month"/>
  <xsl:param name="year"/>
  <xsl:choose>
    <xsl:when test="$n &gt; 0">
      <xsl:variable name="k-day-on-or-before">
        <xsl:variable name="abs-day">
          <xsl:call-template name="ckbk:date-to-absolute-day">
            <xsl:with-param name="month" select="$month"/>
            <xsl:with-param name="day" select="7"/>
            <xsl:with-param name="year" select="$year"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="ckbk:k-day-on-or-before-abs-day">
          <xsl:with-param name="abs-day" select="$abs-day"/>
          <xsl:with-param name="k" select="$k"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$k-day-on-or-before + 7 * ($n - 1)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="k-day-on-or-before">
        <xsl:variable name="abs-day">
          <xsl:call-template name="ckbk:date-to-absolute-day">
            <xsl:with-param name="month" select="$month"/>
            <xsl:with-param name="day">
              <xsl:call-template name="ckbk:last-day-of-month">
                <xsl:with-param name="month" select="$month"/>
                <xsl:with-param name="year" select="$year"/>
              </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="year" select="$year"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="ckbk:k-day-on-or-before-abs-day">
          <xsl:with-param name="abs-day" select="$abs-day"/>
          <xsl:with-param name="k" select="$k"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$k-day-on-or-before + 7 * ($n + 1)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This function assumes the Gregorian calendar. To determine relative dates in other calendar systems, write equivalent routines within those systems.

It is now easy to determine US Labor Day and Memorial Day, the first Monday in September and the last Monday in May respectively:

<xsl:template name="ckbk:labor-day">
  <xsl:param name="year"/>
  <xsl:call-template name="ckbk:n-th-k-day ">
    <xsl:with-param name="n" select="1"/>
    <xsl:with-param name="k" select="1"/>
    <xsl:with-param name="month" select="9"/>
    <xsl:with-param name="year" select="$year"/>
  </xsl:call-template>
</xsl:template>
<xsl:template name="ckbk:memorial-day">
  <xsl:param name="year"/>
  <xsl:call-template name="ckbk:n-th-k-day ">
    <xsl:with-param name="n" select="-1"/>
    <xsl:with-param name="k" select="1"/>
    <xsl:with-param name="month" select="5"/>
    <xsl:with-param name="year" select="$year"/>
  </xsl:call-template>
</xsl:template>

Although not holidays, the start and end dates of US daylight saving time can be handled in the same way:

<xsl:template name="ckbk:day-light-savings-start">
  <xsl:param name="year"/>
  <xsl:call-template name="ckbk:n-th-k-day ">
    <xsl:with-param name="n" select="1"/>
    <xsl:with-param name="k" select="0"/>
    <xsl:with-param name="month" select="4"/>
    <xsl:with-param name="year" select="$year"/>
  </xsl:call-template>
</xsl:template>
<xsl:template name="ckbk:day-light-savings-end">
  <xsl:param name="year"/>
  <xsl:call-template name="ckbk:n-th-k-day ">
    <xsl:with-param name="n" select="-1"/>
    <xsl:with-param name="k" select="0"/>
    <xsl:with-param name="month" select="10"/>
    <xsl:with-param name="year" select="$year"/>
  </xsl:call-template>
</xsl:template>

Discussion

Covering every secular and religious holiday in every country and year is impossible. Most, however, fall into two categories: those on the same date each year in their own calendar, such as US Independence Day, and those on a particular weekday relative to the start or end of a month, such as US Labor Day. Religious holidays are often easy to find in their own calendar. Converting to another calendar is often harder. For example, Orthodox Christmas always falls on December 25 in the Julian calendar. In a Gregorian year it may therefore occur at the beginning, at the end, or not at all. Since we cannot cover every religious holiday in every faith, consult the references listed in this chapter’s introduction.

  

 

 

 

back to top