Search Postgresql Archives

c function returning high resolution timestamp

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 );
}




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux