Date-time Functions





Date-time Functions

This section defines the various date-time functions provided by the XForms core function library. The mismatch between the data types available in XML Schema and XPath is most noticeable when working with values of type xsd:date and xsd:duration or any of their derived data types. This discrepancy is being addressed in XPath 2.0. XForms 1.0 provides an interim solution by defining date-time functions in the XForms core to meet the most common use cases encountered when creating electronic forms.

The date-time functions in the XForms core accept string representation of the appropriate schema types and return a numeric XPath result. They have been designed for use within XPath expressions that appear within property calculate or constraint. Note that all of these functions take legal string representations of the underlying XML Schema type such as xsd:date or xsd:dateTime.

days-from-date

This function takes a string representing a value of type date or dateTime (see XML Schema data types in Figure) and returns the number of days since January 1, 1970.

The minute, hour, and seconds components are ignored if called with a dateTime value; it returns a negative number if the value represents a date before January 1, 1970. As an example,


days-from-date('1971-01-01')

returns 365.

seconds-from-dateTime

This function is similar, except that it accepts a string representing a dateTime and returns the number of seconds since January 1, 1970. The value returned is numeric (possibly including a fractional part). As an example,


seconds-from-dateTime(
'1970-01-02T00:00:00Z')

returns 86400.0, the number of seconds in one day.

seconds

This function takes a string representing a subtype of duration, for example, the duration of a meeting. See type duration in Figure. It returns the number of seconds in the specified duration. As an example,


seconds('P1DT0H0M0.0S')

returns 86400.0, the number of seconds in a 24-hour day.

months

This function is similar, except that it returns a number of months in the specified time period. As an example,


months('P1Y0M')

returns 12.

We used function days-from-date in model property constraint in Figure. Date and time functions are most useful when creating XForms applications that manipulate XML documents dealing with time values, for example, travel bookings or task lists.

6.5.1 Function now

Online transactions may need to capture the time at which a transaction was completed. This can be done by including the current time in the submitted instance. The XForms core library defines a now function that returns the string representation of the current time as represented by type xsd:dateTime.

The value returned by this function can be used in a variety of use cases, for example, to time-stamp data that is being submitted (see Figure). This is achieved by creating an action handler[2] within element model that stores the value returned by now in the instance to be submitted.

[2] The full complement of action handlers provided by XForms will be described in Chapter 7.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows