XML technologies / XSLT / XSLT Cookbook / Dates and times
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
"Does anyone really know what time it is? Does anyone really care?"
Chicago
Introduction
Native XSLT 1.0 does not know the time and seems not to care. Dates and times are nevertheless essential in everyday life, and the need to manipulate them keeps growing in computing, especially web development. It was therefore surprising and regrettable that standard XSLT 1.0 had no built-in date and time support.
In XSLT 2.0, the situation has improved considerably. XPath 2.0 provides many functions for manipulating dates, times and durations. In fact, shorter function names are about the only remaining wish! I was told that names such as subtract-dates-yielding-yearMonthDuration( ) were chosen partly to satisfy XQuery database enthusiasts. The alternation between hyphens and mixed case comes from differing conventions in the XPath and XML Schema committees.
This chapter’s examples help compensate for XSLT 1.0’s missing date and time support. Unfortunately, one essential function cannot be implemented in XSLT 1.0: obtaining the current date and time. You must use another language whose library can access the hardware clock. Both Java and JavaScript can do this. If you only need to format dates and times already in a document, these routines should meet most needs. In 2.0, use current-date( ), current-time( ) and current-dateTime( ).
Manipulating and converting dates and times without native support can be complicated. Ultimately, though, it is an exercise in intricate integer arithmetic, carrying out essentially simple conversions in a mixed-radix system. Non-Gregorian calendars and holiday calculations require historical, religious and cultural knowledge. Readers with no use for date and time routines will probably skip this chapter, since little XSLT technique is unique to these algorithms. Anyone interested in the theory should consult the articles in See also.
I thank Jason Diamond for generously contributing many templates for Gregorian date calculations. The XSLT code for non-Gregorian calendars was adapted from Edward M. Reingold’s public-domain Lisp implementation. Some algorithms were adjusted to fit XSLT better and build on Jason’s code.
Do not be confused by the time-passing technique used in most templates. It is designed for maximum convenience. Pass a date either as an ISO-formatted date-time string or as separate year, month and day parameters. This example illustrates the options:
<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 call computes the weekday for 1 January 2002. The first two forms suit dates already in ISO format; the last suits separately stored components. Parts of an ISO date string can also be overridden 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>Unless stated otherwise, all templates use Gregorian dates. This calendar is used throughout most of the Western world.
See also
Jason Diamond’s original XSLT implementation is available at Date/Time Processing.
This chapter does not cover Chinese or Indian calendars because they are more complex. For calculations with these calendars, see The Mathematics of the Chinese Calendar and The Indian Calendars.
EXSLT.org has a date-time module offering almost the same functionality as this chapter’s Gregorian templates, plus templates for durations: differences between dates and times.
XForms offers some date-time functionality using lexical XML Schema representations (ISO), with durations split into dateTime and yearMonth. The new functions are: now( ), days-from-date( ), seconds-from-dateTime( ), seconds( ) and months( ).
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de