![]() |
|
ESQL/C supports two data types that can hold information about time values:
Figure 6-1 summarizes these two time data types.
Figure 6-1
The header file datetime.h contains the dtime_t and intrvl_t structures, along with a number of macro definitions that you can use to compose qualifier values. Include this file in all C source files that use any datetime or interval host variables:
The decimal.h header file defines the type dec_t, which is a component of the dtime_t and intrvl_t structures.
Because of the multiword nature of these data types, it is not possible to declare an uninitialized datetime or interval host variable named year, month, day, hour, minute, second, or fraction. Avoid the following declarations:
A datetime or interval data type is stored as a decimal number with a scale factor of zero and a precision equal to the number of digits that its qualifier implies. Once you know the precision and scale, you know the storage format. For example, if you define a table column as DATETIME YEAR TO DAY, it contains four digits for year, two digits for month, and two digits for day, for a total of eight digits. It is thus stored as if it were decimal(8,0).
If the default precision of the underlying decimal value is not appropriate, you can specify a different precision. For example, if you have a host variable of type interval, with the qualifier day to day, the default precision of the underlying decimal value is two digits. If you have intervals of one hundred or more days, this precision is not adequate. You can specify a precision of three digits as follows:
For more information on the DATETIME and INTERVAL data types, ../sqlr/intro.htmler to the Informix Guide to SQL: Reference.
Use the datetime data type to declare host variables for database values of type DATETIME. You specify the accuracy of the datetime data type with a qualifier. For example, the qualifier in the following declaration is year to day:
As a host variable, a dtime_t. structure represents a datetime value:
The dtime structure and dtime_t typedef have two parts. Figure 6-2 lists these parts.
Figure 6-2
Field | Description |
---|---|
dt_qual | Qualifier of the datetime value |
dt_dec | Digits of the fields of the datetime value This field is a decimal value. |
Declare a host variable for a DATETIME column with the datetime data type followed by an optional qualifier, as the following example shows:
If you omit the qualifier from the declaration of the datetime host variable, as in the last example, your program must explicitly initialize the qualifier with the macros shown in Figure 6-4 on page 6-10.
Use the interval data type to declare host variables for database values of type INTERVAL. You specify the accuracy of the interval data type with a qualifier. The qualifier in the following declaration is hour to second:
As a host variable, an intrvl_t. represents an interval value:
The intrvl structure and intrvl_t typedef have two parts. Figure 6-3 lists these parts.
Figure 6-3
Field | Description |
---|---|
in_qual | Qualifier of the interval value |
in_dec | Digits of the fields of the interval value This field is a decimal value. |
To declare a host variable for an INTERVAL column, use the interval data type followed by an optional qualifier, as shown in the following example:
If you omit the qualifier from the declaration of the interval host variable, as in the last example, your program must explicitly initialize the qualifier with the macros described in the following section.
In addition to the datetime and interval data structures, the datetime.h file defines the macro functions shown in Figure 6-4 for working directly with qualifiers in binary form.
Figure 6-4
For example, if your program does not provide an interval qualifier in the host-variable declaration, you need to use the interval qualifier macros to initialize and set the interval host variable. In the following example, the interval variable gets a day to second qualifier. The precision of the largest field in the qualifier, day, is set to 2:
When an application fetches or inserts a DATETIME or INTERVAL value, ESQL/C must ensure that the qualifier field of the host variable is valid:
When an application uses a datetime host variable to fetch or insert a DATETIME value, ESQL/C must find a valid qualifier in the datetime host variable. ESQL/C takes one of the following actions, based on the value of the dt_qual field in the dtime_t structure that is associated with the host variable:
When an application uses an interval host variable to fetch or insert an INTERVAL value, ESQL/C must find a valid qualifier in the interval host variable. ESQL/C takes one of the following actions, based on the value of the in_qual field of the intrvl_t structure that is associated with the host variable:
You can fetch a DATETIME or INTERVAL column value into a character (char, string, or fixchar) host variable. ESQL/C converts the DATETIME or INTERVAL column value to a character string before it stores it in the character host variable. This character string conforms to the ANSI SQL standards for DATETIME and INTERVAL values. If the host variable is too short, ESQL/C sets sqlca.sqlwarn.sqlwarn1 to W, fills the host variable with asterisk ( * ) characters, and sets any indicator variable to the length of the untruncated character string.
You can also insert a DATETIME or INTERVAL column value from a character (char, string, fixchar, or varchar) host variable. ESQL/C uses the data type and qualifiers of the column value to convert the character value to a DATETIME or INTERVAL value. It expects the character string to contain a DATETIME or INTERVAL value that conforms to ANSI SQL standards.
If the conversion fails, ESQL/C sets the SQLSTATE status variable to an error-class code (and SQLCODE status variable to a negative value) and the update or insert operation fails.
For more information, see ANSI SQL Standards for DATETIME and INTERVAL Values.
Important: Informix products do not support automatic data conversion from DATETIME and INTERVAL column values to numeric (double, int, and so on) host variables. Nor do Informix products support automatic data conversion from numeric (double, int, and so on) or date host variables to DATETIME and INTERVAL column values.
ANSI SQL Standards for DATETIME and INTERVAL Values
The ANSI SQL standards specify qualifiers and formats for character representations of DATETIME and INTERVAL values. The standard qualifier for a DATETIME value is YEAR TO SECOND, and the standard format is as follows:
The standards for an INTERVAL value specify the following two classes of intervals:
You can use the ESQL/C library functions dtcvasc(), dtcvfmtasc(), dttoasc(), and dttofmtasc() to explicitly convert between DATETIME column values and character strings. To explicitly convert between INTERVAL column values and character strings, you can use the ESQL/C library functions incvasc(), incvfmtasc(), intoasc(), and intofmtasc().
For example, you can perform conversions between the DATETIME and DATE data types with ESQL/C library functions and intermediate strings.