Selecting Dates and Times
From InterBase
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:
- 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 a variable of typestruct 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. - Create a host variable of type
ISC_TIMESTAMP
. For example, the host-variable declaration might look like this:ISC_TIMESTAMP hire_date;
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
. - 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 withgpre
.isc_decode_timestamp()
requires two parameters: the address of theisc_timestamp
host-language variable, and the address of thestruct tm
host-language variable. For example, the following code fragment covertshire_date
tohire_time
:isc_decode_timestamp(&hire_date, &hire_time);