XML technologies / XSLT / XSLT Cookbook / Dates and times / Working with the ISO calendar

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need dates in the internationally standardized ISO 8601 calendar. ISO stands for International Organization for Standardization. (ISO not only sounds better, but also derives from the Greek word isos, meaning “equal”.)

Caution!
Readers familiar with XML Schema must not confuse the ISO calendar with the ISO format for standardized Gregorian dates, such as 2002-04-12 or 2002-04-12T09:26:00. Standard 8601 covers both the calendar and formatting. This recipe concerns the ISO calendar.

Solution

A foundation for ISO dates, and later certain holidays, is a function finding the absolute day of the kth day on or before a given absolute day. For example, the first Monday (k = 1) on or before January 4, 2004 (absolute day 731,584) is December 29, 2003 (absolute day 731,578):

<xsl:template name="ckbk:k-day-on-or-before-abs-day">
  <xsl:param name="abs-day"/>
  <xsl:param name="k"/>
  <xsl:value-of select="$abs-day - (($abs-day - $k) mod 7)"/>
</xsl:template>

You can now convert ISO dates to absolute days by finding the number of absolute days in previous years and adding the remaining days in the given ISO date:

<xsl:template name="ckbk:iso-date-to-absolute-day">
  <xsl:param name="iso-week"/>
  <xsl:param name="iso-day"/>
  <xsl:param name="iso-year"/>
  <xsl:variable name="jan-4-of-year">
    <xsl:call-template name="ckbk:date-to-absolute-day">
      <xsl:with-param name="year" select="$iso-year"/>
      <xsl:with-param name="month" select="1"/>
      <xsl:with-param name="day" select="4"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="days-in-prior-yrs">
    <xsl:call-template name="ckbk:k-day-on-or-before-abs-day">
      <xsl:with-param name="abs-day" select="$jan-4-of-year"/>
      <xsl:with-param name="k" select="1"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="days-in-prior-weeks-this-yr" select="7 * ($iso-week - 1)"/>
  <xsl:variable name="prior-days-this-week" select="$iso-day - 1"/>
  <xsl:value-of select="$days-in-prior-yrs + $days-in-prior-weeks-this-yr + $prior-days-this-week"/>
</xsl:template>

To convert absolute days to an ISO date, the code first estimates the year from the Gregorian calendar date minus 3 days. This may be wrong when the absolute day is January 1–3 of the next year. To correct this, it compares with January 1 of the next year using the existing code iso-date-to-absolute-day. Once the ISO year is established, week and day follow from the offset from January 1 of that year. The ISO date is returned as year-week-day. This ISO convention avoids confusion with Gregorian dates:

<xsl:template name="ckbk:absolute-day-to-iso-date">
  <xsl:param name="abs-day"/>
  <xsl:variable name="d">
    <xsl:call-template name="ckbk:absolute-day-to-date">
      <xsl:with-param name="abs-day" select="$abs-day - 3"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="approx" select="substring-before($d,'/')"/>
  <xsl:variable name="iso-year">
    <xsl:variable name="a">
      <xsl:call-template name="ckbk:iso-date-to-absolute-day">
        <xsl:with-param name="iso-week" select="1"/>
        <xsl:with-param name="iso-day" select="1"/>
        <xsl:with-param name="iso-year" select="$approx + 1"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$abs-day &gt;= $a">
        <xsl:value-of select="$approx + 1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$approx"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="ckbk:iso-week">
    <xsl:variable name="a">
      <xsl:call-template name="ckbk:iso-date-to-absolute-day">
        <xsl:with-param name="iso-week" select="1"/>
        <xsl:with-param name="iso-day" select="1"/>
        <xsl:with-param name="iso-year" select="$iso-year"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="1 + floor(($abs-day - $a) div 7)"/>
  </xsl:variable>
  <xsl:variable name="iso-day">
    <xsl:variable name="a" select="$abs-day mod 7"/>
    <xsl:choose>
      <xsl:when test="not($a)">
        <xsl:value-of select="7"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$a"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="concat($iso-year,'-W',$iso-week,'-',$iso-day)"/>
</xsl:template>

Discussion

European commercial and industrial applications often need to refer to a week of the year. The ISO calendar uses the Gregorian year, week number (1–53) and weekday number (1–7, with Monday first). A week crossing a year boundary belongs to the year containing most of its days. Thus, the first ISO week can start as late as January 4 or as early as December 29 of the previous year. Similarly, the last week can end as early as December 28 or as late as January 3 of the following year. ISO week 1 of 2004 actually started December 29, 2003! (And you thought standards simplified life!) To find the start of the ISO week, find the Monday on or before January 4.

  

 

 

 

back to top