On Thu, April 26, 2007 09:18, Jim Stewart wrote:
> On Thu, 19 Apr 2007, Jim Stewart wrote:
>
>> I'm trying to figure out how to get a database timestamp value
>> (specifically a
>> "timestamp with time zone") into some sort of native Unix/C++ data type.
>> Ideally a struct timeval (ala gettimeofday()), but any type that
>> supports at
>> least millisecond accuracy is ok.  I haven't been able to find anything
>> documented for this.
>>
>> Any suggestions?

The proper way to do this in libpqxx would be to specialize the
from_string<>() function template for this data type.  The from_string
family is used throughout libpqxx to take a string containing postgres's
representation of a data type and convert it into an object of some
appropriate client-side native data type.  All data coming from the
backend is received in string representations determined by postgres, and
from_string decodes that.

In fact this is marked as a "TODO" item in include/pqxx/util.hxx.  Once
such a function exists for, say, struct timeval you can automatically do
things like

   result r = w.exec("SELECT timestamp FROM mytable WHERE id=99");

   timeval tv1 = r[0][0].as<timeval>();

or

   timeval tv2 = {0,0};
   r[0][0].to(tv2);

Naturally this can also be done with other timestamp and interval types. 
The job consists of (1) determining the right types to map to on the
client side, and (2) parsing the string representations of the various
time types.

Of course there is also a matching function template, to_string<>() to
convert from a client-side native data type to a postgres-style string
representation.


Jeroen


_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@gborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general

Reply via email to