Selecting Dates and Times

From InterBase
Jump to: navigation, search

Go Up to Working with Dates and Times


To select a date and time (timestamp) from a table, and convert it to a form usable in a C language 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 variable of type struct tm:
    #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 example, the host-variable declaration might look like this:
    ISC_TIMESTAMP hire_date;
    
    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. Retrieve a timestamp from a table into the ISC_TIMESTAMP variable. For example,
    EXEC SQL
    SELECT LAST_NAME, FIRST_NAME, DATE_OF_HIRE
    INTO :lname, :fname, :hire_date
    FROM EMPLOYEE
    WHERE LAST_NAME = 'Smith' AND FIRST_NAME = 'Margaret';
    

    Convert the isc_timestamp variable into a numeric UNIX format with the InterBase function, isc_decode_timestamp(). This function is automatically declared for programs when they are preprocessed with gpre. isc_decode_timestamp() requires two parameters: the address of the ­isc_timestamp host-language variable, and the address of the struct tm ­hos­t­-language variable. For example, the following code fragment coverts hire_date to hire_time:

    isc_decode_timestamp(&hire_date, &hire_time);
    

Advance To: