# xs:time

Converts $string-var (a string in the time format) to the time data type.

If the value of $string-var is the empty sequence, the empty sequence is returned. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.

If the value of $string-var is not valid to the time format the following error is reported:

Could not cast "invalid_time_string" to type [time@htttp://www.w3.org/2001/XMLSchema] is displayed.

Where invalid_time_string is the string not valid to the time format, for example: "23:5:44".

### Signatures

xs:time(xs:string $string-var) —> xs:time

### Arguments

Data Type | Argument | Description
--- | --- | ---
xs:string | $string-var | Represents a string with the time specified with one of the following formats: hh:mm:ss.sssssss hh:mm:ss.sssssss Z hh:mm:ss+hh:mm hh:mm:ss-hh:mm
| YYYY | Year.
MM | Month (as a number).
DD | Day.
hh | Number of hours.
mm | Number of minutes.
ss.sssssss | Number of seconds. Seconds can be specified up to 7 digits after the decimal place. (Format xx.xxxxxxx)
: | Separator between hours, minutes and seconds.
+ | Positive time zone offset. This option is optional. If a plus or minus is not specified, + is assumed.
- | Negative time zone offset. (Optional)
hh | Number of hours that the time zone differs from UTC.
mm | Number of minutes that the time zone differs from UTC.
Z | Indicates that the time corresponds to the UTC time zone.

### Returns

Returns the specified time in the time data type.

### Examples

#### Simple

Invoking time("23:15:45") returns a time value corresponding to 11:15 PM and 45 seconds in the current time zone, as shown in the following example query:

    let $mytime := xs:time("23:15:45")
    return
    <components>
    	<hours>{xf:get-hours-from-time($mytime)}</hours>
    	<minutes>{xf:get-minutes-from-time($mytime)}</minutes>
    	<seconds>{xf:get-seconds-from-time($mytime)}</seconds>
    </components>

The preceding query, generates the following XML result:

    <components>
    	<hours>23</hours>
    	<minutes>15</minutes>
    	<seconds>45</seconds>
    </components>

#### UTC Time Zone

Invoking time("23:15:45Z") returns a time value corresponding to 11:15 PM and 45 seconds in the UTC time zone, as shown in the following example query:

    let $mytime := xs:time("23:15:45Z")
    return
    <components>
    	<hours>{xf:get-hours-from-time($mytime)}</hours>
    	<minutes>{xf:get-minutes-from-time($mytime)}</minutes>
    	<seconds>{xf:get-seconds-from-time($mytime)}</seconds>
    </components>

The preceding query, generates the following XML result:

    <components>
    	<hours>23</hours>
    	<minutes>15</minutes>
    	<seconds>45</seconds>
    </components>

#### Error—Incorrect Format

Invoking time("23:5:44") outputs an error because the minutes is not specified using two digits.

For example, the following example query:

    <result>{xs:time("23:5:44")}</result>

    Produces the following error:

    Error occurred while executing XQuery: Could not cast "23:5:44" to type [time@http://www.w3.org/2001/XMLSchema]

Related Topics

W3C time data type description