informix
Informix DataBlade API Programmer's Manual
Using Date and Time Data Types

Date, Time, and Interval Data

The DataBlade API provides support for the following kinds of fixed-point data (which correspond to existing SQL data types).

Type of Fixed-Point
Value
SQL Data Type
Date and/or time DATETIME
Interval INTERVAL

Date and time data is an instant in time that is expressed as a calendar date and time of day. A date and/or time value can also have a precision (which is the number of digits required to store the value) and a scale (which is the end qualifier of the date/time).

Interval data is a span of time; that is, the number of units in either of the following interval classes:

The DataBlade API provides support for date values in both their text and binary representations.

Date, Time, and Interval Text Representation

The text representation of a date, time, or interval value is a quoted string that contains a series of digits and symbols. The DataBlade API supports a text representation for both date/time and interval values as a quoted string with the formats that the following table shows.

Data Type Text Representation
DATETIME Date or time string: The date or time string must match the qualifier of the DATETIME column. The default format of the date or time string for the largest DATETIME column is: "yyyy-mm-dd HH:MM:SS.FFFF"
INTERVAL Interval string: The interval string must match the qualifiers of the INTERVAL column. INTERVAL columns have two classes. The default format of an interval string for the largest year-month interval is: "yyyy-mm" The default format of an interval string for the largest day-time interval is: "dd HH:SS.FFFF"

The text representations in the preceding table use the following abbreviations:

yyyy is the 4-digit year (for a DATETIME) or the number of years
(for an INTERVAL).
mm is the 2-digit month (for a DATETIME) or the number of months
(for an INTERVAL).
dd is the 2-digit day of the month (for a DATETIME) or the number of days (for an INTERVAL).
HH is the 2-digit hour (for a DATETIME) or the number of hours
(for an INTERVAL).
MM is the 2-digit minute (for a DATETIME) or the number of minutes (for an INTERVAL).
SS is the 2-digit second (for a DATETIME) or the number of seconds (for an INTERVAL).
FFFF is a fraction of a second (for a DATETIME) or the number of years (for an INTERVAL). Fractions can be from 1 to 5 digits.

A date or time value in its text representation is often called a date/time string. For example, the following date or time string contains the value for 2pm on July 12, 1999 with a qualifier of year to minute:

Usually, a date or time string must match the qualifier of the date or time binary representation with which it is associated.

The following interval string indicates a passage of 3 years and 6 months:

A locale defines the end-user format of a date or time or interval value. The end-user format is the format in which data appears in a client application when the data is a literal string or character variable. The preceding strings are the end-user formats for the default locale, U.S. English. A nondefault locale can define date or time end-user formats that are particular to a country or culture outside the U.S. You can also customize the end-user format of a date with the GL_DATETIME environment variable. For more information, see the Informix Guide to GLS Functionality.

Date, Time, and Interval Binary Representation

The DataBlade API supports the following SQL data types that can hold information about time values.

DataBlade API
Data Type
SQL Date and Time Data Type
mi_datetime DATETIME
mi_interval INTERVAL

The DATETIME Data Type

The SQL DATETIME data type holds the internal (binary) format of a date and/or time value. It encodes an instant in time expressed as a calendar date and time of day. You choose how precisely a DATETIME value is stored with a qualifier. The precision can range from a year to a fraction of a second. For a complete description of the SQL DATETIME data type, see the Informix Guide to SQL: Reference.

The DATETIME data type uses a machine-independent method to encode the date or time qualifiers. It stores the information in the dtime_t structure, as follows:

The dtime structure and dtime_t typedef have two parts, which Figure 4-1 shows.

Figure 4-1
Fields in the dtime Structure

Field Description
dt_qual The qualifier of the datetime value.
dt_dec The digits of the fields of the datetime value. This field is a decimal value.

Tip: The internal format of the DATETIME data type is often referred to as its binary representation.

The DataBlade API supports the SQL DATETIME data type with the mi_datetime data type. Therefore, the mi_datetime data type holds the binary representation of a date and/or time value.

The mi_datetime data type cannot fit into an MI_DATUM. Therefore, it must be passed by reference within C user-defined routines.

All data types, including mi_datetime, must be passed by reference within client LIBMI applications.

Because the binary representation of a DATETIME (mi_datetime) value is an Informix-proprietary format, you cannot use standard system functions to perform operations on mi_datetime values. Instead, the DataBlade API provides the following support for the DATETIME data type.

Category of DATETIME Function For More Information
Conversion functions Converting Date or Time Between Text and Binary
Arithmetic-operation functions Performing Operations on Date and Time Values

The INTERVAL Data Type

The SQL INTERVAL data type holds the internal (binary) format of an interval value. It encodes a value that represents a span of time. INTERVAL types are divided into two classes: year-month intervals and day-time intervals. A year-month interval can represent a span of years and months, and a day-time interval can represent a span of days, hours, minutes, seconds, and fractions of a second. For a complete description of the SQL INTERVAL data type, see the Informix Guide to SQL: Reference.

The INTERVAL data type uses a machine-independent method to encode the interval qualifiers. It stores the information in the intrvl_t structure, as follows:

The intrvl structure and intrvl_t typedef have the two parts that Figure 4-2 shows.

Figure 4-2
Fields in the intrvl_t Structure

Field Description
in_qual The qualifier of the interval value.
in_dec The digits of the fields of the interval value. This field is a decimal value.

Tip: The internal format of the INTERVAL data type is often referred to as its binary representation.

The DataBlade API supports the SQL INTERVAL data type with the mi_interval data type. Therefore, an mi_interval data type holds the binary representation of an interval value.

The mi_interval data type cannot fit into an MI_DATUM. Therefore, it must be passed by reference within C user-defined routines.

All data types, including mi_interval, must be passed by reference within client LIBMI applications.

Because the binary representation of an INTERVAL (mi_interval) value is an Informix-proprietary format, you cannot use standard system functions to perform operations on mi_interval values. Instead, the DataBlade API provides the following support for the INTERVAL data type.

Category of INTERVAL Function For More Information
Conversion functions Converting Date or Time Between Text and Binary
Arithmetic-operation functions Performing Operations on Date and Time Values

datetime.h Header File

The datetime.h header file contains definitions for use with the DATETIME and INTERVAL data types. The header file datetime.h contains the declarations for the date, time, and interval data types, as follows:

In addition to these data structures, the datetime.h file defines the constants and macros for date and time qualifiers that Figure 4-3 shows.

Figure 4-3
Qualifier Macros and Constants for mi_datetime and mi_interval Data Types

Name of Macro Description
TU_YEAR The time unit for the YEAR qualifier field
TU_MONTH The time unit for the MONTH qualifier field
TU_DAY The time unit for the DAY qualifier field
TU_HOUR The time unit for the HOUR qualifier field
TU_MINUTE The time unit for the MINUTE qualifier field
TU_SECOND The time unit for the SECOND qualifier field
TU_FRAC The time unit for the leading qualifier field of FRACTION
TU_Fn The names for mi_datetime ending fields of FRACTION(n), for n from 1 to 5
TU_START(q) Returns the leading field number from qualifier q
TU_END(q) Returns the trailing field number from qualifier q
TU_LEN(q) Returns the length in digits of the qualifier q
TU_FLEN(f) Returns the length in digits of the first field, f, of an interval qualifier
TU_ENCODE(p,f,t) Creates a qualifier from the first field number f with precision p and trailing field number t
TU_DTENCODE(f,t) Creates a mi_datetime qualifier from the first field number f and trailing field number t
TU_IENCODE(p,f,t) Creates an mi_interval qualifier from the first field number f with precision p and trailing field number t

Tip: For a complete list of date and time macros, consult the datetime.h header file that is installed with your database server. This header file resides in the incl/public subdirectory of the INFORMIXDIR directory.

Figure 4-3 on page 4-15 shows the macro definitions that you can use to compose qualifier values. You need these macros only when you work directly with qualifiers in binary form. For example, if your program does not provide an mi_interval qualifier in the variable declaration, you need to use the mi_interval qualifier macros to initialize and set the mi_interval variable, as the following example shows:

In the previous example, the mi_interval variable gets a day to second qualifier. The precision of the largest field in the qualifier, day, is set to 2.

In addition to the declaration of the dtime_t typedef and the preceding date and time macros, the datetime.h header file declares the DATETIME-type functions of the ESQL/C library. The mitypes.h header file automatically includes datetime.h. In turn, the milib.h header file automatically includes mitypes.h and mi.h automatically includes milib.h. Therefore, you automatically have access to the dtime_t and intrvl_t structures, the mi_datetime and mi_interval data types, any of the date or time macros, or any of the ESQL/C DATETIME-type functions when you include mi.h in your DataBlade API module.

Fetching and Inserting DATETIME and INTERVAL Values

When an application fetches or inserts a DATETIME or INTERVAL value, the DataBlade API module must ensure that the qualifier field of the variable is valid:

Selecting and Inserting into mi_datetime Variables

When a DataBlade API module uses an mi_datetime variable to fetch or insert a DATETIME value, the module must find a valid qualifier in the mi_datetime variable. The DataBlade API takes one the following actions; based on the value of the dt_qual field in the dtime_t structure that is associated with the variable:

Selecting and Inserting into mi_interval Variables

When a DataBlade API module uses an mi_interval variable to fetch or insert an INTERVAL value, the DataBlade API must find a valid qualifier in the mi_interval variable. The DataBlade API takes one of the following actions; based on the value of the in_qual field the intrvl_t structure that is associated with the variable:

Implicit Data Conversion

You can select a DATETIME or INTERVAL column value into a character variable. The DataBlade API converts the DATETIME or INTERVAL column value to a character string before it stores it in the character variable. This character string conforms to the 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 (mi_double_precision, mi_integer, and so on) variables.

You can also insert a DATETIME or INTERVAL column value from a character variable. The DataBlade API 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, the DataBlade API 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.

Important: Informix products do not support automatic data conversion from numeric and mi_date variables to DATETIME and INTERVAL column values.

Transferring Date and Time Data

For date/time and interval data to be portable when transferred across different computer architectures, the DataBlade API provides the following functions to handle type alignment and byte order.

DataBlade API
Function
Description
mi_get_datetime() Copies an aligned mi_datetime value, converting any difference in alignment or byte order on the client computer to that of the server computer
mi_get_interval() Copies an aligned mi_interval value, converting any difference in alignment or byte order on the client computer to that of the server computer
mi_put_datetime() Copies an aligned mi_datetime value, converting any difference in alignment or byte order on the server computer to that of the client computer
mi_put_interval() Copies an aligned mi_interval value, converting any difference in alignment or byte order on the server computer to that of the client computer

The mi_get_datetime, mi_get_interval(), mi_put_datetime(), and mi_put_interval() functions are useful in the send and receive support function of an opaque data type that contains mi_datetime or mi_interval values. They allow you to ensure that DATETIME or INTERVAL values remained aligned when transferred to and from client applications. For more information, see Converting Opaque-Type Data With Machine-Specific Data Types.

Converting Date or Time Between Text and Binary

Both the DataBlade API library and the ESQL/C library provide functions that convert from the text (string) representation of a date, time, or interval value to the binary (internal) representation for DATETIME or INTERVAL, respectively.

DataBlade API Functions for Date and Time Conversion

The DataBlade API provides the following functions for conversion between text and binary representations of date, time, and interval data.

DataBlade API Function Converts From Converts To
mi_datetime_to_string() DATETIME (mi_datetime) Date/time string
mi_interval_to_string() INTERVAL (mi_interval) Interval string
mi_string_to_datetime() Date/time string DATETIME (mi_datetime)
mi_string_to_interval() Interval string INTERVAL (mi_interval)

The mi_datetime_to_string(), mi_interval_to_string(), mi_string_to_datetime(), and mi_string_to_interval() functions convert DATETIME and INTERVAL values to and from the ANSI SQL standards formats for these data types.

The mi_datetime_to_string(), mi_interval_to_string(), mi_string_to_datetime(), and mi_string_to_interval() functions are useful in the input and output support function of an opaque data type that contains mi_datetime and mi_interval values, as long as these values use the ANSI SQL formats. They allow you to convert DATETIME and INTERVAL values between their external format (text) and their internal (binary) format when transferred to and from client applications. For more information, see Converting Opaque-Type Data Between Text and Binary Representations.

ESQL/C Functions for Date, Time, and Interval Conversion

The ESQL/C function library provides functions for conversion between text and binary representations of date, time, and interval data.

Data Conversion for DATETIME Values

The ESQL/C library provides the following functions that convert internal DATETIME (mi_datetime) values to and from char strings.

Function Name Description
dtcvasc() Converts an ANSI-compliant character string to mi_datetime
dtcvfmtasc() Converts a character string to a mi_datetime value
dtextend() Changes the qualifier of a mi_datetime
dttoasc() Converts a mi_datetime value to an ANSI-compliant character string
dttofmtasc() Converts a mi_datetime value to a character string

The dttoasc() and dtcvasc() functions convert mi_datetime values to and from the ANSI SQL standards for DATETIME strings. 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 dttofmtasc() and dtcvfmtasc() functions convert mi_datetime values to and from a date or time string using a time-formatting mask. This time-formatting mask contains the same formatting directives that the DBTIME environment variable supports. (For a list of these directives, refer to the description of DBTIME in the Informix Guide to SQL: Reference.)

The dtextend() function extends an mi_datetime value to a different qualifier. You can use it to convert between DATETIME and DATE values.

To convert a DATETIME value to a DATE value

  1. Use dtextend() to adjust the DATETIME qualifier to year to day.
  2. Apply dttoasc() to create a character string in the form yyyy-mm-dd.
  3. Use rdefmtdate() with a pattern argument of yyyy-mm-dd to convert the string to a DATE value.

To convert a DATE value into a DATETIME value

  1. Declare a variable with a qualifier of year to day (or initialize the qualifier with the value that the TU_DTENCODE (TU_YEAR,TU_DAY) macro returns).
  2. Use rfmtdate() with a pattern of yyyy-mm-dd to convert the DATE value to a character string.
  3. Use dtcvasc() to convert the character string to a value in the prepared DATETIME variable.
  4. If necessary, use dtextend() to adjust the DATETIME qualifier.
Data Conversion for INTERVAL Values

The ESQL/C library provides the following functions that convert internal INTERVAL (mi_interval) values to and from char text.

Function Name Description
incvasc() Converts an ANSI-compliant character string to an interval value
incvfmtasc() Converts a character string to an interval value
intoasc() Converts an interval value to an ANSI-compliant character string
intofmtasc() Converts an interval value to a string
invextend() Copies an interval value under a different qualifier

The intoasc() and incvasc() functions convert mi_interval values to and from the ANSI SQL standards for INVERVAL strings. The ANSI SQL standards specify qualifiers and formats for character representations of DATETIME and INTERVAL values. The standards for an INTERVAL value specify the following two classes of intervals:

The intofmtasc() and incvfmtasc() functions convert mi_interval values to and from an interval string using a time-formatting mask. This time-formatting mask contains the same formatting directives that the DBTIME environment variable supports. (For a list of these directives, refer to the description of DBTIME in the Informix Guide to SQL: Reference.)

Performing Operations on Date and Time Values

The ESQL/C library provides the following functions to perform operations on DATETIME (mi_datetime) and INTERVAL (mi_interval) values.

Function Name Description
dtaddinv() Adds an mi_interval value to a mi_datetime value
dtcurrent() Gets current date and time
dtsub() Subtracts one mi_datetime value from another
dtsubinv() Subtracts an mi_interval value from a mi_datetime value
invdivdbl() Divides an mi_interval value by a numeric value
invdivinv() Divides an mi_interval value by an mi_interval value
invmuldbl() Multiplies an mi_interval value by a numeric value

Any other operations, modifications, or analyses can produce unpredictable results.

Obtaining Date and Time Type Information

Figure 4-4 shows the DataBlade API functions that obtain qualifier information of a DATETIME (mi_datetime) or INTERVAL (mi_interval) value.

Figure 4-4
DataBlade API Functions That Obtain DATETIME or INTERVAL Information

Source DataBlade API Functions
For a data type mi_type_qualifier(), mi_type_precision(), mi_type_scale()
For a UDR argument mi_fp_argprec(), mi_fp_setargprec()
For a UDR return value mi_fp_retprec(), mi_fp_setretprec()
For a column in a row
(or field in a row type)
mi_column_precision()
For an input parameter in a prepared statement mi_parameter_precision()

Suppose you have a table with a single column, dt_col, of type DATETIME YEAR TO SECOND. If row_desc is a row descriptor for a row with this table, the code fragment in Figure 4-5 obtains the name, qualifier, precision, and scale for this column value.

Figure 4-5
Obtaining Type Information for a DATETIME Value

In Figure 4-5, the value in the type_buf buffer for a row of the is:

Qualifier of a Date or Time Data Type

The mi_type_qualifier() function returns the encoded qualifier of a DATETIME or INTERVAL data type from a type descriptor. This qualifier is the internal value that the database server uses to track the complete qualifier range, from the starting field to the end field. It is the value stored in the collength column of the syscolumns table for DATETIME and INTERVAL columns. You can use the qualifier macros and constants (see Figure 4-3 on page 4-15) to interpret this encoded value.

In Figure 4-5, the value in type_qual contains the encoded integer qualifier (3594) for the dt_col column. You can obtain the starting qualifier for the DATETIME from the encoded qualifier with the TU_START macro, as follows:

This TU_START call yields 0, which is the value of the TU_YEAR constant in datetime.h header file. You can obtain also the ending qualifier for the DATETIME from the encoded qualifier with the TU_END macro, as follows:

This TU_END call yields 10, which is the value of the TU_SECOND constant in the datetime.h header file. Therefore, the encoded qualifier 3594 represents the qualifier year to second.

Precision of a Date or Time Data Type

For the DATETIME and INTERVAL data types, the precision is the number of digits required to store a value with the specified qualifier. In Figure 4-5, the call to the mi_type_precision() function saves in type_prec the precision for the dt_col column from its type descriptor. This precision has a value of 14 because a DATETIME YEAR TO SECOND value requires 14 digits:

The DataBlade API also provides functions that obtain DATETIME or INTERVAL precision of a column associated with an input parameter, a UDR argument, UDR return value, or a row column. For a list of these functions, see Figure 4-4 on page 4-25.

Scale of a Date or Time Data Type

For the DATETIME and INTERVAL data types, the scale is the encoded integer value for the end qualifier. In Figure 4-5, the call to the mi_type_scale() function stores in type_scale the scale for the dt_col column. This precision has a value of 10 because the end qualifier for the DATETIME YEAR TO SECOND data type is SECOND, whose encoded value (TU_SECOND) is 10.

The DataBlade API also provides functions that obtain DATETIME or INTERVAL scale of an input parameter, a UDR argument, UDR return value, or column. For a list of these functions, see Figure 4-4 on page 4-25.


Informix DataBlade API Programmer's Manual, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved