c function returning high resolution timestamp - Mailing list pgsql-general

From Ron Peterson
Subject c function returning high resolution timestamp
Date
Msg-id 20061020124404.GA31422@yellowbank.com
Whole thread Raw
In response to Re: uuid c function contrib  (Ron Peterson <ron.peterson@yellowbank.com>)
Responses Re: c function returning high resolution timestamp  (Andreas Seltenreich <andreas+pg@gate450.dyndns.org>)
List pgsql-general
On Wed, Oct 18, 2006 at 04:43:40PM -0400, Ron Peterson wrote:
> On Wed, Oct 18, 2006 at 04:31:57PM -0400, Ron Peterson wrote:
>
> > I'm having a hard time finding any examples of functions returning
> > timestamps I can study to see how they are handled internally.  I'm sure
> > it's only a line or two of code.
>
> ...I just found date.c

I'm pretty close, but I'm still not understanding something about
PostgreSQL's internal timestamp representation.  If I do 'select
now();', I get a return value with microsecond resolution, which would
seem to indicate that internally, PostgreSQL is using an INT64 value
rather than a float to hold the timestamp.  My function below, however,
always takes the float path through the ifdef.  If I force the int64
path, I just get a garbage timestamp which still only has a seconds
resolution.

What do I need to do to generate a high resolution timestamp?

TIA.

Ron Peterson
https://www.yellowbank.com/


#include "postgres.h"
#include "fmgr.h"
#include "utils/datetime.h"
#include <uuid/uuid.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>

PG_FUNCTION_INFO_V1( y_uuid_time );
Datum
y_uuid_time( PG_FUNCTION_ARGS )
{
    if( PG_ARGISNULL(0) ) {
        PG_RETURN_NULL();
    }
    bytea* uuid = PG_GETARG_BYTEA_P(0);

    typedef unsigned int uint;

    uuid_t uu;
    struct timeval tv;
    time_t t;
    Timestamp ts;
    uint epoch_offset;

    epoch_offset = (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;

    memcpy( uu, VARDATA( uuid ), 16 );

    t = uuid_time( uu, &tv );

#ifdef HAVE_INT64_TIMESTAMP
    ts = (tv.tv_sec - epoch_offset) * 1000000 + tv.tv_usec;
#else
    ts = (double)(tv.tv_sec - epoch_offset);
#endif

    PG_RETURN_TIMESTAMP( ts );
}



pgsql-general by date:

Previous
From: Andreas Seltenreich
Date:
Subject: Re: why not kill -9 postmaster
Next
From: "Taras Kopets"
Date:
Subject: Re: Link error: LNK2019: unresolved external symbol _pg_detoast_datum