XQuery uses three types to represent durations of time:
xs:duration
,
xs:yearMonthDuration
, and xs:dayTimeDuration
.
The xs:duration
type represents a duration of time expressed as a number of years, months, days, hours, minutes, and seconds. Its format is PnYnMnDTnHnMnS
, where P
is a literal value that starts the expression, nY
is the number of years followed by a literal Y
, and so on where M
refers to months, D
refers to days, H
refers to hours, the second M
refers to minutes, and S
refers to seconds. T
is a literal value that separates the date and time. The numbers are all integers, except the number of seconds, which may be a decimal number. It is also possible to have a negative duration by preceding the P
with a minus sign (−
).
The xs:duration
type is not totally ordered, meaning that values of this type cannot always be compared. For example, if you try to determine whether the xs:duration
value P1M
is greater than or less than the xs:duration
value P30D
, it is ambiguous. Months may have 28, 29, 30, or 31 days. So, is 30 days less than a month, or not?
Because xs:duration
is not ordered, XQuery defines two types that are derived from duration: xs:yearMonthDuration
and xs:dayTimeDuration
.
By ensuring that month and day components never appear in the same duration, the ambiguity is eliminated.
Values of type xs:yearMonthDuration
can only specify years and months, and they are represented as PnYnM
. Values of type xs:dayTimeDuration
can only specify days, hours, minutes, and seconds, and are represented as PnDTnHnMnS
.
In previous versions of XQuery (including the Candidate Recommendation), the names of the types yearMonthDuration
and dayTimeDuration
were prefixed with xdt:
instead of xs:
because they were in the now defunct XPath Datatypes Namespace rather than the XML Schema Namespace. Some processors still support the old names for these types instead.
Table 19-3 summarizes the three duration types and provides some examples.
Table 19-3. Summary of duration types
Type name |
Description |
Format |
Examples |
---|---|---|---|
|
Duration of time |
|
|
|
Duration in years and months |
|
|
|
Duration in days, hours, minutes, and seconds |
|
|
Two values of xs:duration
can be tested for equality (or inequality), but you cannot compare them using the operators <
, <=
, >
, or >=
because attempting to do so results in a type error. However, values of one of the two derived types, xs:yearMonthDuration
and xs:dayTimeDuration
can be compared because they are totally ordered. They are compared based on the number of months and the number of seconds, respectively.
For example, the xs:yearMonthDuration
value P1Y
is equal to the xs:yearMonthDuration
value P12M
. Even though they have different components, they represent an equal number of months. However, xs:yearMonthDuration
values cannot be compared with xs:dayTimeDuration
values, or with xs:duration
values.
Table 19-4 shows some examples of comparing duration values.
Table 19-4. Examples of comparing durations
Example |
Value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
Type error |
|
Type error |
3.135.247.181