Skip to content

Instantly share code, notes, and snippets.

@perj
Last active March 23, 2016 09:41
Show Gist options
  • Save perj/f2db2a90fc831ab5e7f2 to your computer and use it in GitHub Desktop.
Save perj/f2db2a90fc831ab5e7f2 to your computer and use it in GitHub Desktop.
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <sys/time.h>
#include <time.h>
static mach_timebase_info_data_t timebase_info;
int
clock_gettime_monotonic(struct timespec *tp) {
uint64_t mticks = mach_absolute_time();
if (timebase_info.denom == 0)
mach_timebase_info(&timebase_info);
uint64_t nsecs = mticks * timebase_info.numer / timebase_info.denom;
tp->tv_sec = nsecs / 1000000000;
tp->tv_nsec = nsecs % 1000000000;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment