Inserting Dates and Times
From InterBase
Go Up to Working with Dates and Times
To insert a date and time (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++ compilers provide a typedef declaration,
struct tm
, for the C time structure in thetime.h
header file. The following C code includes that header file, and declares astruct tm
variable,hire_time
:#include <time.h>; . . . struct tm hire_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 mydate;
TheISC_TIMESTAMP
structure is automatically declared for programs when they are preprocessed withgpre
, but the programmer must declare actual host-language variables of typeISC_TIMESTAMP
. - Put date information into
hire_time.
- Use the InterBase
isc_encode_timestamp()
function to convert the information inhire_time
into InterBase internal format and store that formatted information in theISC_TIMESTAMP
host variable (hire_date
in the example). This function is automatically declared for programs when they are preprocessed withgpre
.isc_encode_timestamp()
requires two parameters, the address of the UNIX time structure, and the address of theISC_TIMESTAMP
host-language variable. For example, the following code convertshire_time
tohire_date
:isc_encode_timestamp(&hire_time, &hire_date);
- Insert the date into a table. For example,
EXEC SQL INSERT INTO EMPLOYEE (EMP_NO, DEPARTMENT, DATE_OF_HIRE) VALUES (:emp_no, :deptname, :hire_date);