The primitive type xs:duration
represents a duration of time. It allows you to specify a number of years (Y
), months (M
), days (D
), hours (H
), minutes (M
), and seconds (S
). The lexical representation of xs:duration
is PnYnMnDTnHnMnS
, where an uppercase P
starts the expression, n
indicates the quantity of each component, and an uppercase letter T
separates the day and time components. For example, the value P3YT5H
represents a period of three years and five hours.
The following rules apply to xs:duration
values:
A minus sign may appear at the beginning of the value (before the P
) to indicate a negative duration.
You can omit components whose quantity is zero, but at least one component is required.
If no time components (hours, minutes, seconds) are specified, the T
cannot appear.
The numbers must be integers, except for the number of seconds, which can include a decimal point. XQuery implementations are required to support up to three fractional digits in the number of seconds, but may support more.
If a decimal point appears in the number of seconds, there must be at least one digit after the decimal point.
Table B-9 lists some values of the xs:duration
type. For more information on working with durations, see Chapter 19.
Table B-9. Values of the xs:duration type
The xs:duration
type is not totally ordered, meaning that values of this type cannot be compared because it is sometimes ambiguous. 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?
For this reason, XQuery defines two new 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 xs:duration
can be tested for equality (or inequality) with other values of the same type. Two xs:duration
values will be considered equal if they have the same (normalized) number of months and seconds. For example, P1YT60S
is equal to P12MT1M
because they represent the same duration of time (12 months and 60 seconds).[*] However, you cannot compare them using the operators <
, <=
, >
, or >=
. These operators can be used on the two ordered subtypes.
[*] This is in contrast to the way they are handled in XML Schema validation, where they would be considered two different values.
18.191.67.40