c function returning high resolution timestamp

Поиск
Список
Период
Сортировка
От Ron Peterson
Тема c function returning high resolution timestamp
Дата
Msg-id 20061020124404.GA31422@yellowbank.com
обсуждение исходный текст
Ответ на Re: uuid c function contrib  (Ron Peterson <ron.peterson@yellowbank.com>)
Ответы Re: c function returning high resolution timestamp  (Andreas Seltenreich <andreas+pg@gate450.dyndns.org>)
Список 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 по дате отправления:

Предыдущее
От: Andreas Seltenreich
Дата:
Сообщение: Re: why not kill -9 postmaster
Следующее
От: "Taras Kopets"
Дата:
Сообщение: Re: Link error: LNK2019: unresolved external symbol _pg_detoast_datum