Converting Dates from C to InterBase Format
From InterBase
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:
- 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:To create host-language time structures in languages other than C and C++, see the host-language reference manual.#include <time.h>; . . . struct tm entry_time; . . .
- Create a host variable of type
ISC_TIMESTAMP, for use by InterBase. For example, the host-variable declaration might look like this:TheISC_TIMESTAMP mytime;
ISC_TIMESTAMPstructure is declared inibase.h, but the programmer must declare actual host-language variables of typeISC_TIMESTAMP. - Put date information into
entry_time. - 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);
- Insert the date into a table.