Skip to content

Instantly share code, notes, and snippets.

@dreid
Created December 1, 2013 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreid/7736422 to your computer and use it in GitHub Desktop.
Save dreid/7736422 to your computer and use it in GitHub Desktop.
from cffi import FFI
ffi = FFI()
ffi.cdef("""
typedef long int time_t;
struct timespec {
time_t tv_sec;
long tv_nsec;
};
int gettime(struct timespec *);
""")
lib = ffi.verify("""
#include <time.h>
#include <mach/clock.h>
#include <mach/mach.h>
int gettime(struct timespec* ts) {
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
return 0;
};
""")
def gettime():
ts = ffi.new('struct timespec *')
lib.gettime(ts)
return (ts.tv_sec * 1000000000) + ts.tv_nsec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment