Previous Page Next Page

Introduction

Native XSLT 1.0 does not know what time it is and does not seem to care. However, dates and times are a necessary aspect of everyday life. The need to manipulate them arises frequently in computing, especially in web development. Therefore, it was surprising and unfortunate that standard XSLT 1.0 did not have any built-in date and time support.

The situation in XSLT 2.0 has improved substantially. XPath 2.0 has numerous functions for manipulating dates, times, and durations. In fact, the only thing 2.0 leaves us wanting for, with respect to date and time, is functions with shorter names! I am told names like subtract-dates-yielding-yearMonthDuration( ) were provided partly to appease the XQuery database weenies. The alternation between dashes and camelcase follows from the differing conventions adopted by XPath and XML Schema committees.

The examples in this section can help compensate for XSLT 1.0's lack of support for dates and times. Unfortunately, one of the most crucial date and time capabilities cannot be implemented in XSLT 1.0that is, getting the current date and time. For that, you need to call out to another language whose library supports interacting with the hardware's real-time clock. Both Java and JavaScript have this capability. If your application just needs to format dates and times that already exist in a document, then the routines here should cover most needs. In 2.0, use current-date(), current-time( ), and current-dateTime( ).

Doing date and time manipulation and conversion without native support in the language can be tricky, but it is almost purely an exercise in intricate integer arithmetic involving what are essentially base conversions in a mixed radix system. Working with non-Gregorian calendars and determining holidays also requires quite a bit of historical, religious, and cultural knowledge. Readers with no application for date and time routines may wish to skip this chapter because little by way of XSLT technique is unique to these algorithms. Those who are curious about the theory behind these calculations should definitely look at the papers cited in the See Also section, next.

I am grateful to Jason Diamond for graciously contributing many of the templates dealing with Gregorian time. The XSLT code for dealing with non-Gregorian calendars was adapted from Edward M. Reingold's public domain Lisp implementation. Some of the algorithms were adapted to better suit XSLT and build upon the existing foundation provided by Jason's code.

Do not be confused by the technique used to pass a date into most templates. It is designed for maximum convenience. You can pass in the date in two ways: by using a string formatted using ISO date-time rules and by using the individual parameters for the year, month, and day. The following example should clarify usage:

<xsl:call-template name="ckbk:calculate-day-of-the-week">
     <xsl:with-param name="date-time" select="'2002-01-01T01:00:00'"/>
</xsl:call-template>
   
<xsl:call-template name="ckbk:calculate-day-of-the-week">
     <xsl:with-param name="date" select="'2002-01-01'"/>
</xsl:call-template>
   
<xsl:call-template name="ckbk:calculate-day-of-the-week">
     <xsl:with-param name="year" select="2002"/>
     <xsl:with-param name="month" select="01"/>
     <xsl:with-param name="day" select="01"/>
</xsl:call-template>

Each of the calls evaluates the day of the week for January 1, 2002. The first two variations are convenient when a date is already ISO formatted. The last variation is convenient when the components of the date are stored separately. You can also override parts of an ISO date string as follows:

<xsl:call-template name="ckbk:calculate-day-of-the-week">
     <xsl:with-param name="date" select="'2002-01-01'"/>
     <xsl:with-parm name="day" select="25"/>
</xsl:call-template>

In all templates, unless otherwise stated, dates are Gregorian by default. This civil calendar system is used by most of the Western world.

See Also

Claus Tøndering's calendars FAQ (available at http://www.pauahtun.org/CalendarFAQ/cal/calendar24.pdf) contains the illuminating theory behind many calculations in this chapter.

Jason Diamond's original XSLT implementation can be found at http://xsltsl.sourceforge.net/date-time.html.

Calendrical Calculations (http://emr.cs.iit.edu/~reingold/calendar.ps) by Nachum Dershowitz and Edward M. Reingold provides additional insight. Their paper discusses the non-Gregorian systems covered here.

I do not cover the Chinese or Indian calendars in this chapter because they are more complex. Information about the mathematics of the Chinese and Indian calendars can be found at http://www.math.nus.edu.sg/aslaksen/calendar/chinese.shtml and http://www.math.nus.edu.sg/aslaksen/calendar/indian.shtml, respectively.

If, for some reason, you have an application for the Mayan, French Revolutionary, or Old Hindu calendars, then you should investigate Calendrical Calculations II (http://emr.cs.iit.edu/~reingold/calendar2.ps).

EXSLT.org has a date-time module that provides much of the same functionality as the Gregorian date-time templates implemented in this chapter. They also provide templates for working durations (differences between dates and times).

XForms (http://www.w3.org/MarkUp/Forms/) provides some date-time functionality. It uses XML Schema (ISO) lexical representations (with durations subdivided into dateTime and yearMonth flavors). The new functions are: now( ), days-from-date( ), seconds-from-dateTime(), seconds( ), and months( ).


Previous Page Next Page