c function returning high resolution timestamp

From: Ron Peterson <ron(dot)peterson(at)yellowbank(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: c function returning high resolution timestamp
Date: 2006-10-20 12:44:04
Message-ID: 20061020124404.GA31422@yellowbank.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 );
}

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Taras Kopets 2006-10-20 12:56:37 Re: Link error: LNK2019: unresolved external symbol _pg_detoast_datum
Previous Message Andreas Seltenreich 2006-10-20 12:10:48 Re: why not kill -9 postmaster