Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XZPqh-0007N4-Fy for pgsql-interfaces@arkaria.postgresql.org; Wed, 01 Oct 2014 19:46:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XZPqg-0007BX-1J for pgsql-interfaces@arkaria.postgresql.org; Wed, 01 Oct 2014 19:46:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XZPqf-0007BR-Ad for pgsql-interfaces@postgresql.org; Wed, 01 Oct 2014 19:46:05 +0000 Received: from [2001:41d0:2:9c82::1] (helo=ks311736.kimsufi.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XZPqX-0000To-Cu for pgsql-interfaces@postgresql.org; Wed, 01 Oct 2014 19:46:03 +0000 Received: from sd-36720.dedibox.fr (195-154-189-246.rev.poneytelecom.eu [195.154.189.246]) by ks311736.kimsufi.com (Postfix) with ESMTP id 9DC04C05F5; Wed, 1 Oct 2014 21:59:13 +0200 (CEST) Received: by sd-36720.dedibox.fr (Postfix, from userid 1001) id 7EA1B38A0583; Wed, 1 Oct 2014 21:45:54 +0200 (CEST) Content-Type: text/plain; charset="iso-8859-15" Content-Disposition: inline Content-Transfer-Encoding: 7bit MIME-Version: 1.0 Subject: Re: libpq binary data From: "Daniel Verite" To: thilo@riessner.de CC: pgsql-interfaces@postgresql.org In-Reply-To: <3808965.tNLrlL6qTR@thilo.site> Date: Wed, 01 Oct 2014 21:45:50 +0200 Message-ID: <68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm> X-Mailer: Manitou v1.3.1 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2001:41d0:2:9c82::1 (failed) X-Pg-Spam-Score: 0.3 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-interfaces Precedence: bulk Sender: pgsql-interfaces-owner@postgresql.org 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 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 -- Sent via pgsql-interfaces mailing list (pgsql-interfaces@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-interfaces