Converting Dates from C to InterBase Format
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 thetime.hheader file. The following C code includes that header file, and declares a variable of typestruct 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:
ISC_TIMESTAMP mytime;
- The
ISC_TIMESTAMPstructure is declared inibase.h, but the programmer must declare actual host-language variables of typeISC_TIMESTAMP. - 3. Put date information into
entry_time. - 4. Use the InterBase
isc_encode_sql_date()function to convert the information inentry_timeinto InterBase internal format and store that formatted information in theISC_TIMESTAMPhost variable (entry_datein the example). This function is also declared inibase.h. isc_encode_timestamp()requires two parameters, the address of the C time structure, and the address of theISC_TIMESTAMPhost-language variable. For example, the following code convertsentry_timetoentry_date:
isc_encode_timestamp(&entry_time, &entry_date);
- 5. Insert the date into a table.