0

I'm trying to compile and create a dynamic linking library for Apache-Age. There's a use of clock_gettime() in src/backend/utils/adt/agetype.c to get the timestamp of query.

SYSTEMTIME st;
    GetSystemTime(&st);
    clock_gettime(st, &ts);
    ms += (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);

I followed this stackoverflow answers but in all the answers clock_gettime() is accepting CLOCK_REALTIME as a parameter which is also not available in MSVC.

But in AGE implementation st variable of type SYSTEMTIME struct is used defined in minwinbase.h

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

How can I make my own clock_gettime() with SYSTEMTIME as a parameter.

Sarthak
  • 380
  • 7

1 Answers1

1

CLOCK_GETTIME & CLOCK_REALTIME are POSIX only. Instead of defining your own clock_gettime() for Windows you can use timespec_get(&ts, TIME_UTC); from time.h.

So, in src/backend/utils/adt/agetype.c:

#include <time.h>
.
.
.
Datum age_timestamp(PG_FUNCTION_ARGS)
{
    agtype_value agtv_result;
    struct timespec ts;
    long ms = 0;

    /* get the system time and convert it to milliseconds */
    timespec_get(&ts, TIME_UTC);
    ms += (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
    
    /* build the result */
    agtv_result.type = AGTV_INTEGER;
    agtv_result.val.int_value = ms;

    PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
}
Ahmar
  • 584
  • 2
  • 7