XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (343 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
5.95Mb size Format: txt, pdf, ePub

Duration Times Number

An
xs:dayTimeDuration
or
xs:yearMonthDuration
can be multiplied or divided by a number, to give another duration of the same type. The operand combinations are:

Operand 1
Operand 2
Result
xs:yearMonthDuration
xs:double
xs:float
xs:decimal
xs:yearMonthDuration
xs:dayTimeDuration
xs:double
xs:float
xs:decimal
xs:dayTimeDuration

The operator can be
*
(multiply) or
div
(divide). If the operator is
*
, the operands can appear in either order; if it is
div
, then the numeric operand must be the second operand.

The effect of the operation is equivalent to converting the duration to a number of months or seconds, performing a numeric multiplication or division, and then converting the result back to a duration.

Date/Time Minus Date/Time

The problem of comparing dates and times is illustrated nicely by the following letter published in
The Times
of London, which we will return to later:

This week the wives of two of my nephews have given birth to sons. The first was born at 12.11 am on July 10 in Melbourne; the second arrived at 9.40 pm on July 9 in London. I am confused as to which is the older. (John Wayman, Sudbury, Suffolk. 13 July 2007)

The subtraction operator
-
can be used to subtract date/time values to give a duration, provided the operands match one of the rows in the following table:

Operand 1
Operand 2
Result
xs:date
xs:date
xs:dayTimeDuration
xs:dateTime
xs:dateTime
xs:dayTimeDuration
xs:time
xs:time
xs:dayTimeDuration

If the first operand represents an instant in time later than the second operand, then the result will be a positive duration; if it represents an earlier instant in time, then the result will be a negative duration.

If the date/time was supplied without a timezone, then it is assumed to represent a date/time in the implicit timezone defined by the evaluation context (see Chapter 7).

For operands of type
xs:dateTime
, the result is the duration corresponding to the number of seconds that separate the two instants in time, taking timezone into account. For example, the difference between the ages of John Wayman's two great-nephews is

xs:dateTime(‘2007-07-09T21:40:00+01:00’) -

         xs:dateTime(‘2007-07-10T00:11:00+10:00’)

which makes the British child 6 hours and 29 minutes younger than his Australian cousin, despite having an earlier date on his birth certificate.

For operands of type
xs:date
, the result is the difference between the starting instants of the two dates. Since the dates can be in different timezones, the result is not necessarily an integer number of days.

For operands of type
xs:time
, the values are assumed to represent two times occurring on the same date. If the times are in the same timezone (or both in no timezone), this gives results that are reasonably intuitive—except that
01:00:00
minus
22:00:00
is not 3 hours, but −21 hours. With different timezones, the answers can be quite surprising. For example the difference between
22:00:00-05:00
(10 p.m in New York) and
01:00:00Z
(1 a.m in London) is the same as the difference between
2008-01-01 T22:00:00-05:00
and
2008-01-01 T01:00:00Z
, namely 26 hours.

A working draft of the XPath 2.0 specification included functions to compute the difference between two dates in months. These functions were dropped because of difficulties in defining exactly what they should return. What's the difference in months, for example, between 29 Feb 2008 and 31 Jan 2008?

Duration Divided by Duration

It is possible to divide an
xs:dayTimeDuration
by another
xs:dayTimeDuration
, or an
xs:yearMonthDuration
by another
xs:yearMonthDuration
, to obtain an
xs:double
. The division operator must be
div
(
idiv
is not supported). The result is equivalent to converting both the durations into a number of months or seconds and performing a numeric division.

Other books

Retro Demonology by Jana Oliver
VA 2 - Blood Jewel by Georgia Cates
The Unexpected Honeymoon by Barbara Wallace
Loving Sofia by Alina Man
Camelot & Vine by Petrea Burchard
Winner Takes All by Jenny Santana
Acoustic Shadows by Patrick Kendrick