Converting Date and Times from InterBase to C Format

From InterBase
Jump to: navigation, search

Go Up to Working with Conversions


The following steps show how to convert the TIMESTAMP data type from InterBase to C format; the same steps could be used to convert the TIME and DATE data types by substituting the appropriate API call above. Starting with InterBase 6, the TIMESTAMP data type replaces the older DATE data type used in earlier versions.

To select a 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++ development systems provide a type, 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>
    #include <ibase.h>
    . . .
    struct tm entry_time;
    . . .
    
    Note: 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 entry_date;
    
    The ISC_TIMESTAMP structure is declared in ibase.h, but the programmer must declare actual host-language variables of type ISC_TIMESTAMP.
  3. Retrieve a date from a table into the ISC_TIMESTAMP variable.
  4. Convert the ISC_TIMESTAMP variable into a numeric C format with the InterBase function, isc_decode_timestamp(). This function is also declared in ibase.h. isc_decode_timestamp() requires two parameters, the address of the ISC_TIMESTAMP host-language variable, and the address of the struct tm host-language variable. For example, the following code fragment coverts entry_date to entry_time:
    isc_decode_timestamp(&entry_date, &entry_time);
    

Advance To: