add clock_gettime for macos 10.11 and earlier

This commit is contained in:
Brent Cook
2018-03-22 20:50:24 -05:00
parent 78600e9bec
commit 987aa6a084
5 changed files with 47 additions and 4 deletions

View File

@@ -0,0 +1,26 @@
#include <time.h>
#include <mach/mach_time.h>
#define ORWL_NANO (+1.0E-9)
#define ORWL_GIGA UINT64_C(1000000000)
int
clock_gettime(clock_id_t clock_id, struct timespec *tp)
{
static double orwl_timebase = 0.0;
static uint64_t orwl_timestart = 0;
if (!orwl_timestart) {
mach_timebase_info_data_t tb = { 0 };
mach_timebase_info(&tb);
orwl_timebase = tb.numer;
orwl_timebase /= tb.denom;
orwl_timestart = mach_absolute_time();
}
double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase;
tp->tv_sec = diff * ORWL_NANO;
tp->tv_nsec = diff - (tp->tv_sec * ORWL_GIGA);
return 0;
}