XML technologies / XML Schema / Using predefined simple data types / Date and time data types

(Excerpt from “XML Schema” by Eric van der Vlist)

The types discussed here are shown in the following figure.

Date and time data types

Figure: Date and time data types

The realm of ISO 8601

The W3C Recommendation “XML Schema Part 2: Datatypes” again confirms how difficult it is to specify times precisely.

Date and time support is entirely based on a subset of ISO 8601, the only format supported by W3C XML Schema. ISO 8601 aims to prevent confusion among countries’ different formats. In other words, W3C XML Schema does not support regional formats, requiring ISO 8601 for every type with date or time semantics. This benefits interchange but is questionable for XML-defined user interfaces: ISO 8601 is not very user-friendly. Month names and differing year/month/day orders are not the only casualties: ISO 8601 enforces the Gregorian, Christian calendar over those used by other cultures and religions.

ISO 8601 defines formats for dates, times, intervals and recurring instants with differing precision and uncertainty. After much discussion, W3C XML Schema selected a subset and created a primitive type for each supported format.

Uncertainty permitted by some formats complicates comparison and calculation. An instant may omit its time zone, leaving it unknown. That unknown zone is identical throughout a document, its schema and matching instances, so comparing two instants without zones is straightforward. Comparing one with a zone and one without is harder: nearby values yield an indeterminate result because one may differ from Coordinated Universal Time, UTC by –13 to +12 hours. These instant types thus introduce a partial ordering.

Another warning: ISO 8601 supports time zones only as UTC offsets, ignoring daylight saving time. An application using Central European Summer Time must know whether a date falls in summer, two hours ahead of UTC, or winter, one hour ahead, to specify its zone and compare instants. ISO 8601 ignores named zones such as PST, BST and WET used in everyday life. Omitting the zone is a somewhat dangerous shorthand for local time, whatever that may be.

Data types

Instant: xs:dateTime

The type xs:dateTime defines a specific point in time, a subset of ISO 8601’s instant in time. Its lexical format is “CCYY-MM-DDThh:mm:ss”, C for century, YY for two-digit year, and so on. All fields are required, optionally preceded by a sign and extra leading digits when needed. Fractional seconds may follow, then an optional time zone: “Z” for UTC or an offset from UTC.

The value space of xs:dateTime is the instant itself. The zone specifying it, if present, is considered insignificant, which causes problems for some applications. Even though 2002-01-18T12:00:00+00:00 und 2002-01-18T11:00:00-01:00 refer to the same instant, they carry different zones, which should be reflected in the value space.

Valid values for xs:dateTime include:

2001-10-26T21:32:52 2001-10-26T21:32:52+02:00 2001-10-26T19:32:52Z 2001-10-26T19:32:52+00:00 -2001-10-26T21:32:52 2001-10-26T21:32:52.12679

The following values are invalid:

2001-10-26 (all components are required)
2001-10-26T21:32 (all components are required)
2001-10-26T25:32:52+02:00 (hour 25 is out of range)
01-10-26T21:32 (all components are required)

Three examples above have identical values in the value space:

2001-10-26T21:32:52+02:00 2001-10-26T19:32:52Z 2001-10-26T19:32:52+00:00

The first value, 2001-10-26T21:32:52, without a zone, is considered an indeterminate value between 2001-10-26T21:32:52-14:00 and 2001-10-26T21:32:52+14:00. National daylight-saving rules can alter this range. At publication it ranged from -13:00 to +12:00, but the Working Group allowed generously for possible rule changes.

Regardless of uncertainty when no zone is specified, the W3C XML Schema Recommendation treats all zoneless instants as implicitly sharing the same unknown zone, so they can be compared. This suits single-zone local applications, but can confuse and cause errors in global applications or when calculating intervals spanning different daylight-saving phases within one zone.

Intervals: xs:date, xs:gYearMonth and xs:gYear

The lexical space of type xs:date is the date part of xs:dateTime. Like xs:dateTime , it includes a zone, which should always be given for unambiguous date comparisons. According to W3C XML Schema, a date is a day in its zone, regardless of its length in hours. Thus dates in different zones are unequal unless they denote the same interval, for example 2001-10-26+12:00 and 2001-10-25-12:00. Also, ordering dates with and without zones is partial, as with xs:dateTime.

Valid values for xs:date include:

2001-10-26 2001-10-26+02:00 2001-10-26Z 2001-10-26+00:00 -2001-10-26 -20000-04-01

The following values are invalid:

2001-10 (all components are required)
2001-10-32 (day 32 is out of range)
2001-13-26+02:00 (month 13 is out of range)
01-10-26 (the century is missing)

xs:date represents a Gregorian-calendar day and could therefore have been called “gYearMonthDay”. xs:gYearMonth, g for Gregorian, represents a Gregorian month; xs:gYear represents a year. All three are fixed intervals with optional zones. They differ only in length, one day, month or year, and format, their lexical spaces.

The format of xs:gYearMonth is that of xs:date without the day. Valid values for xs:gYearMonth include:

2001-10 2001-10+02:00 2001-10Z 2001-10+00:00 -2001-10 -20000-04

The following values are invalid:

2001 (the month is missing)
2001-13 (month out of range)
2001-13-26+02:00 (month out of range)
01-10 (the century is missing)

The format of xs:gYear is that of xs:gYearMonth without the month. Valid values for xs:gYear include:

2001 2001+02:00 2001Z 2001+00:00 -2001 -20000

The following values are invalid:

01 (the century is missing)
2001-13 (no month component is allowed)

This interval support is restrictive: only whole Gregorian days, months and years are allowed, without arbitrary lengths or starting times.

Recurring instants: xs:time

The lexical space of xs:time is the time part of xs:dateTime. The semantics of xs:time describes an instant recurring daily: 01:20:15 means the instant recurring every day at 01:20:15. Like xs:date and xs:dateTime , xs:time accepts an optional zone. Comparing times with and without zones poses the same problem.

Although 01:20:15 commonly denotes a duration of 1 hour, 20 minutes and 15 seconds, durations use a different format.

Valid values for xs:time include:

21:32:52 21:32:52+02:00 19:32:52Z 19:32:52+00:00 21:32:52.12679

The following values are invalid:

21:32 (all components are required)
25:25:10 (hour out of range)
-10:00:00 (hour out of range)
1:20:10 (all digits are required)

Recurring-instant support is also restrictive: recurrence must be one Gregorian calendar day, not an arbitrary period.

Recurring intervals: xs:gDay, xs:gMonth and xs:gMonthDay

We have seen instants, intervals and recurring instants. Recurring intervals complete the list. W3C XML Schema supports three predefined recurring intervals corresponding to Gregorian months recurring yearly and days recurring monthly or yearly. Both recurrence, only a Gregorian year or month, and interval are limited: starts can only be a Gregorian day or month, and durations only a Gregorian month or year.

xs:gDay is a one-day Gregorian interval recurring every Gregorian month. The lexical representation of xs:gDay is ---DD with an optional zone. Valid values for xs:gDay include:

---01 ---01Z ---01+02:00 ---01-04:00 ---15 ---31

The following values are invalid:

--30- (the format must be “---DD”)
---35 (day out of range)
---5 (all digits are required)
15 (leading “---” missing)

Date and interval arithmetic applies here too. Days are clamped to each month’s valid range: in our “--31” example, January 31, February 28 or 29, March 31, April 30, and so on.

xs:gMonthDay is a one-day Gregorian interval recurring every Gregorian year. The lexical representation of xs:gMonthDay is --MM-DD with an optional zone. Valid values for xs:gMonthDay include:

--05-01 --11-01Z --11-01+02:00 --11-01-04:00 --11-15 --02-29

The following values are invalid:

-01-30- (the format must be --MM-DD)
--01-35 (day out of range)
--1-5 (all digits are required)
01-15 (leading “--” missing)

xs:gMonth is a one-month Gregorian interval recurring every Gregorian year. The Recommendation defines the lexical representation of xs:gMonth as --MM-- with an optional zone. The W3C XML Schema Working Group acknowledged an error: the ISO 8601 format --MM should have been used. Whether to prohibit or merely deprecate the Recommendation’s format is undecided, but the recommended format is --MM, assuming tools already support it. Valid values for xs:gMonth include:

--05 --11Z --11+02:00 --11-04:00 --02

The following values are invalid:

-01- (the format must be --MM)
--13 (month out of range)
--1 (both digits are required)
01 (leading “--” missing)

xs:duration

Naive programmers who think duration is simple should read the Recommendation: “xs:duration is defined as a six-dimensional space.” Mathematicians might object that most axes of these dimensions are parallel. But programmers promising one month and three days actually specify 31 to 34 days. Attempts by W3C XML Schema to address this beyond ISO 8601 have made duration comparisons somewhat more indeterminate.

The lexical space of xs:duration follows the ISO 8601 format PnYnMnDTnHnMnS , with uppercase separators omitted when their corresponding numbers are absent. Unlike xs:dateTime , no component is required or range-limited. Units can be freely chosen and combined, for example P1Y2MT123S, 1 year, 2 months and 123 seconds. This flexibility has a cost: the duration is not fully specified. A year has 365 or 366 days and two months 59 to 62 days. Durations are not always comparable; their ordering is partial. The next chapter shows how custom types derived from xs:duration can restrict the components used to describe intervals and eliminate these uncertainties.

Once its starting instant is known, a duration has a fixed value. The Working Group identified four starting instants producing the largest variations when adding days, months and other components:

1696-09-01T00:00:00Z 1697-02-01T00:00:00Z 1903-03-01T00:00:00Z 1903-07-01T00:00:00Z

A duration comparison is undefined exactly when its result varies according to which of those four starting instants is used.

Valid values for xs:duration include:

PT1004199059S PT130S PT2M10S P1DT2S -P1Y P1Y2M3DT5H20M30.123S

The following values are invalid:

1Y (leading P missing)
P1S (separator T missing)
P-1Y (all components must be nonnegative)
P1M2Y (component order matters; Y must precede M)
P1Y-1M (all components must be nonnegative)

   

 

 

 

back to top