Converting Dates from C to InterBase Format

From InterBase
Jump to: navigation, search

Go Up to Working with Conversions

The following steps show how to convert the TIMESTAMP data type 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 the parent page. 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:

1. 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>;
. . .
struct tm entry_time;
. . .
To create host-language time structures in languages other than C and C++, see the host-language reference manual.
2. Create a host variable of type ISC_TIMESTAMP, for use by InterBase. For example, the host-variable declaration might look like this:
The ISC_TIMESTAMP structure is declared in ibase.h, but the programmer must declare actual host-language variables of type ISC_TIMESTAMP.
3. Put date information into entry_time.
4. 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);
5. Insert the date into a table.

Advance To: