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

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

All these functions are described in detail in Chapter 13. The syntax of regular expressions is described in Chapter 14.

xs:time

The
xs:time
type represents a time of day, for example, 12:15:00. Like an
xs:dateTime
, it can represent the fractional number of seconds to an arbitrary precision determined by the implementation (at least three decimal digits are required), and it can optionally include a timezone. A time with a timezone is written, for example, as
12:15:00+01:00
to indicate a timezone one hour ahead of UTC (as used in much of continental Europe during the winter months, and in Britain during the summer).

Like
xs:date
and
xs:dateTime
values,
xs:time
values without an explicit timezone are assumed to apply to an implicit timezone taken from the evaluation context.

Operations you can perform on an
xs:time
include:

  • Comparing and sorting times
  • Converting times to and from strings
  • Extracting the component parts of a time (hour, minutes, seconds, timezone)
  • Adding a duration to a time (or subtracting a duration) to get another time
  • Determining the difference between two times, as a duration
  • Combining the time with a date to create an
    xs:dateTime
  • Adjusting the timezone: that is, creating an equivalent time with or without a timezone, or with a different timezone (see the
    adjust-time-to-timezone()
    function on page 715)

Although timezones are complex enough already, one problem that the XPath model doesn't tackle is daylight savings time (also known as summer time). If you want to use
xs:time
values to represent, say, a schedule of flights departing from Logan Airport in Boston, then you probably want to use the value
13:15:00
to mean “a quarter past one, in Boston's time zone”. Specifying this as
13:15:00-05:00
would be incorrect, because for half the year Boston is five hours behind UTC, and for the other half it is only four hours behind. My recommendation in this situation would be not to store a timezone with the value itself, but to use some other way of representing the information (for example, a
timezone
attribute on the containing element). Alternatively, it might be better to hold all times internally in UTC (sometimes called Zulu time) and only convert them to a local timezone for display purposes.

A particular problem that is unique to
xs:time
values is comparison and sorting, because the natural ordering is cyclic. For example, most people would agree that
18:00:00
is before
23:59:00
, but is it before
00:00:00
? And is
20:00:00-05:00
(8 p.m. in New York) before or after
00:30:00Z
(half past midnight in London)?

There is no correct answer to this question, but the rule that XPath has adopted is chosen to give the least number of surprises. The rule is to take an arbitrary date (the example used in the spec is 1972-12-31), and treat both times as representing
xs:dateTime
values on this date; then compare the resulting
xs:dateTime
values. So
18:00:00-05:00
(6 p.m. in New York) is before
22:00:00-05:00
(10 p.m. in New York) because
1972-12-31 T18:00:00-05:00
was earlier than
1972-12-31 T22:00:00-05:00
. Less predictably,
20:00:00-05:00
(8 p.m. in New York) is after
00:30:00Z
(half past midnight in London) because
1972-12-31 T20:00:00-05:00
is the same instant as
1973-01-01 T01:00:00Z
, which is well after
1972-12-31 T00:30:00Z
.

xs:dayTimeDuration and xs:yearMonthDuration

XML Schema provides a primitive type
xs:duration
, which we will discuss briefly on page 208. A duration represents a period of time, expressed in years, months, days, hours, minutes, seconds, and fractions of a second.

Durations that mix these different units are difficult to handle because the length of a month is variable. For example, what should be the result of comparing a duration of one month with a duration of 30 days? XML Schema addresses this problem by defining a partial ordering for durations, which means that some durations are clearly longer than others, but for some pairs of durations (like the example just cited), the relative magnitude is undefined.

The idea of a partial ordering makes life rather difficult for a language like XPath. Operations like
=
and
<
need to produce a yes-or-no answer, introducing a “maybe” would complicate the language immensely. For this reason, XPath decided to introduce two new duration types, which are defined as subtypes of
xs:duration
. The
xs:dayTimeDuration
handles durations expressed in days, hours, minutes, seconds, and fractions of a second, while
xs:yearMonthDuration
handles durations in years and months. These behave much more cleanly: an
xs:dayTimeDuration
is just a decimal number of seconds, and an
xs:yearMonthDuration
is just an integer number of months.

Other books

The Young Dread by Arwen Elys Dayton
No Apologies by Jamie Dossie
Be Good by Dakota Madison
On the Rocks by Erin Duffy
Hunting for Hidden Gold by Franklin W. Dixon
Prisoners of the North by Pierre Berton