thilo@riessner.de wrote:
> I try to get the epoch value of a date via the
> PQexecParams(conn, "SELECT extract(epoch from date + time) as epoch, content
> FROM daten .....);
> In that database, the timestamp ist stored in the two fields date and time.
> I want to get this data in binary form. The
> PQfsize(res, 1);
> tells me, that the size of the returned data is 8 byte (in contrast to the
> standard size of epoch, which is meant to be 4 byte)
> I don't manage to get the epoch valule (seconds since 1970) from that
> returned value.
The doc about EXTRACT(field FROM source) says:
"The extract function returns values of type double precision"
In C, that would presumably map to the "double" 64 bits floating point type.
For code converting the binary representation to a host variable, you may get
bits from postgres itself in backend/libpq/pqformat.c
Otherwise, a working example could look like this
(without guarantee that it's suitable for your platform):
#include <stdint.h>
union { uint64_t i; double fp;
} swap;
uint64_t ibe = *((uint64_t*)PQgetvalue(result, row, column);
swap.i = be64toh(ibe);
And your result would be in swap.fp
Or you if prefer getting an int from postgres, cast the result of extract()
to an integer in the SQL query itself.
Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org