Converting Dates from C to InterBase Format
The following steps show how to convert the TIMESTAMP datatype from C to InterBase format; the same steps could be used to convert the TIME and DATE data types by substituting the appropriate API call listed on page 9-1. To insert a timestamp in a table, it must be converted from the host-language format into InterBase format, and then stored. To perform the conversion and insertion in a C program, follow these steps:
- Create a host variable for a C time structure. Most C and C++ development systems provide a type, struct tm, for the C time structure in the time.h header file. The following C code includes that header file, and declares a variable of type struct tm:
- #include <time.h>;
- . . .
- truct tm entry_time;
- . . .
- To create host-language time structures in languages other than C and C++, see the host-language reference manual.
- Create a host variable of type ISC_TIMESTAMP, for use by InterBase. For example, the host-variable declaration might look like this:
- ISC_TIMESTAMP mytime;
- The ISC_TIMESTAMP structure is declared in ibase.h, but the programmer must declare actual host-language variables of type ISC_TIMESTAMP.
- Put date information into entry_time.
- Use the InterBase isc_encode_sql_date() function to convert the information in entry_time into InterBase internal format and store that formatted information in the ISC_TIMESTAMP host variable (entry_date in the example). This function is also declared in ibase.h. isc_encode_timestamp() requires two parameters, the address of the C time structure, and the address of the ISC_TIMESTAMP host-language variable. For example, the following code converts entry_time to entry_date:
- isc_encode_timestamp(&entry_time, &entry_date);
- Insert the date into a table.