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:

  1. 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 the time.h header file. The following C code includes that header file, and declares a struct 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.
  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 mydate;
    
    The ISC_TIMESTAMP structure is automatically declared for programs when they are preprocessed with gpre, but the programmer must declare actual host-language variables of type ISC_TIMESTAMP.
  3. Put date information into hire_time.
  4. Use the InterBase isc_encode_timestamp() function to convert the information in hire_time into InterBase internal format and store that formatted information in the ISC_TIMESTAMP host variable (hire_date in the example). This function is automatically declared for programs when they are preprocessed with gpre. isc_encode_timestamp() requires two parameters, the address of the UNIX time structure, and the address of the ISC_TIMESTAMP host-language variable. For example, the following code converts hire_time to hire_date:
    isc_encode_timestamp(&hire_time, &hire_date);
    
  5. 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);
    

Advance To: