The Duration Types

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?

The yearMonthDuration and dayTimeDuration Types

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.

Important

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

xs:duration

Duration of time

PnYnMnDTnHnMnS.SS

P5Y4M5DT3H5M15.5S,

P5Y, PT3H

xs:yearMonthDuration

Duration in years and months

PnYnM

P5Y4M, P15M

xs:dayTimeDuration

Duration in days, hours, minutes, and seconds

PnDTnHnMnS.SS

P5DT3H5M15.5S, PT125S

Comparing Durations

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

xs:yearMonthDuration("P1Y") = xs:yearMonthDuration("P1Y")

true

xs:yearMonthDuration("P1Y") = xs:yearMonthDuration("P12M")

true

xs:yearMonthDuration("P1Y2M") > xs:yearMonthDuration("P1Y3M")

false

xs:dayTimeDuration("P1DT12H") = xs:dayTimeDuration("PT36H")

true

xs:dayTimeDuration("P1DT12H") < xs:dayTimeDuration("PT37H")

true

xs:yearMonthDuration("P1Y") < xs:dayTimeDuration("P366D")

Type error

xs:duration("P11M") < xs:duration("P12M")

Type error

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.181