xs:duration

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

Values

Explanation

Valid

 
P3Y5M8DT9H25M20S

3 years, 5 months, 8 days, 9 hours, 25 minutes, 20 seconds

P2DT3H

2 day, 3 hours

P25M

25 months (the number of months may be more than 12)

PT25M

25 minutes

P0Y

0 years

-P60Y

Minus 60 years

PT1M30.5S

1 minute, 30.5 seconds

Invalid

 
P16.3D

All numbers except the seconds must be integers

P3D5H

The T must be used to separate days and time components

P-40M

The minus sign must appear first

P1YM5D

The number of months is missing

 

An empty value or zero-length string is not permitted

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.

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

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